Skip to content

Commit 4c64903

Browse files
Support construction from rgb8 values (#136)
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.
1 parent 2f4652e commit 4c64903

File tree

3 files changed

+167
-141
lines changed

3 files changed

+167
-141
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ This release has an [MSRV][] of 1.82.
2020
* Support for the ACES2065-1 color space. ([#124][] by [@tomcur][])
2121
* A documentation example implementing `ColorSpace`. ([#130][] by [@tomcur][])
2222
* Conversions of `[u8; 4]` and packed `u32` into `Rgba8` and `PremulRgba8` are now provided. ([#135][] by [@tomcur][])
23+
* Support construction of `AlphaColor<Srgb>`, `OpaqueColor<Srgb>` and `PremulColor<Srgb>` from rgb8 values. ([#136][] by [@waywardmonkeys][])
2324

2425
### Fixed
2526

@@ -121,6 +122,7 @@ This is the initial release.
121122
[#129]: https://github.com/linebender/color/pull/129
122123
[#130]: https://github.com/linebender/color/pull/130
123124
[#135]: https://github.com/linebender/color/pull/135
125+
[#136]: https://github.com/linebender/color/pull/136
124126

125127
[Unreleased]: https://github.com/linebender/color/compare/v0.2.2...HEAD
126128
[0.2.2]: https://github.com/linebender/color/releases/tag/v0.2.2

color/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,22 @@ impl AlphaColor<Srgb> {
138138
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), u8_to_f32(a)];
139139
Self::new(components)
140140
}
141+
142+
/// Create a color from 8-bit rgb values with an opaque alpha.
143+
///
144+
/// Note: for conversion from the [`Rgba8`] type, just use the `From` trait.
145+
pub const fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
146+
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), 1.];
147+
Self::new(components)
148+
}
149+
}
150+
151+
impl OpaqueColor<Srgb> {
152+
/// Create a color from 8-bit rgb values.
153+
pub const fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
154+
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b)];
155+
Self::new(components)
156+
}
141157
}
142158

143159
impl PremulColor<Srgb> {
@@ -148,6 +164,14 @@ impl PremulColor<Srgb> {
148164
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), u8_to_f32(a)];
149165
Self::new(components)
150166
}
167+
168+
/// Create a color from 8-bit rgb values with an opaque alpha.
169+
///
170+
/// Note: for conversion from the [`Rgba8`] type, just use the `From` trait.
171+
pub const fn from_rgb8(r: u8, g: u8, b: u8) -> Self {
172+
let components = [u8_to_f32(r), u8_to_f32(g), u8_to_f32(b), 1.];
173+
Self::new(components)
174+
}
151175
}
152176

153177
// Keep clippy from complaining about unused libm in nostd test case.

0 commit comments

Comments
 (0)