Skip to content

Commit

Permalink
Add from_lrgb_u8(), from_lrgba_u8() and to_lrgba_u8().
Browse files Browse the repository at this point in the history
  • Loading branch information
mazznoer committed Jan 23, 2021
1 parent 033419e commit 632fa55
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ impl Color {
Color::from_rgba(from_linear(r), from_linear(g), from_linear(b), a)
}

/// Arguments:
///
/// * `r`: Red value [0..255]
/// * `g`: Green value [0..255]
/// * `b`: Blue value [0..255]
pub fn from_lrgb_u8(r: u8, g: u8, b: u8) -> Color {
Color::from_lrgba(r as f64 / 255., g as f64 / 255., b as f64 / 255., 1.)
}

/// Arguments:
///
/// * `r`: Red value [0..255]
/// * `g`: Green value [0..255]
/// * `b`: Blue value [0..255]
/// * `a`: Alpha value [0..255]
pub fn from_lrgba_u8(r: u8, g: u8, b: u8, a: u8) -> Color {
Color::from_lrgba(
r as f64 / 255.,
g as f64 / 255.,
b as f64 / 255.,
a as f64 / 255.,
)
}

/// Arguments:
///
/// * `h`: Hue angle [0..360]
Expand Down Expand Up @@ -394,6 +418,19 @@ impl Color {
)
}

/// Returns: `(r, g, b, a)`
///
/// * Red, green, blue and alpha in the range [0..255]
pub fn to_lrgba_u8(&self) -> (u8, u8, u8, u8) {
let (r, g, b, a) = self.to_lrgba();
(
(r * 255.).round() as u8,
(g * 255.).round() as u8,
(b * 255.).round() as u8,
(a * 255.).round() as u8,
)
}

/// Returns: `(l, a, b, alpha)`
pub fn to_oklaba(&self) -> (f64, f64, f64, f64) {
let (r, g, b, _) = self.to_lrgba();
Expand Down
3 changes: 3 additions & 0 deletions tests/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn basic() {
assert_eq!(c.to_hsla(), (0., 1., 0.5, 1.));
assert_eq!(c.to_hwba(), (0., 0., 0., 1.));
assert_eq!(c.to_lrgba(), (1., 0., 0., 1.));
assert_eq!(c.to_lrgba_u8(), (255, 0, 0, 255));
//assert_eq!(c.to_oklaba(), (0.6279151939969809, 0.2249032308661071, 0.12580287012451802, 1.));
assert_eq!(c.red(), 1.);
assert_eq!(c.green(), 0.);
Expand Down Expand Up @@ -54,6 +55,8 @@ fn red() {
Color::from_rgb_u8(255, 0, 0),
Color::from_rgba_u8(255, 0, 0, 255),
Color::from_lrgb(1., 0., 0.),
Color::from_lrgb_u8(255, 0, 0),
Color::from_lrgba_u8(255, 0, 0, 255),
Color::from_hsv(0., 1., 1.),
Color::from_hsl(360., 1., 0.5),
Color::from_hwb(0., 0., 0.),
Expand Down

0 comments on commit 632fa55

Please sign in to comment.