Skip to content

Commit

Permalink
Support construction from rgb8 values (#136)
Browse files Browse the repository at this point in the history
This is a common pattern in code in Vello examples and other places
where they're just constructing an opaque color from the r,g,b values
and a solid alpha.
  • Loading branch information
waywardmonkeys authored Jan 19, 2025
1 parent 2f4652e commit 4c64903
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 141 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This release has an [MSRV][] of 1.82.
* Support for the ACES2065-1 color space. ([#124][] by [@tomcur][])
* A documentation example implementing `ColorSpace`. ([#130][] by [@tomcur][])
* Conversions of `[u8; 4]` and packed `u32` into `Rgba8` and `PremulRgba8` are now provided. ([#135][] by [@tomcur][])
* Support construction of `AlphaColor<Srgb>`, `OpaqueColor<Srgb>` and `PremulColor<Srgb>` from rgb8 values. ([#136][] by [@waywardmonkeys][])

### Fixed

Expand Down Expand Up @@ -121,6 +122,7 @@ This is the initial release.
[#129]: https://github.com/linebender/color/pull/129
[#130]: https://github.com/linebender/color/pull/130
[#135]: https://github.com/linebender/color/pull/135
[#136]: https://github.com/linebender/color/pull/136

[Unreleased]: https://github.com/linebender/color/compare/v0.2.2...HEAD
[0.2.2]: https://github.com/linebender/color/releases/tag/v0.2.2
Expand Down
24 changes: 24 additions & 0 deletions color/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,22 @@ impl AlphaColor<Srgb> {
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), u8_to_f32(a)];
Self::new(components)
}

/// Create a color from 8-bit rgb values with an opaque alpha.
///
/// Note: for conversion from the [`Rgba8`] type, just use the `From` trait.
pub const fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), 1.];
Self::new(components)
}
}

impl OpaqueColor<Srgb> {
/// Create a color from 8-bit rgb values.
pub const fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b)];
Self::new(components)
}
}

impl PremulColor<Srgb> {
Expand All @@ -148,6 +164,14 @@ impl PremulColor<Srgb> {
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), u8_to_f32(a)];
Self::new(components)
}

/// Create a color from 8-bit rgb values with an opaque alpha.
///
/// Note: for conversion from the [`Rgba8`] type, just use the `From` trait.
pub const fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), 1.];
Self::new(components)
}
}

// Keep clippy from complaining about unused libm in nostd test case.
Expand Down
Loading

0 comments on commit 4c64903

Please sign in to comment.