Skip to content

Commit

Permalink
Use from_be_bytes for Rgba8::to_u32 (#74)
Browse files Browse the repository at this point in the history
Also, add a short sanity check test.

Based on feedback from @DJMcNab in PR #66.
  • Loading branch information
waywardmonkeys authored Nov 26, 2024
1 parent 8ce1814 commit 8394db1
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions color/src/rgba8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Rgba8 {
/// `a` the least.
#[must_use]
pub const fn to_u32(self) -> u32 {
((self.r as u32) << 24) | ((self.g as u32) << 16) | ((self.b as u32) << 8) | self.a as u32
u32::from_be_bytes([self.r, self.g, self.b, self.a])
}
}

Expand Down Expand Up @@ -63,7 +63,7 @@ impl PremulRgba8 {
/// `a` the least.
#[must_use]
pub const fn to_u32(self) -> u32 {
((self.r as u32) << 24) | ((self.g as u32) << 16) | ((self.b as u32) << 8) | self.a as u32
u32::from_be_bytes([self.r, self.g, self.b, self.a])
}
}

Expand All @@ -72,3 +72,27 @@ impl From<Rgba8> for PremulColor<Srgb> {
Self::from_rgba8(value.r, value.g, value.b, value.a)
}
}

#[cfg(test)]
mod tests {
use super::{PremulRgba8, Rgba8};

#[test]
fn to_u32() {
let c = Rgba8 {
r: 1,
g: 2,
b: 3,
a: 4,
};
assert_eq!(0x01020304_u32, c.to_u32());

let p = PremulRgba8 {
r: 0xaa,
g: 0xbb,
b: 0xcc,
a: 0xff,
};
assert_eq!(0xaabbccff_u32, p.to_u32());
}
}

0 comments on commit 8394db1

Please # to comment.