Skip to content

Commit

Permalink
Implement explicit Color::into_u32 instead of Into trait
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Jun 6, 2023
1 parent 0aff0f6 commit 946d616
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
25 changes: 12 additions & 13 deletions core/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ impl Color {
]
}

/// Converts the [`Color`] into a `u32` value containing its RGBA8 components.
pub fn into_u32(self) -> u32 {
let [r, g, b, a] = self.into_rgba8();

let r = (r as u32) << 24;
let g = (g as u32) << 16;
let b = (b as u32) << 8;
let a = a as u32;

r | g | b | a
}

/// Inverts the [`Color`] in-place.
pub fn invert(&mut self) {
self.r = 1.0f32 - self.r;
Expand All @@ -145,19 +157,6 @@ impl From<[f32; 4]> for Color {
}
}

impl Into<u32> for Color {
fn into(self) -> u32 {
let [r, g, b, a] = self.into_rgba8();

let r = (r as u32) << 24;
let g = (g as u32) << 16;
let b = (b as u32) << 8;
let a = a as u32;

r | g | b | a
}
}

/// Creates a [`Color`] with shorter and cleaner syntax.
///
/// # Examples
Expand Down
8 changes: 4 additions & 4 deletions graphics/src/gradient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ impl Linear {

for (index, stop) in self.stops.iter().enumerate() {
let (color, offset) = stop
.map_or((Color::default().into(), 2.0), |s| {
(s.color.into(), s.offset)
.map_or((Color::default().into_u32(), 2.0), |s| {
(s.color.into_u32(), s.offset)
});
colors[index] = color;
offsets[index] = offset;
Expand Down Expand Up @@ -138,8 +138,8 @@ pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed {

for (index, stop) in linear.stops.iter().enumerate() {
let (color, offset) = stop
.map_or((Color::default().into(), 2.0), |s| {
(s.color.into(), s.offset)
.map_or((Color::default().into_u32(), 2.0), |s| {
(s.color.into_u32(), s.offset)
});

colors[index] = color;
Expand Down

0 comments on commit 946d616

Please sign in to comment.