diff --git a/palette/src/rgb/mod.rs b/palette/src/rgb/mod.rs index 5fe5832f9..dd58d5e77 100644 --- a/palette/src/rgb/mod.rs +++ b/palette/src/rgb/mod.rs @@ -1,5 +1,6 @@ //!RGB types, spaces and standards. +use alpha::Alpha; use float::Float; use core::any::Any; @@ -70,3 +71,75 @@ pub trait Primaries: Any { ///Primary blue. fn blue() -> Yxy; } + +impl From> for Srgb +where + T: Component + Float, + U: Component, +{ + fn from(lin_srgb: LinSrgb) -> Self { + let non_lin = Srgb::::from_linear(lin_srgb); + non_lin.into_format() + } +} + +impl From> for LinSrgb +where + T: Component + Float, + U: Component, +{ + fn from(srgb: Srgb) -> Self { + srgb.into_linear() + .into_format() + } +} + +impl From> for Srgba +where + T: Component + Float, + U: Component, +{ + fn from(lin_srgb: LinSrgb) -> Self { + let color = lin_srgb.into(); + let alpha = U::max_intensity(); + Alpha { color, alpha } + } +} + +impl From> for Srgba +where + T: Component + Float, + U: Component, +{ + fn from(lin_srgba: LinSrgba) -> Self { + let (r, g, b, a) = lin_srgba.into(); + let color = LinSrgb::new(r, g, b).into(); + let alpha = a.convert(); + Alpha { color, alpha } + } +} + +impl From> for LinSrgba +where + T: Component + Float, + U: Component, +{ + fn from(srgb: Srgb) -> Self { + let color = srgb.into(); + let alpha = U::max_intensity(); + Alpha { color, alpha } + } +} + +impl From> for LinSrgba +where + T: Component + Float, + U: Component, +{ + fn from(srgba: Srgba) -> Self { + let (r, g, b, a) = srgba.into(); + let color = Srgb::new(r, g, b).into(); + let alpha = a.convert(); + Alpha { color, alpha } + } +}