From 62fddce2e676123b1325a16d144a1d0674dcd1ff Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Fri, 21 Feb 2020 15:22:54 -0600 Subject: Add check_rgba fn to clamp float values to [0,1] --- core/src/color.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index db509b88..799d85c9 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -2,12 +2,17 @@ #[derive(Debug, Clone, Copy, PartialEq)] #[allow(missing_docs)] pub struct Color { + /// Red component, 0.0 - 1.0 pub r: f32, + /// Green component, 0.0 - 1.0 pub g: f32, + /// Blue component, 0.0 - 1.0 pub b: f32, + /// Transparency, 0.0 - 1.0 pub a: f32, } + impl Color { /// The black color. pub const BLACK: Color = Color { @@ -33,11 +38,26 @@ impl Color { a: 0.0, }; + /// Calmps a float value to the range [0.0, 1.0] + pub fn clamp(v: f32) -> f32 { + v.max(0.0f32).min(1.0f32) + } + + /// Ensures RGBA values on the range [0.0, 1.0] + pub fn check_rgba(r: f32, g: f32, b: f32, a:f32) -> Color { + Color { + r: Color::clamp(r), + g: Color::clamp(g), + b: Color::clamp(b), + a: Color::clamp(a), + } + } + /// Creates a [`Color`] from its RGB components. /// /// [`Color`]: struct.Color.html - pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color { - Color { r, g, b, a: 1.0 } + pub fn from_rgb(r: f32, g: f32, b: f32) -> Color { + Color::check_rgba(r, g, b, 1.0) } /// Creates a [`Color`] from its RGB8 components. @@ -55,7 +75,7 @@ impl Color { r: f32::from(r) / 255.0, g: f32::from(g) / 255.0, b: f32::from(b) / 255.0, - a, + a: Color::clamp(a), } } -- cgit From 27a4cbccea91c508b914f2211a07aec2a4bed96e Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Fri, 21 Feb 2020 15:40:37 -0600 Subject: Add inversion functions, rename check_rgba -> new --- core/src/color.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index 799d85c9..fbc160e3 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -1,6 +1,5 @@ /// A color in the sRGB color space. #[derive(Debug, Clone, Copy, PartialEq)] -#[allow(missing_docs)] pub struct Color { /// Red component, 0.0 - 1.0 pub r: f32, @@ -12,7 +11,6 @@ pub struct Color { pub a: f32, } - impl Color { /// The black color. pub const BLACK: Color = Color { @@ -44,7 +42,7 @@ impl Color { } /// Ensures RGBA values on the range [0.0, 1.0] - pub fn check_rgba(r: f32, g: f32, b: f32, a:f32) -> Color { + pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color { Color { r: Color::clamp(r), g: Color::clamp(g), @@ -57,7 +55,7 @@ impl Color { /// /// [`Color`]: struct.Color.html pub fn from_rgb(r: f32, g: f32, b: f32) -> Color { - Color::check_rgba(r, g, b, 1.0) + Color::new(r, g, b, 1.0) } /// Creates a [`Color`] from its RGB8 components. @@ -100,6 +98,18 @@ impl Color { self.a, ] } + + /// Invert the Color in-place + pub fn invert(&mut self) { + self.r = Color::clamp(1.0f32 - self.r); + self.b = Color::clamp(1.0f32 - self.g); + self.g = Color::clamp(1.0f32 - self.b); + } + + /// Return an inverted Color + pub fn inverse(self) -> Color { + Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a) + } } impl From<[f32; 3]> for Color { -- cgit From 0ff3cbf543e84b1e4d1965efe3d6cce3b1fb5900 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Fri, 21 Feb 2020 16:42:12 -0600 Subject: HSLColor struct, with conversions to/from RGB --- core/src/color.rs | 122 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 106 insertions(+), 16 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index fbc160e3..a46f44ee 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -36,18 +36,13 @@ impl Color { a: 0.0, }; - /// Calmps a float value to the range [0.0, 1.0] - pub fn clamp(v: f32) -> f32 { - v.max(0.0f32).min(1.0f32) - } - - /// Ensures RGBA values on the range [0.0, 1.0] + /// New Color with range checks pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color { Color { - r: Color::clamp(r), - g: Color::clamp(g), - b: Color::clamp(b), - a: Color::clamp(a), + r: clamp(r), + g: clamp(g), + b: clamp(b), + a: clamp(a), } } @@ -73,7 +68,7 @@ impl Color { r: f32::from(r) / 255.0, g: f32::from(g) / 255.0, b: f32::from(b) / 255.0, - a: Color::clamp(a), + a: clamp(a), } } @@ -101,9 +96,9 @@ impl Color { /// Invert the Color in-place pub fn invert(&mut self) { - self.r = Color::clamp(1.0f32 - self.r); - self.b = Color::clamp(1.0f32 - self.g); - self.g = Color::clamp(1.0f32 - self.b); + self.r = clamp(1.0f32 - self.r); + self.b = clamp(1.0f32 - self.g); + self.g = clamp(1.0f32 - self.b); } /// Return an inverted Color @@ -114,12 +109,107 @@ impl Color { impl From<[f32; 3]> for Color { fn from([r, g, b]: [f32; 3]) -> Self { - Color { r, g, b, a: 1.0 } + Color::new(r, g, b, 1.0) } } impl From<[f32; 4]> for Color { fn from([r, g, b, a]: [f32; 4]) -> Self { - Color { r, g, b, a } + Color::new(r, g, b, a) + } +} + +impl From for Color { + fn from(hsl: HSLColor) -> Self { + // Compute Chroma + let ch = (1.0 - (2.0 * hsl.l - 1.0).abs()) * hsl.s; + + // Quantized Hue: H' + let hp: u8 = (hsl.h / 60.0).ceil() as u8; + let x: f32 = ch * f32::from(1 - ((hp % 2) - 1)); + + // Intermediate RGB values + let (r1, g1, b1): (f32, f32, f32) = match hp { + 1 => (ch, x, 0.0), + 2 => (x, ch, 0.0), + 3 => (0.0, ch, x), + 4 => (0.0, x, ch), + 5 => (x, 0.0, ch), + 6 => (ch, 0.0, x), + _ => (0.0, 0.0, 0.0), + }; + + // Match lightness + let m = hsl.l - ch / 2.0; + + Color::new(r1 + m, g1 + m, b1 + m, hsl.a) + } +} + +/// A color in the HSL color space. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct HSLColor { + /// Hue, 0.0 - 360.0 + pub h: f32, + /// Saturation, 0.0 - 1.0 + pub s: f32, + /// Lightness, 0.0 - 1.0 + pub l: f32, + /// Transparency, 0.0 - 1.0 + pub a: f32, +} + +impl HSLColor { + /// New HSLColor with range checks + pub fn new(h: f32, s: f32, l: f32, a: f32) -> HSLColor { + HSLColor { + h: clamp_hue(h), + s: clamp(s), + l: clamp(l), + a: clamp(a), + } + } +} + +impl From for HSLColor { + fn from(c: Color) -> Self { + // https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB + + // Maximum of the RGB: color Value (for HSV) + let v: f32 = c.r.max(c.g).max(c.b); + // Minimum of the RGB values + let m: f32 = c.r.min(c.g).min(c.b); + // Chroma + let ch: f32 = v - m; + // Lightness + let l: f32 = (v + m) / 2.0; + + // Determine Hue + let mut h = 0.0f32; + if c.r >= c.g && c.r >= c.b { + h = 60.0 * (c.g - c.b) / ch; + } else if c.g >= c.r && c.g >= c.b { + h = 60.0 * (2.0 + (c.b - c.r) / ch); + } else if c.b >= c.r && c.b >= c.g { + h = 60.0 * (4.0 + (c.r - c.g) / ch); + } + + // Determine saturation + let mut s = 0.0f32; + if l > 0.0 && l < 1.0 { + s = (v - l) / l.min(1.0 - l); + } + + HSLColor::new(h, s, l, c.a) } } + +/// Calmps a float value to the range [0.0, 1.0] +pub fn clamp(v: f32) -> f32 { + v.max(0.0f32).min(1.0f32) +} + +/// Calmps a float value to the range [0.0, 360.0] +pub fn clamp_hue(v: f32) -> f32 { + v.max(0.0f32).min(360.0f32) +} -- cgit From 63933e26d2ffd530fc1d8c9a7d7b94927c0e8cc8 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 12:11:37 -0500 Subject: Add `palette` dependency behind "colors" feature flag --- core/Cargo.toml | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'core') diff --git a/core/Cargo.toml b/core/Cargo.toml index 837f6aae..e05f824c 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -7,4 +7,11 @@ description = "The essential concepts of Iced" license = "MIT" repository = "https://github.com/hecrj/iced" +[features] +colors = ["palette"] + [dependencies] + +[dependencies.palette] +version = "0.5.0" +optional = true -- cgit From 831a07f720d522954a75b159ccc00824f3affee6 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 15:20:47 -0500 Subject: Conversion to palette's Srgba type --- core/src/color.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index a46f44ee..ce0ea5ed 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -1,3 +1,6 @@ +#[cfg(feature = "colors")] +use palette::rgb::Srgba; + /// A color in the sRGB color space. #[derive(Debug, Clone, Copy, PartialEq)] pub struct Color { @@ -94,6 +97,24 @@ impl Color { ] } + #[cfg(feature = "colors")] + /// Convert from palette's [`Srgba`] type to a [`Color`] + /// + /// [`Srgba`]: ../palette/rgb/type.Srgba.html + /// [`Color`]: struct.Color.html + pub fn from_srgba(srgba: Srgba) -> Color { + Color::new(srgba.red, srgba.green, srgba.blue, srgba.alpha) + } + + #[cfg(feature = "colors")] + /// Convert from [`Color`] to palette's [`Srgba`] type + /// + /// [`Color`]: struct.Color.html + /// [`Srgba`]: ../palette/rgb/type.Srgba.html + pub fn into_srgba(self) -> Srgba { + Srgba::new(self.r, self.g, self.b, self.a) + } + /// Invert the Color in-place pub fn invert(&mut self) { self.r = clamp(1.0f32 - self.r); @@ -119,6 +140,28 @@ impl From<[f32; 4]> for Color { } } +#[cfg(feature = "colors")] +/// Convert from palette's [`Srgba`] type to a [`Color`] +/// +/// [`Srgba`]: ../palette/rgb/type.Srgba.html +/// [`Color`]: struct.Color.html +impl From for Color { + fn from(srgba: Srgba) -> Self { + Color::new(srgba.red, srgba.green, srgba.blue, srgba.alpha) + } +} + +#[cfg(feature = "colors")] +/// Convert from [`Color`] to palette's [`Srgba`] type +/// +/// [`Color`]: struct.Color.html +/// [`Srgba`]: ../palette/rgb/type.Srgba.html +impl From for Srgba { + fn from(c: Color) -> Self { + Srgba::new(c.r, c.g, c.b, c.a) + } +} + impl From for Color { fn from(hsl: HSLColor) -> Self { // Compute Chroma -- cgit From 4009f0cf73cdf261a3218a44706fdd1434653c8f Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 15:31:44 -0500 Subject: Remove HSLColor --- core/src/color.rs | 90 ------------------------------------------------------- 1 file changed, 90 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index ce0ea5ed..33eeedaf 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -162,97 +162,7 @@ impl From for Srgba { } } -impl From for Color { - fn from(hsl: HSLColor) -> Self { - // Compute Chroma - let ch = (1.0 - (2.0 * hsl.l - 1.0).abs()) * hsl.s; - - // Quantized Hue: H' - let hp: u8 = (hsl.h / 60.0).ceil() as u8; - let x: f32 = ch * f32::from(1 - ((hp % 2) - 1)); - - // Intermediate RGB values - let (r1, g1, b1): (f32, f32, f32) = match hp { - 1 => (ch, x, 0.0), - 2 => (x, ch, 0.0), - 3 => (0.0, ch, x), - 4 => (0.0, x, ch), - 5 => (x, 0.0, ch), - 6 => (ch, 0.0, x), - _ => (0.0, 0.0, 0.0), - }; - - // Match lightness - let m = hsl.l - ch / 2.0; - - Color::new(r1 + m, g1 + m, b1 + m, hsl.a) - } -} - -/// A color in the HSL color space. -#[derive(Debug, Clone, Copy, PartialEq)] -pub struct HSLColor { - /// Hue, 0.0 - 360.0 - pub h: f32, - /// Saturation, 0.0 - 1.0 - pub s: f32, - /// Lightness, 0.0 - 1.0 - pub l: f32, - /// Transparency, 0.0 - 1.0 - pub a: f32, -} - -impl HSLColor { - /// New HSLColor with range checks - pub fn new(h: f32, s: f32, l: f32, a: f32) -> HSLColor { - HSLColor { - h: clamp_hue(h), - s: clamp(s), - l: clamp(l), - a: clamp(a), - } - } -} - -impl From for HSLColor { - fn from(c: Color) -> Self { - // https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB - - // Maximum of the RGB: color Value (for HSV) - let v: f32 = c.r.max(c.g).max(c.b); - // Minimum of the RGB values - let m: f32 = c.r.min(c.g).min(c.b); - // Chroma - let ch: f32 = v - m; - // Lightness - let l: f32 = (v + m) / 2.0; - - // Determine Hue - let mut h = 0.0f32; - if c.r >= c.g && c.r >= c.b { - h = 60.0 * (c.g - c.b) / ch; - } else if c.g >= c.r && c.g >= c.b { - h = 60.0 * (2.0 + (c.b - c.r) / ch); - } else if c.b >= c.r && c.b >= c.g { - h = 60.0 * (4.0 + (c.r - c.g) / ch); - } - - // Determine saturation - let mut s = 0.0f32; - if l > 0.0 && l < 1.0 { - s = (v - l) / l.min(1.0 - l); - } - - HSLColor::new(h, s, l, c.a) - } -} - /// Calmps a float value to the range [0.0, 1.0] pub fn clamp(v: f32) -> f32 { v.max(0.0f32).min(1.0f32) } - -/// Calmps a float value to the range [0.0, 360.0] -pub fn clamp_hue(v: f32) -> f32 { - v.max(0.0f32).min(360.0f32) -} -- cgit From bb443988197ecfbb1effe9ae73ec5533db7e3339 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 16:09:06 -0500 Subject: Revert from_rgb to const --- core/src/color.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index 33eeedaf..4ac2f8a7 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -52,8 +52,8 @@ impl Color { /// Creates a [`Color`] from its RGB components. /// /// [`Color`]: struct.Color.html - pub fn from_rgb(r: f32, g: f32, b: f32) -> Color { - Color::new(r, g, b, 1.0) + pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color { + Color { r, g, b, a: 1.0 } } /// Creates a [`Color`] from its RGB8 components. -- cgit From fd484c76381cd77fdd485939f6435df115f1ca65 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 16:12:18 -0500 Subject: Fix docstring typo --- core/src/color.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index 4ac2f8a7..8a0a26ba 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -162,7 +162,7 @@ impl From for Srgba { } } -/// Calmps a float value to the range [0.0, 1.0] +/// Clamps a float value to the range [0.0, 1.0] pub fn clamp(v: f32) -> f32 { v.max(0.0f32).min(1.0f32) } -- cgit From 9a4ad3d6a7de5305cc992b140ca208a4277f75ea Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 16:40:44 -0500 Subject: Use debug assertions instead of clamp --- core/src/color.rs | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index 8a0a26ba..b7445a8c 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -41,12 +41,24 @@ impl Color { /// New Color with range checks pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color { - Color { - r: clamp(r), - g: clamp(g), - b: clamp(b), - a: clamp(a), - } + debug_assert!( + (0.0f32..=1.0f32).contains(&r), + "Red component must be on [0, 1]" + ); + debug_assert!( + (0.0f32..=1.0f32).contains(&g), + "Green component must be on [0, 1]" + ); + debug_assert!( + (0.0f32..=1.0f32).contains(&b), + "Blue component must be on [0, 1]" + ); + debug_assert!( + (0.0f32..=1.0f32).contains(&a), + "Alpha component must be on [0, 1]" + ); + + Color { r, g, b, a } } /// Creates a [`Color`] from its RGB components. @@ -71,7 +83,7 @@ impl Color { r: f32::from(r) / 255.0, g: f32::from(g) / 255.0, b: f32::from(b) / 255.0, - a: clamp(a), + a, } } @@ -117,9 +129,9 @@ impl Color { /// Invert the Color in-place pub fn invert(&mut self) { - self.r = clamp(1.0f32 - self.r); - self.b = clamp(1.0f32 - self.g); - self.g = clamp(1.0f32 - self.b); + self.r = 1.0f32 - self.r; + self.b = 1.0f32 - self.g; + self.g = 1.0f32 - self.b; } /// Return an inverted Color @@ -161,8 +173,3 @@ impl From for Srgba { Srgba::new(c.r, c.g, c.b, c.a) } } - -/// Clamps a float value to the range [0.0, 1.0] -pub fn clamp(v: f32) -> f32 { - v.max(0.0f32).min(1.0f32) -} -- cgit From e926e4374242cc590ac507b059e3ce0cfa97d52f Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 16:41:07 -0500 Subject: Add const from_rgba, for RGBA initialization --- core/src/color.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index b7445a8c..4f0d974b 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -65,7 +65,14 @@ impl Color { /// /// [`Color`]: struct.Color.html pub const fn from_rgb(r: f32, g: f32, b: f32) -> Color { - Color { r, g, b, a: 1.0 } + Color::from_rgba(r, g, b, 1.0f32) + } + + /// Creates a [`Color`] from its RGBA components. + /// + /// [`Color`]: struct.Color.html + pub const fn from_rgba(r: f32, g: f32, b: f32, a: f32) -> Color { + Color { r, g, b, a } } /// Creates a [`Color`] from its RGB8 components. -- cgit From 7b15e4b0e29c64d53a00fbe6709ff2069630fec5 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 16:44:02 -0500 Subject: Feature name colors -> palette --- core/Cargo.toml | 3 --- core/src/color.rs | 10 +++++----- 2 files changed, 5 insertions(+), 8 deletions(-) (limited to 'core') diff --git a/core/Cargo.toml b/core/Cargo.toml index e05f824c..b52bf315 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -7,9 +7,6 @@ description = "The essential concepts of Iced" license = "MIT" repository = "https://github.com/hecrj/iced" -[features] -colors = ["palette"] - [dependencies] [dependencies.palette] diff --git a/core/src/color.rs b/core/src/color.rs index 4f0d974b..be1a2870 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -1,4 +1,4 @@ -#[cfg(feature = "colors")] +#[cfg(feature = "palette")] use palette::rgb::Srgba; /// A color in the sRGB color space. @@ -116,7 +116,7 @@ impl Color { ] } - #[cfg(feature = "colors")] + #[cfg(feature = "palette")] /// Convert from palette's [`Srgba`] type to a [`Color`] /// /// [`Srgba`]: ../palette/rgb/type.Srgba.html @@ -125,7 +125,7 @@ impl Color { Color::new(srgba.red, srgba.green, srgba.blue, srgba.alpha) } - #[cfg(feature = "colors")] + #[cfg(feature = "palette")] /// Convert from [`Color`] to palette's [`Srgba`] type /// /// [`Color`]: struct.Color.html @@ -159,7 +159,7 @@ impl From<[f32; 4]> for Color { } } -#[cfg(feature = "colors")] +#[cfg(feature = "palette")] /// Convert from palette's [`Srgba`] type to a [`Color`] /// /// [`Srgba`]: ../palette/rgb/type.Srgba.html @@ -170,7 +170,7 @@ impl From for Color { } } -#[cfg(feature = "colors")] +#[cfg(feature = "palette")] /// Convert from [`Color`] to palette's [`Srgba`] type /// /// [`Color`]: struct.Color.html -- cgit From 408e9e566f740a4a9eb564492e96714dd5db4cc3 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Tue, 31 Mar 2020 17:36:09 -0500 Subject: Add palette test for Color <-> Srgba & manipulation --- core/src/color.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index be1a2870..67433ded 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -180,3 +180,44 @@ impl From for Srgba { Srgba::new(c.r, c.g, c.b, c.a) } } + +#[cfg(feature = "palette")] +#[cfg(test)] +mod tests { + use super::*; + use palette::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(); + let r: Color = s.into(); + assert_eq!(c, r); + } + + #[test] + fn color_manipulation() { + let c1 = Color::from_rgb(0.5, 0.4, 0.3); + let c2 = Color::from_rgb(0.2, 0.5, 0.3); + + // Convert to linear color for manipulation + let l1 = c1.into_srgba().into_linear(); + let l2 = c2.into_srgba().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(); + assert_eq!( + r, + Color { + r: 0.5, + g: 0.5, + b: 0.3, + a: 1.0 + } + ); + } +} -- cgit From a95d494f707b9492d180b41bd93565b21c729dd8 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Wed, 1 Apr 2020 16:15:29 -0500 Subject: Remove redundant from_srgba and into_srgba methods --- core/src/color.rs | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index 67433ded..57765df0 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -116,24 +116,6 @@ impl Color { ] } - #[cfg(feature = "palette")] - /// Convert from palette's [`Srgba`] type to a [`Color`] - /// - /// [`Srgba`]: ../palette/rgb/type.Srgba.html - /// [`Color`]: struct.Color.html - pub fn from_srgba(srgba: Srgba) -> Color { - Color::new(srgba.red, srgba.green, srgba.blue, srgba.alpha) - } - - #[cfg(feature = "palette")] - /// Convert from [`Color`] to palette's [`Srgba`] type - /// - /// [`Color`]: struct.Color.html - /// [`Srgba`]: ../palette/rgb/type.Srgba.html - pub fn into_srgba(self) -> Srgba { - Srgba::new(self.r, self.g, self.b, self.a) - } - /// Invert the Color in-place pub fn invert(&mut self) { self.r = 1.0f32 - self.r; @@ -202,8 +184,8 @@ mod tests { let c2 = Color::from_rgb(0.2, 0.5, 0.3); // Convert to linear color for manipulation - let l1 = c1.into_srgba().into_linear(); - let l2 = c2.into_srgba().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 let lighter = l1.lighten(l2); -- cgit From 56ce01e262832b78530b0721f735a95919651d91 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Wed, 1 Apr 2020 16:17:46 -0500 Subject: Simplify range declaration --- core/src/color.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index 57765df0..eff14948 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -42,19 +42,19 @@ impl Color { /// New Color with range checks pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color { debug_assert!( - (0.0f32..=1.0f32).contains(&r), + (0.0..=1.0).contains(&r), "Red component must be on [0, 1]" ); debug_assert!( - (0.0f32..=1.0f32).contains(&g), + (0.0..=1.0).contains(&g), "Green component must be on [0, 1]" ); debug_assert!( - (0.0f32..=1.0f32).contains(&b), + (0.0..=1.0).contains(&b), "Blue component must be on [0, 1]" ); debug_assert!( - (0.0f32..=1.0f32).contains(&a), + (0.0..=1.0).contains(&a), "Alpha component must be on [0, 1]" ); -- cgit From ea3b7b528275c7ae8a336004ad77f85341599335 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Thu, 2 Apr 2020 15:24:40 -0500 Subject: Derive Default for Color --- core/src/color.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index eff14948..56d5455f 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -2,7 +2,7 @@ use palette::rgb::Srgba; /// A color in the sRGB color space. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, Copy, PartialEq, Default)] pub struct Color { /// Red component, 0.0 - 1.0 pub r: f32, -- cgit From 04be010fbdf84300531b806fa8855f57bbf727b7 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Thu, 2 Apr 2020 17:29:26 -0500 Subject: Conversion traits for palette::Srgb --- core/src/color.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index 56d5455f..c061add6 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -1,5 +1,5 @@ #[cfg(feature = "palette")] -use palette::rgb::Srgba; +use palette::rgb::{Srgb, Srgba}; /// A color in the sRGB color space. #[derive(Debug, Clone, Copy, PartialEq, Default)] @@ -163,6 +163,28 @@ impl From for Srgba { } } +#[cfg(feature = "palette")] +/// Convert from palette's [`Srgb`] type to a [`Color`] +/// +/// [`Srgb`]: ../palette/rgb/type.Srgb.html +/// [`Color`]: struct.Color.html +impl From for Color { + fn from(srgb: Srgb) -> Self { + Color::new(srgb.red, srgb.green, srgb.blue, 1.0) + } +} + +#[cfg(feature = "palette")] +/// Convert from [`Color`] to palette's [`Srgb`] type +/// +/// [`Color`]: struct.Color.html +/// [`Srgb`]: ../palette/rgb/type.Srgb.html +impl From for Srgb { + fn from(c: Color) -> Self { + Srgb::new(c.r, c.g, c.b) + } +} + #[cfg(feature = "palette")] #[cfg(test)] mod tests { -- cgit From 71657b50dd69d860663051c588ff643242e971a7 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Thu, 2 Apr 2020 17:49:23 -0500 Subject: Conditional re-export of palette from iced_core --- core/src/lib.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'core') diff --git a/core/src/lib.rs b/core/src/lib.rs index c2887a0b..ca6013da 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -35,3 +35,6 @@ pub use point::Point; pub use rectangle::Rectangle; pub use size::Size; pub use vector::Vector; + +#[cfg(feature = "palette")] +pub use palette; -- cgit From 27fadad3246d555f52b991230a0352353d6700b4 Mon Sep 17 00:00:00 2001 From: Clark Moody Date: Fri, 24 Apr 2020 15:20:00 -0500 Subject: Do not re-export Palette from iced_core --- core/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) (limited to 'core') diff --git a/core/src/lib.rs b/core/src/lib.rs index ca6013da..c2887a0b 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -35,6 +35,3 @@ pub use point::Point; pub use rectangle::Rectangle; pub use size::Size; pub use vector::Vector; - -#[cfg(feature = "palette")] -pub use palette; -- cgit From c0fd5de8a0dbb1b99de8c83e4f84c98a6219778b Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 4 May 2020 23:04:02 +0200 Subject: Improve minor documentation details in `Color` --- core/src/color.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/src/color.rs b/core/src/color.rs index c061add6..a4c3d87c 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -39,7 +39,12 @@ impl Color { a: 0.0, }; - /// New Color with range checks + /// Creates a new [`Color`]. + /// + /// In debug mode, it will panic if the values are not in the correct + /// range: 0.0 - 1.0 + /// + /// [`Color`]: struct.Color.html pub fn new(r: f32, g: f32, b: f32, a: f32) -> Color { debug_assert!( (0.0..=1.0).contains(&r), @@ -116,14 +121,18 @@ impl Color { ] } - /// Invert the Color in-place + /// Inverts the [`Color`] in-place. + /// + /// [`Color`]: struct.Color.html pub fn invert(&mut self) { self.r = 1.0f32 - self.r; self.b = 1.0f32 - self.g; self.g = 1.0f32 - self.b; } - /// Return an inverted Color + /// Returns the inverted [`Color`]. + /// + /// [`Color`]: struct.Color.html pub fn inverse(self) -> Color { Color::new(1.0f32 - self.r, 1.0f32 - self.g, 1.0f32 - self.b, self.a) } @@ -142,9 +151,8 @@ impl From<[f32; 4]> for Color { } #[cfg(feature = "palette")] -/// Convert from palette's [`Srgba`] type to a [`Color`] +/// Converts from palette's `Srgba` type to a [`Color`]. /// -/// [`Srgba`]: ../palette/rgb/type.Srgba.html /// [`Color`]: struct.Color.html impl From for Color { fn from(srgba: Srgba) -> Self { @@ -153,10 +161,9 @@ impl From for Color { } #[cfg(feature = "palette")] -/// Convert from [`Color`] to palette's [`Srgba`] type +/// Converts from [`Color`] to palette's `Srgba` type. /// /// [`Color`]: struct.Color.html -/// [`Srgba`]: ../palette/rgb/type.Srgba.html impl From for Srgba { fn from(c: Color) -> Self { Srgba::new(c.r, c.g, c.b, c.a) @@ -164,9 +171,8 @@ impl From for Srgba { } #[cfg(feature = "palette")] -/// Convert from palette's [`Srgb`] type to a [`Color`] +/// Converts from palette's `Srgb` type to a [`Color`]. /// -/// [`Srgb`]: ../palette/rgb/type.Srgb.html /// [`Color`]: struct.Color.html impl From for Color { fn from(srgb: Srgb) -> Self { @@ -175,7 +181,7 @@ impl From for Color { } #[cfg(feature = "palette")] -/// Convert from [`Color`] to palette's [`Srgb`] type +/// Converts from [`Color`] to palette's `Srgb` type. /// /// [`Color`]: struct.Color.html /// [`Srgb`]: ../palette/rgb/type.Srgb.html -- cgit