From 2d21d0900e9fcabfc01a7deaaab5b4fd4b8573e8 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Thu, 25 May 2023 13:59:58 -0500 Subject: Upgrade `palette` dependency --- core/src/color.rs | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) (limited to 'core/src') diff --git a/core/src/color.rs b/core/src/color.rs index fe0a1856..f5423871 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -1,5 +1,5 @@ #[cfg(feature = "palette")] -use palette::rgb::{Srgb, Srgba}; +use palette::rgb::{Rgb, Rgba}; /// A color in the sRGB color space. #[derive(Debug, Clone, Copy, PartialEq, Default)] @@ -183,34 +183,34 @@ macro_rules! color { } #[cfg(feature = "palette")] -/// Converts from palette's `Srgba` type to a [`Color`]. -impl From for Color { - fn from(srgba: Srgba) -> Self { - Color::new(srgba.red, srgba.green, srgba.blue, srgba.alpha) +/// Converts from palette's `Rgba` type to a [`Color`]. +impl From for Color { + fn from(rgba: Rgba) -> Self { + Color::new(rgba.red, rgba.green, rgba.blue, rgba.alpha) } } #[cfg(feature = "palette")] -/// Converts from [`Color`] to palette's `Srgba` type. -impl From for Srgba { +/// Converts from [`Color`] to palette's `Rgba` type. +impl From for Rgba { fn from(c: Color) -> Self { - Srgba::new(c.r, c.g, c.b, c.a) + Rgba::new(c.r, c.g, c.b, c.a) } } #[cfg(feature = "palette")] -/// Converts from palette's `Srgb` type to a [`Color`]. -impl From for Color { - fn from(srgb: Srgb) -> Self { - Color::new(srgb.red, srgb.green, srgb.blue, 1.0) +/// Converts from palette's `Rgb` type to a [`Color`]. +impl From for Color { + fn from(rgb: Rgb) -> Self { + Color::new(rgb.red, rgb.green, rgb.blue, 1.0) } } #[cfg(feature = "palette")] -/// Converts from [`Color`] to palette's `Srgb` type. -impl From for Srgb { +/// Converts from [`Color`] to palette's `Rgb` type. +impl From for Rgb { fn from(c: Color) -> Self { - Srgb::new(c.r, c.g, c.b) + Rgb::new(c.r, c.g, c.b) } } @@ -218,13 +218,13 @@ impl From for Srgb { #[cfg(test)] mod tests { use super::*; - use palette::Blend; + use palette::blend::Blend; #[test] fn srgba_traits() { let c = Color::from_rgb(0.5, 0.4, 0.3); - // Round-trip conversion to the palette:Srgba type - let s: Srgba = c.into(); + // Round-trip conversion to the palette:Rgba type + let s: Rgba = c.into(); let r: Color = s.into(); assert_eq!(c, r); } @@ -235,14 +235,14 @@ mod tests { let c2 = Color::from_rgb(0.2, 0.5, 0.3); // Convert to linear color for manipulation - let l1 = Srgba::from(c1).into_linear(); - let l2 = Srgba::from(c2).into_linear(); + let l1 = Rgba::from(c1).into_linear(); + let l2 = Rgba::from(c2).into_linear(); // Take the lighter of each of the RGB components let lighter = l1.lighten(l2); // Convert back to our Color - let r: Color = Srgba::from_linear(lighter).into(); + let r: Color = Rgba::from_linear(lighter).into(); assert_eq!( r, Color { -- cgit From b741893013eda90045e5a3950c37ba21d073b351 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 26 May 2023 00:03:34 +0200 Subject: Use `Srgb` and `Srgba` from `palette` directly --- core/src/color.rs | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'core/src') diff --git a/core/src/color.rs b/core/src/color.rs index f5423871..88115f25 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -1,5 +1,5 @@ #[cfg(feature = "palette")] -use palette::rgb::{Rgb, Rgba}; +use palette::rgb::{Srgb, Srgba}; /// A color in the sRGB color space. #[derive(Debug, Clone, Copy, PartialEq, Default)] @@ -184,33 +184,33 @@ macro_rules! color { #[cfg(feature = "palette")] /// Converts from palette's `Rgba` type to a [`Color`]. -impl From for Color { - fn from(rgba: Rgba) -> Self { +impl From for Color { + fn from(rgba: Srgba) -> Self { Color::new(rgba.red, rgba.green, rgba.blue, rgba.alpha) } } #[cfg(feature = "palette")] /// Converts from [`Color`] to palette's `Rgba` type. -impl From for Rgba { +impl From for Srgba { fn from(c: Color) -> Self { - Rgba::new(c.r, c.g, c.b, c.a) + Srgba::new(c.r, c.g, c.b, c.a) } } #[cfg(feature = "palette")] /// Converts from palette's `Rgb` type to a [`Color`]. -impl From for Color { - fn from(rgb: Rgb) -> Self { +impl From for Color { + fn from(rgb: Srgb) -> Self { Color::new(rgb.red, rgb.green, rgb.blue, 1.0) } } #[cfg(feature = "palette")] /// Converts from [`Color`] to palette's `Rgb` type. -impl From for Rgb { +impl From for Srgb { fn from(c: Color) -> Self { - Rgb::new(c.r, c.g, c.b) + Srgb::new(c.r, c.g, c.b) } } @@ -223,8 +223,8 @@ mod tests { #[test] fn srgba_traits() { let c = Color::from_rgb(0.5, 0.4, 0.3); - // Round-trip conversion to the palette:Rgba type - let s: Rgba = c.into(); + // Round-trip conversion to the palette::Srgba type + let s: Srgba = c.into(); let r: Color = s.into(); assert_eq!(c, r); } @@ -235,14 +235,15 @@ mod tests { let c2 = Color::from_rgb(0.2, 0.5, 0.3); // Convert to linear color for manipulation - let l1 = Rgba::from(c1).into_linear(); - let l2 = Rgba::from(c2).into_linear(); + let l1 = Srgba::from(c1).into_linear(); + let l2 = Srgba::from(c2).into_linear(); - // Take the lighter of each of the RGB components + // Take the lighter of each of the sRGB components let lighter = l1.lighten(l2); // Convert back to our Color - let r: Color = Rgba::from_linear(lighter).into(); + let r: Color = Srgba::from_linear(lighter).into(); + assert_eq!( r, Color { -- cgit From cf2c8f2037907d8d619348a70594bab28f166d64 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 26 May 2023 00:04:10 +0200 Subject: Use approx for testing color operations --- core/src/color.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) (limited to 'core/src') diff --git a/core/src/color.rs b/core/src/color.rs index 88115f25..1392f28b 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -231,6 +231,8 @@ mod tests { #[test] fn color_manipulation() { + use approx::assert_relative_eq; + let c1 = Color::from_rgb(0.5, 0.4, 0.3); let c2 = Color::from_rgb(0.2, 0.5, 0.3); @@ -242,16 +244,11 @@ mod tests { let lighter = l1.lighten(l2); // Convert back to our Color - let r: Color = Srgba::from_linear(lighter).into(); - - assert_eq!( - r, - Color { - r: 0.5, - g: 0.5, - b: 0.3, - a: 1.0 - } - ); + let result: Color = Srgba::from_linear(lighter).into(); + + assert_relative_eq!(result.r, 0.5); + assert_relative_eq!(result.g, 0.5); + assert_relative_eq!(result.b, 0.3); + assert_relative_eq!(result.a, 1.0); } } -- cgit