diff options
author | 2020-03-31 15:20:47 -0500 | |
---|---|---|
committer | 2020-04-24 15:13:22 -0500 | |
commit | 831a07f720d522954a75b159ccc00824f3affee6 (patch) | |
tree | 11b05a87f18fb604d8491adf752d7f8aadafd8ab /core | |
parent | 63933e26d2ffd530fc1d8c9a7d7b94927c0e8cc8 (diff) | |
download | iced-831a07f720d522954a75b159ccc00824f3affee6.tar.gz iced-831a07f720d522954a75b159ccc00824f3affee6.tar.bz2 iced-831a07f720d522954a75b159ccc00824f3affee6.zip |
Conversion to palette's Srgba type
Diffstat (limited to 'core')
-rw-r--r-- | core/src/color.rs | 43 |
1 files changed, 43 insertions, 0 deletions
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<Srgba> 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<Color> for Srgba { + fn from(c: Color) -> Self { + Srgba::new(c.r, c.g, c.b, c.a) + } +} + impl From<HSLColor> for Color { fn from(hsl: HSLColor) -> Self { // Compute Chroma |