Skip to content

Commit

Permalink
refactor!: remove round color channel
Browse files Browse the repository at this point in the history
for improve result accuracy
  • Loading branch information
JiatLn committed Aug 27, 2023
1 parent 90f72b1 commit efb9e71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
36 changes: 20 additions & 16 deletions src/color/color_channel.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{ Color, ColorSpace };
use crate::utils::*;
use crate::{Color, ColorSpace};

/// Color channel extraction methods.
///
Expand Down Expand Up @@ -38,23 +38,23 @@ impl Color {
}
/// Extracts the hue channel of color as a number between 0.0 and 360.0.
pub fn hue(&self) -> f64 {
round(self.vec_of(ColorSpace::HSL)[0], 4)
self.vec_of(ColorSpace::HSL)[0]
}
/// Extracts the HSL saturation of color as a number between 0.0 and 1.0.
pub fn saturation(&self) -> f64 {
round(self.vec_of(ColorSpace::HSL)[1], 4)
self.vec_of(ColorSpace::HSL)[1]
}
/// Extracts the HSL lightness of color as a number between 0.0 and 1.0.
pub fn lightness(&self) -> f64 {
round(self.vec_of(ColorSpace::HSL)[2], 4)
self.vec_of(ColorSpace::HSL)[2]
}
/// Extracts the HWB whiteness of color as a number between 0.0 and 1.0.
pub fn whiteness(&self) -> f64 {
round(self.vec_of(ColorSpace::HWB)[1], 4)
self.vec_of(ColorSpace::HWB)[1]
}
/// Extracts the HWB blackness of color as a number between 0.0 and 1.0.
pub fn blackness(&self) -> f64 {
round(self.vec_of(ColorSpace::HWB)[2], 4)
self.vec_of(ColorSpace::HWB)[2]
}
/// Calculates the [relative luminance](https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef) of color.
///
Expand All @@ -78,31 +78,35 @@ impl Color {
}
/// Extracts the hue channel of color in the HSV color space.
pub fn hsv_hue(&self) -> f64 {
round(self.vec_of(ColorSpace::HSV)[0], 4)
self.vec_of(ColorSpace::HSV)[0]
}
/// Extracts the saturation channel of color in the HSV color space.
pub fn hsv_saturation(&self) -> f64 {
round(self.vec_of(ColorSpace::HSV)[1], 4)
self.vec_of(ColorSpace::HSV)[1]
}
/// Extracts the value channel of color in the HSV color space.
pub fn hsv_value(&self) -> f64 {
round(self.vec_of(ColorSpace::HSV)[2], 4)
self.vec_of(ColorSpace::HSV)[2]
}
/// Calculates the [gray](http://en.wikipedia.org/wiki/Grayscale) value of color.
pub fn gray(&self) -> f64 {
let [r, g, b] = self.rgb;
round(0.299 * r + 0.587 * g + 0.114 * b, 4)
0.299 * r + 0.587 * g + 0.114 * b
}
}

fn luminance_x(x: f64) -> f64 {
if x <= 0.03928 { x / 12.92 } else { ((x + 0.055) / 1.055).powf(2.4) }
if x <= 0.03928 {
x / 12.92
} else {
((x + 0.055) / 1.055).powf(2.4)
}
}

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

#[test]
fn test_color_channel() {
Expand All @@ -113,8 +117,8 @@ 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.whiteness(), 0.0392156862745098);
assert_eq!(color.blackness(), 0.8823529411764706);

assert_eq!(color.luma(), 0.006585790668061925);
assert_eq!(color.luminance(), 0.006585790668061925);
Expand All @@ -138,12 +142,12 @@ mod tests {
assert_eq!(color.hsv_hue(), 90.0);
assert_eq!(color.hsv_saturation(), 1.0);
assert_eq!(color.hsv_value(), 0.5);
assert_eq!(color.gray(), 93.9038);
assert_eq!(color.gray(), 93.90375);

let color = color!(rgb(100, 200, 30));

assert_eq!(color.luma(), 0.44111615679100963);
assert_eq!(color.luminance(), 0.44111615679100963);
assert_eq!(color.gray(), 150.72);
assert_eq!(color.gray(), 150.71999999999997);
}
}
12 changes: 6 additions & 6 deletions tests/color_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ fn test_color_channel() {
assert_eq!(color.alpha(), 1.0);

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.saturation(), 0.6800000000000003);
assert_eq!(color.lightness(), 0.803921568627451);
assert_eq!(color.whiteness(), 0.6705882352941176);
assert_eq!(color.blackness(), 0.06274509803921569);

assert_eq!(color.hsv_hue(), 210.0);
assert_eq!(color.hsv_saturation(), 0.2845);
assert_eq!(color.hsv_value(), 0.9373);
assert_eq!(color.hsv_saturation(), 0.2845188284518829);
assert_eq!(color.hsv_value(), 0.9372549019607843);

assert_eq!(color.luma(), 0.5855256521034803);
assert_eq!(color.luminance(), 0.5855256521034803);
Expand Down

0 comments on commit efb9e71

Please sign in to comment.