Skip to content

Commit

Permalink
feat(color): support lab string of the color
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed Apr 23, 2023
1 parent 56d732d commit fb9def9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ let color = Color::from_hex("#ffff00").unwrap();

Stringify a color to a string.

You can use the `hex`, `rgb`, `rgba`, `hsl`, `hsv`, `hwb`, `cmyk`, `xyz`, `yuv`, `YCbCr`, `name` method to stringify a color to a string.
You can use the `hex`, `rgb`, `rgba`, `hsl`, `hsv`, `hwb`, `cmyk`, `xyz`, `yuv`, `YCbCr`, `lab`, `name` method to stringify a color to a string.

For example:

Expand All @@ -149,6 +149,7 @@ color.xyz(); // "xyz(0.932231, 0.975339, 0.502949)"
color.yuv(); // "yuv(0.886, -0.4359, 0.1)"
color.ycbcr(); // "YCbCr(225.93, 0.5755, 148.7269)"
color.name(); // "yellow"
color.lab(); // "lab(97.14, -21.55, 94.48)"
```

### Color Channel
Expand Down
20 changes: 18 additions & 2 deletions src/color/stringify.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
conversion::{
cmyk::rgb2cmyk, hex::rgb2hex, hsl::rgb2hsl, hsv::rgb2hsv, hwb::rgb2hwb, xyz::rgb2xyz,
ycbcr::rgb2ycbcr, yuv::rgb2yuv,
cmyk::rgb2cmyk, hex::rgb2hex, hsl::rgb2hsl, hsv::rgb2hsv, hwb::rgb2hwb, lab::rgb2lab,
xyz::rgb2xyz, ycbcr::rgb2ycbcr, yuv::rgb2yuv,
},
helper::round,
Color,
Expand Down Expand Up @@ -152,6 +152,19 @@ impl Color {
let (y, u, v) = rgb2yuv(self.rgb);
format!("yuv({}, {}, {})", round(y, 4), round(u, 4), round(v, 4))
}
/// `lab` string of the color
///
/// # Examples
/// ```rust
/// use color_art::Color;
///
/// let color = Color::new(255.0, 255.0, 0.0, 1.0);
/// assert_eq!(color.lab(), "lab(97.14, -21.55, 94.48)");
/// ```
pub fn lab(self) -> String {
let (l, a, b) = rgb2lab(self.rgb);
format!("lab({}, {}, {})", round(l, 2), round(a, 2), round(b, 2))
}
/// `YCbCr` string of the color
///
/// # Examples
Expand Down Expand Up @@ -209,6 +222,7 @@ mod tests {
assert_eq!(color.hwb(), "hwb(0, 100%, 0%)");
assert_eq!(color.xyz(), "xyz(1, 1, 1)");
assert_eq!(color.ycbcr(), "YCbCr(255, 128, 128)");
assert_eq!(color.lab(), "lab(100, 0, 0)");

let color = Color::new(0.0, 0.0, 0.0, 0.5);
assert_eq!(color.hex(), "#000000");
Expand All @@ -219,6 +233,7 @@ mod tests {
assert_eq!(color.hwb(), "hwb(0, 0%, 100%)");
assert_eq!(color.xyz(), "xyz(0.137931, 0.137931, 0.137931)");
assert_eq!(color.ycbcr(), "YCbCr(0, 128, 128)");
assert_eq!(color.lab(), "lab(0, 0, 0)");

let color = Color::new(0.0, 128.0, 128.0, 1.0);
assert_eq!(color.hex(), "#008080");
Expand All @@ -229,5 +244,6 @@ mod tests {
assert_eq!(color.hwb(), "hwb(180, 0%, 50%)");
assert_eq!(color.xyz(), "xyz(0.496222, 0.553915, 0.596299)");
assert_eq!(color.ycbcr(), "YCbCr(89.728, 149.5854, 64.0239)");
assert_eq!(color.lab(), "lab(48.25, -28.85, -8.48)");
}
}
1 change: 0 additions & 1 deletion src/conversion/lab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use super::{
};
use crate::helper::round;

#[allow(unused)]
pub fn rgb2lab(color: (f64, f64, f64)) -> (f64, f64, f64) {
let (x, y, z) = rgb2xyz(color);

Expand Down
4 changes: 4 additions & 0 deletions tests/color_stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ fn test_color_stringify() {
assert_eq!(color.name(), "yellow");
assert_eq!(color.xyz(), "xyz(0.932231, 0.975339, 0.502949)");
assert_eq!(color.yuv(), "yuv(0.886, -0.4359, 0.1)");
assert_eq!(color.lab(), "lab(97.14, -21.55, 94.48)");

let color = Color::from_str("orange").unwrap();
assert_eq!(color.lab(), "lab(74.94, 23.93, 78.95)");
}

0 comments on commit fb9def9

Please sign in to comment.