Skip to content

Commit

Permalink
fix: improve rgb2hsl precision
Browse files Browse the repository at this point in the history
  • Loading branch information
JiatLn committed May 6, 2023
1 parent 9d8a60d commit 34bef2f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ color.alpha(); // 1.0

color.hue(); // 210.0
color.saturation(); // 0.68
color.lightness(); // 0.8
color.lightness(); // 0.8039

color.hsv_hue(); // 210.0
color.hsv_saturation(); // 0.28
Expand Down
13 changes: 13 additions & 0 deletions src/color/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,5 +300,18 @@ mod tests {
assert_eq!(color.ycbcr(), "YCbCr(89.728, 149.5854, 64.0239)");
assert_eq!(color.lab(), "lab(48.25, -28.85, -8.48)");
assert_eq!(color.name(), "teal");

let color = Color::new(161, 110, 87, 1.0);
assert_eq!(color.hex(), "#a16e57");
assert_eq!(color.rgb(), "rgb(161, 110, 87)");
assert_eq!(color.rgba(), "rgba(161, 110, 87, 1)");
assert_eq!(color.hsl(), "hsl(19, 30%, 49%)");
assert_eq!(color.hsla(), "hsla(19, 30%, 49%, 1)");
assert_eq!(color.hsv(), "hsv(19, 46%, 63%)");
assert_eq!(color.hwb(), "hwb(19, 34%, 37%)");
assert_eq!(color.xyz(), "xyz(0.613946, 0.579082, 0.474123)");
assert_eq!(color.ycbcr(), "YCbCr(122.627, 107.9064, 155.3599)");
assert_eq!(color.lab(), "lab(51.17, 17.43, 20.99)");
assert_eq!(color.name(), "#a16e57");
}
}
4 changes: 2 additions & 2 deletions src/color_calc/distance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{Color, ColorSpace};
/// let color2 = Color::from_str("#fff").unwrap();
///
/// let d = distance_with(&color1, &color2, ColorSpace::HSL);
/// assert_eq!(d, 60.01000749874974);
/// assert_eq!(d, 60.01007098096119);
/// ```
pub fn distance_with(color1: &Color, color2: &Color, color_space: ColorSpace) -> f64 {
let vec1 = color1.vec_of(color_space.clone());
Expand Down Expand Up @@ -63,6 +63,6 @@ mod tests {
assert_eq!(d, 241.00414934187336);

let d = distance_with(&color1, &color2, ColorSpace::HSL);
assert_eq!(d, 60.01000749874974);
assert_eq!(d, 60.01007098096119);
}
}
4 changes: 2 additions & 2 deletions src/color_ops/darken.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Color {
///
/// let mut color = Color::from_str("#426105").unwrap();
/// color.darken(0.1).unwrap();
/// assert_eq!(color.hex(), "#213003");
/// assert_eq!(color.hex(), "#213102");
/// ```
pub fn darken(&mut self, amount: f64) -> Result<Self> {
if amount.abs() > 1.0 {
Expand Down Expand Up @@ -56,7 +56,7 @@ mod tests {
fn test_darken() {
let mut color = Color::from_str("#426105").unwrap();
color.darken(0.1).unwrap();
assert_eq!(color.hex(), "#213003");
assert_eq!(color.hex(), "#213102");

let mut color = Color::from_str("#426105").unwrap();
color.darken(0.5).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/conversion/hsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,5 @@ pub fn rgb2hsl(color: (f64, f64, f64)) -> (f64, f64, f64) {
s = delta / (1.0 - (2.0 * l - 1.0).abs());
}

(round(h, 2), round(s, 2), round(l, 2))
(round(h, 4), round(s, 4), round(l, 4))
}
2 changes: 1 addition & 1 deletion tests/color_calc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ fn test_color_calc() {
assert_eq!(d, 265.00377355803823);

let d = distance_with(&color1, &color2, ColorSpace::HSL);
assert_eq!(d, 0.6007495318350237);
assert_eq!(d, 0.6005717609078869);
}
2 changes: 1 addition & 1 deletion tests/color_channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn test_color_channel() {

assert_eq!(color.hue(), 210.0);
assert_eq!(color.saturation(), 0.68);
assert_eq!(color.lightness(), 0.8);
assert_eq!(color.lightness(), 0.8039);

assert_eq!(color.hsv_hue(), 210.0);
assert_eq!(color.hsv_saturation(), 0.28);
Expand Down

0 comments on commit 34bef2f

Please sign in to comment.