diff --git a/README.md b/README.md index 8d5d700..f0f1c32 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ color.lab(); // "lab(97.14, -21.55, 94.48)" ### Color Channel -You can use the `red`, `green`, `blue`, `alpha`, `hue`, `saturation`, `lightness`, `hsv_hue`, `hsv_saturation`, `hsv_value`, `luma`, `luminance`, `gray` method to extract the color channel. +You can use the `red`, `green`, `blue`, `alpha`, `hue`, `saturation`, `lightness`, `whiteness`, `blackness`, `hsv_hue`, `hsv_saturation`, `hsv_value`, `luma`, `luminance`, `gray` method to extract the color channel.
For example @@ -207,6 +207,9 @@ color.hue(); // 210.0 color.saturation(); // 0.68 color.lightness(); // 0.8039 +color.whiteness(); // 0.6706 +color.blackness(); // 0.0627 + color.hsv_hue(); // 210.0 color.hsv_saturation(); // 0.2845 color.hsv_value(); // 0.9373 diff --git a/src/color/color_channel.rs b/src/color/color_channel.rs index f098baa..bea3610 100644 --- a/src/color/color_channel.rs +++ b/src/color/color_channel.rs @@ -47,6 +47,14 @@ impl Color { pub fn lightness(&self) -> f64 { self.vec_of(ColorSpace::HSL)[2] } + /// Returns the HWB whiteness of color as a number between 0.0 and 1.0. + pub fn whiteness(&self) -> f64 { + self.vec_of(ColorSpace::HWB)[1] + } + /// Returns the HWB blackness of color as a number between 0.0 and 1.0. + pub fn blackness(&self) -> f64 { + self.vec_of(ColorSpace::HWB)[2] + } /// Calculates the [luma](http://en.wikipedia.org/wiki/Luma_%28video%29) (perceptual brightness) of a color. pub fn luma(&self) -> f64 { let (r, g, b) = normalize_color(self.rgb); @@ -85,7 +93,7 @@ impl Color { #[cfg(test)] mod tests { - use std::str::FromStr; + use std::{ str::FromStr, assert_eq }; use crate::*; #[test] @@ -97,6 +105,9 @@ mod tests { assert_eq!(color.blue(), 30); assert_eq!(color.alpha(), 0.8); + assert_eq!(color.whiteness(), 0.0392); + assert_eq!(color.blackness(), 0.8824); + assert_eq!(color.luma(), 0.01); assert_eq!(color.luminance(), 0.07); assert_eq!(color.gray(), 18.15); @@ -107,6 +118,9 @@ mod tests { assert_eq!(color.saturation(), 1.0); assert_eq!(color.lightness(), 0.5); + assert_eq!(color.whiteness(), 0.0); + assert_eq!(color.blackness(), 0.0); + assert_eq!(color.luma(), 0.76); assert_eq!(color.luminance(), 0.82); assert_eq!(color.gray(), 187.8075); diff --git a/src/conversion/hwb.rs b/src/conversion/hwb.rs index d3ba1e4..c0f672a 100644 --- a/src/conversion/hwb.rs +++ b/src/conversion/hwb.rs @@ -13,8 +13,8 @@ pub fn rgb2hwb(color: (f64, f64, f64)) -> (f64, f64, f64) { let hsl = hsl::rgb2hsl(color); let hue = round(hsl.0, 0); - let whiteness = round(min, 2); - let blackness = round(1.0 - max, 2); + let whiteness = round(min, 4); + let blackness = round(1.0 - max, 4); (hue, whiteness, blackness) } @@ -43,12 +43,12 @@ mod tests { #[test] fn test_rgb2hwb() { assert_eq!(rgb2hwb((0.0, 255.0, 102.0)), (144.0, 0.0, 0.0)); - assert_eq!(rgb2hwb((86.0, 59.0, 133.0)), (262.0, 0.23, 0.48)); + assert_eq!(rgb2hwb((86.0, 59.0, 133.0)), (262.0, 0.2314, 0.4784)); } #[test] fn test_hwb2rgb() { assert_eq!(hwb2rgb((144.0, 0.0, 0.0)), (0.0, 255.0, 102.0)); - assert_eq!(hwb2rgb((262.0, 0.23, 0.48)), (86.0, 59.0, 133.0)); + assert_eq!(hwb2rgb((262.0, 0.2314, 0.4784)), (86.0, 59.0, 133.0)); } } diff --git a/tests/color_channel.rs b/tests/color_channel.rs index c7228d8..3ee40dd 100644 --- a/tests/color_channel.rs +++ b/tests/color_channel.rs @@ -12,6 +12,8 @@ fn test_color_channel() { assert_eq!(color.hue(), 210.0); assert_eq!(color.saturation(), 0.68); assert_eq!(color.lightness(), 0.8039); + assert_eq!(color.whiteness(), 0.6706); + assert_eq!(color.blackness(), 0.0627); assert_eq!(color.hsv_hue(), 210.0); assert_eq!(color.hsv_saturation(), 0.2845);