Conversion to palette's Srgba type

This commit is contained in:
Clark Moody 2020-03-31 15:20:47 -05:00
parent 63933e26d2
commit 831a07f720

View File

@ -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