Skip to content

Commit

Permalink
feat(Color): add whiteness & blackness
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed May 16, 2023
1 parent 0e643ee commit 6b14c12
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<details>
<summary>For example</summary>
Expand All @@ -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
Expand Down
16 changes: 15 additions & 1 deletion src/color/color_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -85,7 +93,7 @@ impl Color {

#[cfg(test)]
mod tests {
use std::str::FromStr;
use std::{ str::FromStr, assert_eq };
use crate::*;

#[test]
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/conversion/hwb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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));
}
}
2 changes: 2 additions & 0 deletions tests/color_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 6b14c12

Please sign in to comment.