diff --git a/README.md b/README.md index e8630ebb6..e90750e45 100644 --- a/README.md +++ b/README.md @@ -82,11 +82,11 @@ The following example shows how the `Color` type is used to make a lighter and a ```Rust extern crate palette; -use palette::{Color, Srgb, Shade, Saturate}; +use palette::{Saturate, Shade, Srgb, Lch}; -let color: Color = Srgb::new(0.8, 0.2, 0.1).into_linear().into(); +let color = Srgb::new(0.8, 0.2, 0.1).into_linear(); let lighter = color.lighten(0.1); -let desaturated = color.desaturate(0.5); +let desaturated = Lch::from(color).desaturate(0.5); ``` This results in the following three colors: diff --git a/palette/examples/color_scheme.rs b/palette/examples/color_scheme.rs index 9c8e14096..e2949c5e1 100644 --- a/palette/examples/color_scheme.rs +++ b/palette/examples/color_scheme.rs @@ -2,7 +2,7 @@ extern crate clap; extern crate image; extern crate palette; -use palette::{Color, Hue, Pixel, Shade, Srgb}; +use palette::{Hue, Pixel, Shade, Lch, Srgb, LinSrgb}; use image::{GenericImage, RgbImage, SubImage}; @@ -93,8 +93,8 @@ fn main() { .and_then(|r| r.parse().ok()) .expect("the blue channel must be a number in the range [0-255]"); - let primary: Color = Srgb::new(red, green, blue) - .into_format() + let primary: Lch = Srgb::new(red, green, blue) + .into_format::() .into_linear() .into(); @@ -154,12 +154,12 @@ fn main() { let mut image = RgbImage::new((secondary.len() as u32 + 1) * SWATCH_SIZE, SWATCH_SIZE); //Draw the primary swatches - blit_shades(primary, image.sub_image(0, 0, SWATCH_SIZE, SWATCH_SIZE)); + blit_shades(primary.into(), image.sub_image(0, 0, SWATCH_SIZE, SWATCH_SIZE)); //Draw the secondary swatches for (n, color) in secondary.into_iter().enumerate() { blit_shades( - color, + color.into(), image.sub_image((n as u32 + 1) * SWATCH_SIZE, 0, SWATCH_SIZE, SWATCH_SIZE), ); } @@ -170,14 +170,14 @@ fn main() { } } -fn blit_shades> + 'static>( - color: Color, +fn blit_shades( + color: LinSrgb, mut canvas: SubImage, -) { +) where I: GenericImage> + 'static { let width = canvas.width(); let height = canvas.height(); - let primary = Srgb::from_linear(color.into()).into_format().into_raw(); + let primary = Srgb::from_linear(color).into_format().into_raw(); //Generate one lighter and two darker versions of the color let light = Srgb::from_linear(color.lighten(0.1).into()) diff --git a/palette/examples/readme_examples.rs b/palette/examples/readme_examples.rs index 1f1400559..68f0c696e 100644 --- a/palette/examples/readme_examples.rs +++ b/palette/examples/readme_examples.rs @@ -27,13 +27,13 @@ mod color_spaces { } mod manipulation { - use palette::{Color, Saturate, Shade, Srgb}; + use palette::{Saturate, Shade, Srgb, Lch}; use display_colors; pub fn run() { - let color: Color = Srgb::new(0.8, 0.2, 0.1).into_linear().into(); + let color = Srgb::new(0.8, 0.2, 0.1).into_linear(); let lighter = color.lighten(0.1); - let desaturated = color.desaturate(0.5); + let desaturated = Lch::from(color).desaturate(0.5); display_colors( "examples/readme_manipulation.png", diff --git a/palette/src/blend/test.rs b/palette/src/blend/test.rs index 250f2027c..473d3ec15 100644 --- a/palette/src/blend/test.rs +++ b/palette/src/blend/test.rs @@ -1,12 +1,12 @@ -use {Blend, Color, Colora, ComponentWise, LinSrgb, LinSrgba}; +use {Blend, ComponentWise, LinSrgb, LinSrgba}; use rgb::Rgb; use encoding::Linear; use blend::PreAlpha; #[test] fn blend_color() { - let a = Color::linear_rgb(1.0, 0.0, 0.0); - let b = Color::linear_rgb(0.0, 0.0, 1.0); + let a = LinSrgb::new(1.0, 0.0, 0.0); + let b = LinSrgb::new(0.0, 0.0, 1.0); let c: LinSrgb = a.blend( b, @@ -19,8 +19,8 @@ fn blend_color() { #[test] fn blend_alpha_color() { - let a = Colora::linear_rgb(1.0, 0.0, 0.0, 0.2); - let b = Colora::linear_rgb(0.0, 0.0, 1.0, 0.2); + let a = LinSrgba::new(1.0, 0.0, 0.0, 0.2); + let b = LinSrgba::new(0.0, 0.0, 1.0, 0.2); let c: LinSrgba = a.blend( b, diff --git a/palette/src/convert.rs b/palette/src/convert.rs index 446e82cae..e2f373351 100644 --- a/palette/src/convert.rs +++ b/palette/src/convert.rs @@ -556,7 +556,6 @@ pub trait ConvertInto: Into { ///let rgb: Srgb = Lch::new(50.0, 100.0, -175.0).convert_unclamped_into(); ///assert!(!rgb.is_valid()); ///``` - #[inline] fn convert_unclamped_into(self) -> T; ///Convert into T, returning ok if the color is inside of its defined range, @@ -762,10 +761,10 @@ mod tests { use core::marker::PhantomData; use float::Float; use Component; - use Linear; + use encoding::linear::Linear; use rgb::{Rgb, RgbSpace}; use luma::Luma; - use {Color, Hsl, Hsv, Hwb, Lab, Lch, Xyz, Yxy}; + use {Hsl, Hsv, Hwb, Lab, Lch, Xyz, Yxy}; #[derive(Copy, Clone, FromColor, IntoColor)] #[palette_manual_from(Xyz, Luma = "from_luma_internal")] @@ -857,9 +856,6 @@ mod tests { let luma: Luma<::encoding::Srgb, f64> = Default::default(); WithXyz::<::encoding::Srgb>::from(luma); - - let color: Color<_, f64> = Default::default(); - WithXyz::<::encoding::Srgb>::from(color); } #[test] @@ -875,7 +871,6 @@ mod tests { let _hsv: Hsv<_, f64> = color.into(); let _hwb: Hwb<_, f64> = color.into(); let _luma: Luma<::encoding::Srgb, f64> = color.into(); - let _color: Color<::encoding::Srgb, f64> = color.into(); } #[test] @@ -906,9 +901,6 @@ mod tests { let luma: Luma, f64> = Default::default(); WithoutXyz::::from(luma); - - let color: Color<_, f64> = Default::default(); - WithoutXyz::::from(color); } #[test] @@ -924,6 +916,5 @@ mod tests { let _hsv: Hsv<_, f64> = color.into(); let _hwb: Hwb<_, f64> = color.into(); let _luma: Luma, f64> = color.into(); - let _color: Color<_, f64> = color.into(); } } diff --git a/palette/src/lib.rs b/palette/src/lib.rs index 11a8ffc16..5fe61bff5 100644 --- a/palette/src/lib.rs +++ b/palette/src/lib.rs @@ -165,12 +165,7 @@ extern crate serde_json; use num_traits::{NumCast, ToPrimitive, Zero}; use float::Float; -use approx::{AbsDiffEq, RelativeEq, UlpsEq}; - -use blend::PreAlpha; -use encoding::Linear; use luma::Luma; -use rgb::{Rgb, RgbSpace, Rgba}; #[doc(hidden)] pub use palette_derive::*; @@ -379,245 +374,6 @@ pub mod white_point; pub mod float; -macro_rules! make_color { - ($( - #[$variant_comment:meta] - $variant: ident < $variant_ty_param:ty > $(and $($representations:ident),+ )* {$( - #[$ctor_comment:meta] - $ctor_name:ident $( <$( $ty_params:ident: $ty_param_traits:ident $( <$( $ty_inner_traits:ident ),*> )*),*> )* ($($ctor_field:ident : $ctor_ty:ty),*) [alpha: $alpha_ty:ty] => $ctor_original:ident; - )+} - )+) => ( - - ///Generic color with an alpha component. See the [`Colora` implementation in `Alpha`](struct.Alpha.html#Colora). - pub type Colora = Alpha, T>; - - ///A generic color type. - /// - ///The `Color` may belong to any color space and it may change - ///depending on which operation is performed. That makes it immune to - ///the "without conversion" rule of the operations it supports. The - ///color spaces are selected as follows: - /// - /// * `Mix`: RGB for no particular reason, except that it's intuitive. - /// * `Shade`: CIE L\*a\*b\* for its luminance component. - /// * `Hue` and `GetHue`: CIE L\*C\*h° for its hue component and how it preserves the apparent lightness. - /// * `Saturate`: CIE L\*C\*h° for its chromaticity component and how it preserves the apparent lightness. - /// - ///It's not recommended to use `Color` when full control is necessary, - ///but it can easily be converted to a fixed color space in those - ///cases. - #[derive(Debug, PartialEq)] - pub enum Color - where T: Float + Component, - S: RgbSpace, - { - $(#[$variant_comment] $variant($variant<$variant_ty_param, T>)),+ - } - - impl Copy for Color - where S: RgbSpace, - T: Float + Component, - {} - - impl Clone for Color - where S: RgbSpace, - T: Float + Component, - { - fn clone(&self) -> Color { *self } - } - - impl Default for Color - where S: RgbSpace, - T: Float + Component, - { - fn default() -> Color { Color::Rgb(Default::default()) } - } - - impl Color { - $( - $( - #[$ctor_comment] - pub fn $ctor_name$(<$($ty_params : $ty_param_traits$( <$( $ty_inner_traits ),*> )*),*>)*($($ctor_field: $ctor_ty),*) -> Color { - Color::$variant($variant::$ctor_original($($ctor_field),*)) - } - )+ - )+ - } - - ///[`Colora`](type.Colora.html) implementations. - impl Alpha, T> { - $( - $( - #[$ctor_comment] - pub fn $ctor_name$(<$($ty_params : $ty_param_traits$( <$( $ty_inner_traits ),*> )*),*>)*($($ctor_field: $ctor_ty,)* alpha: $alpha_ty) -> Colora { - Alpha::<$variant<_, T>, T>::$ctor_original($($ctor_field,)* alpha).into() - } - )+ - )+ - } - - impl Mix for Color - where T: Float + Component, - S: RgbSpace, - { - type Scalar = T; - - fn mix(&self, other: &Color, factor: T) -> Color { - Rgb::, T>::from(*self).mix(&Rgb::, T>::from(*other), factor).into() - } - } - - impl Shade for Color - where T: Float + Component, - S: RgbSpace, - { - type Scalar = T; - - fn lighten(&self, amount: T) -> Color { - Lab::from(*self).lighten(amount).into() - } - } - - impl GetHue for Color - where T: Float + Component, - S: RgbSpace, - { - type Hue = LabHue; - - fn get_hue(&self) -> Option> { - Lch::from(*self).get_hue() - } - } - - impl Hue for Color - where T: Float + Component, - S: RgbSpace, - { - fn with_hue>(&self, hue: H) -> Color { - Lch::from(*self).with_hue(hue).into() - } - - fn shift_hue>(&self, amount: H) -> Color { - Lch::from(*self).shift_hue(amount).into() - } - } - - impl Saturate for Color - where T: Float + Component, - S: RgbSpace, - { - type Scalar = T; - - fn saturate(&self, factor: T) -> Color { - Lch::from(*self).saturate(factor).into() - } - } - - impl Blend for Color - where T: Float + Component, - S: RgbSpace, - { - type Color = Rgb, T>; - - fn into_premultiplied(self) -> PreAlpha, T>, T> { - Rgba::, T>::from(self).into() - } - - fn from_premultiplied(color: PreAlpha, T>, T>) -> Self { - Rgba::, T>::from(color).into() - } - } - - impl AbsDiffEq for Color - where T: Float + Component + AbsDiffEq, - T::Epsilon: Float, - S: RgbSpace + PartialEq, - ::WhitePoint: PartialEq, - { - type Epsilon = T::Epsilon; - - fn default_epsilon() -> Self::Epsilon { - T::default_epsilon() - } - - fn abs_diff_eq(&self, other: &Self, epsilon: T::Epsilon) -> bool { - match (*self, *other) { - $((Color::$variant(ref s), Color::$variant(ref o)) => s.abs_diff_eq(o, epsilon),)+ - _ => false - } - } - } - - impl RelativeEq for Color - where T: Float + Component + RelativeEq, - T::Epsilon: Float, - S: RgbSpace + PartialEq, - ::WhitePoint: PartialEq, - { - fn default_max_relative() -> Self::Epsilon { - T::default_max_relative() - } - - fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool { - match (*self, *other) { - $((Color::$variant(ref s), Color::$variant(ref o)) => s.relative_eq(o, epsilon, max_relative),)+ - _ => false - } - } - } - - impl UlpsEq for Color - where T: Float + Component + UlpsEq, - T::Epsilon: Float, - S: RgbSpace + PartialEq, - ::WhitePoint: PartialEq, - { - fn default_max_ulps() -> u32 { - T::default_max_ulps() - } - - fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool { - match (*self, *other) { - $((Color::$variant(ref s), Color::$variant(ref o)) => s.ulps_eq(o, epsilon, max_ulps),)+ - _ => false - } - } - } - - $( - impl From<$variant<$variant_ty_param, T>> for Color - where T: Float + Component, - S: RgbSpace, - { - fn from(color: $variant<$variant_ty_param, T>) -> Color { - Color::$variant(color) - } - } - - impl From, T>> for Color - where T: Float + Component, - S: RgbSpace, - { - fn from(color: Alpha<$variant<$variant_ty_param, T>,T>) -> Color { - Color::$variant(color.color) - } - } - - impl From, T>> for Alpha,T> - where T: Float + Component, - S: RgbSpace, - { - fn from(color: Alpha<$variant<$variant_ty_param, T>,T>) -> Alpha,T> { - Alpha { - color: Color::$variant(color.color), - alpha: color.alpha, - } - } - } - )+ - ) -} - fn clamp(v: T, min: T, max: T) -> T { if v < min { min @@ -628,62 +384,6 @@ fn clamp(v: T, min: T, max: T) -> T { } } -make_color! { - ///Linear luminance. - Luma> { - ///Linear luminance. - linear_y(luma: T)[alpha: T] => new; - } - - ///Linear RGB. - Rgb> { - ///Linear RGB. - linear_rgb(red: T, green: T, blue: T)[alpha: T] => new; - } - - ///CIE 1931 XYZ. - Xyz { - ///CIE XYZ. - xyz(x: T, y: T, z: T)[alpha: T] => new; - } - - ///CIE 1931 Yxy. - Yxy { - ///CIE Yxy. - yxy(x: T, y: T, luma: T)[alpha: T] => new; - } - - ///CIE L\*a\*b\* (CIELAB). - Lab { - ///CIE L\*a\*b\*. - lab(l: T, a: T, b: T)[alpha: T] => new; - } - - ///CIE L\*C\*h°, a polar version of CIE L\*a\*b\*. - Lch { - ///CIE L\*C\*h°. - lch(l: T, chroma: T, hue: LabHue)[alpha: T] => new; - } - - ///Linear HSV, a cylindrical version of RGB. - Hsv { - ///Linear HSV. - hsv(hue: RgbHue, saturation: T, value: T)[alpha: T] => new; - } - - ///Linear HSL, a cylindrical version of RGB. - Hsl { - ///Linear HSL. - hsl(hue: RgbHue, saturation: T, lightness: T)[alpha: T] => new; - } - - ///Linear HWB, an intuitive cylindrical version of RGB. - Hwb { - ///Linear HWB. - hwb(hue: RgbHue, whiteness: T, balckness: T)[alpha: T] => new; - } -} - ///A trait for clamping and checking if colors are within their ranges. pub trait Limited { ///Check if the color's components are within the expected ranges. diff --git a/palette_derive/src/convert/from_color.rs b/palette_derive/src/convert/from_color.rs index f871b0080..34ccf8a48 100644 --- a/palette_derive/src/convert/from_color.rs +++ b/palette_derive/src/convert/from_color.rs @@ -129,39 +129,6 @@ pub fn derive(tokens: TokenStream) -> TokenStream { }) .collect(); - let from_color_ty = impl_from_color_type(&ident, &meta, &original_generics, generic_component); - - let from_color_ty_self_alpha = if meta.internal { - Some(impl_from_color_type_no_alpha_to_alpha( - &ident, - &meta, - &original_generics, - generic_component, - )) - } else { - None - }; - - let from_color_ty_other_alpha = impl_from_color_type_alpha_to_no_alpha( - &ident, - &meta, - &original_generics, - generic_component, - alpha_property.as_ref(), - &alpha_type, - ); - - let from_color_ty_both_alpha = if meta.internal { - Some(impl_from_color_type_alpha_to_alpha( - &ident, - &meta, - &original_generics, - generic_component, - )) - } else { - None - }; - let trait_path = util::path(&["FromColor"], meta.internal); let from_color_impl = quote!{ #[automatically_derived] @@ -177,10 +144,6 @@ pub fn derive(tokens: TokenStream) -> TokenStream { quote! { #from_color_impl #(#from_impls)* - #from_color_ty - #from_color_ty_self_alpha - #from_color_ty_other_alpha - #from_color_ty_both_alpha }, ); @@ -392,214 +355,6 @@ struct FromImplParameters { method_call: TokenStream2, } -fn impl_from_color_type( - ident: &Ident, - meta: &FromColorMeta, - generics: &Generics, - generic_component: bool, -) -> TokenStream2 { - let (_, type_generics, _) = generics.split_for_impl(); - let turbofish_generics = type_generics.as_turbofish(); - - let FromColorTypeImplParameters { - color_path, - generics, - color_ty, - .. - } = prepare_from_color_type_impl(meta, generics, generic_component); - - let match_arms = COLOR_TYPES.into_iter().map(|&color| { - let color_ident = Ident::new(color, Span::call_site()); - if meta.internal && ident == color { - let convert_function = meta - .manual_implementations - .iter() - .find(|color_impl| color_impl.key == color) - .and_then(|color_impl| color_impl.value.as_ref()); - - if let Some(convert_function) = convert_function { - quote!(#color_path::#color_ident(c) => #ident #turbofish_generics::#convert_function(c)) - } else { - quote!(#color_path::#color_ident(c) => c) - } - } else { - quote!(#color_path::#color_ident(c) => Self::from(c)) - } - }); - - let (impl_generics, _, where_clause) = generics.split_for_impl(); - - quote!{ - #[automatically_derived] - impl #impl_generics From<#color_ty> for #ident #type_generics #where_clause { - fn from(color: #color_ty) -> Self { - match color { - #(#match_arms),* - } - } - } - } -} - -fn impl_from_color_type_alpha_to_alpha( - ident: &Ident, - meta: &FromColorMeta, - generics: &Generics, - generic_component: bool, -) -> TokenStream2 { - let (_, type_generics, _) = generics.split_for_impl(); - let turbofish_generics = type_generics.as_turbofish(); - - let FromColorTypeImplParameters { - generics, - alpha_path, - color_ty, - component, - .. - } = prepare_from_color_type_impl(meta, generics, generic_component); - - let (impl_generics, _, where_clause) = generics.split_for_impl(); - - quote!{ - #[automatically_derived] - impl #impl_generics From<#alpha_path<#color_ty, #component>> for #alpha_path<#ident #type_generics, #component> #where_clause { - fn from(color: #alpha_path<#color_ty, #component>) -> Self { - let #alpha_path {color, alpha} = color; - #alpha_path { - color: #ident #turbofish_generics::from(color), - alpha - } - } - } - } -} - -fn impl_from_color_type_no_alpha_to_alpha( - ident: &Ident, - meta: &FromColorMeta, - generics: &Generics, - generic_component: bool, -) -> TokenStream2 { - let (_, type_generics, _) = generics.split_for_impl(); - let turbofish_generics = type_generics.as_turbofish(); - - let FromColorTypeImplParameters { - generics, - alpha_path, - color_ty, - component, - .. - } = prepare_from_color_type_impl(meta, generics, generic_component); - - let (impl_generics, _, where_clause) = generics.split_for_impl(); - - quote!{ - #[automatically_derived] - impl #impl_generics From<#color_ty> for #alpha_path<#ident #type_generics, #component> #where_clause { - fn from(color: #color_ty) -> Self { - #ident #turbofish_generics::from(color).into() - } - } - } -} - -fn impl_from_color_type_alpha_to_no_alpha( - ident: &Ident, - meta: &FromColorMeta, - generics: &Generics, - generic_component: bool, - alpha_property: Option<&IdentOrIndex>, - alpha_type: &Type, -) -> TokenStream2 { - let (_, type_generics, _) = generics.split_for_impl(); - - let FromColorTypeImplParameters { - generics, - alpha_path, - color_ty, - .. - } = prepare_from_color_type_impl(meta, generics, generic_component); - - let (impl_generics, _, where_clause) = generics.split_for_impl(); - - if let Some(alpha_property) = alpha_property { - quote!{ - #[automatically_derived] - impl #impl_generics From<#alpha_path<#color_ty, #alpha_type>> for #ident #type_generics #where_clause { - fn from(color: #alpha_path<#color_ty, #alpha_type>) -> Self { - let #alpha_path { color, alpha } = color; - let mut result = Self::from(color); - result.#alpha_property = alpha; - result - } - } - } - } else { - quote!{ - #[automatically_derived] - impl #impl_generics From<#alpha_path<#color_ty, #alpha_type>> for #ident #type_generics #where_clause { - fn from(color: #alpha_path<#color_ty, #alpha_type>) -> Self { - Self::from(color.color) - } - } - } - } -} - -fn prepare_from_color_type_impl( - meta: &FromColorMeta, - generics: &Generics, - generic_component: bool, -) -> FromColorTypeImplParameters { - let mut generics = generics.clone(); - - let color_path = util::path(&["Color"], meta.internal); - let alpha_path = util::path(&["Alpha"], meta.internal); - - let white_point = shared::white_point_type(meta.white_point.clone(), meta.internal); - let component = shared::component_type(meta.component.clone()); - - if meta.rgb_space.is_none() { - generics.params.push(parse_quote!(_S)); - } - - if generic_component { - shared::add_component_where_clause(&component, &mut generics, meta.internal); - } - - let color_ty = { - let rgb_space_type = if let Some(ref rgb_space) = meta.rgb_space { - rgb_space.clone() - } else { - let rgb_space_path = util::path(&["rgb", "RgbSpace"], meta.internal); - generics - .make_where_clause() - .predicates - .push(parse_quote!(_S: #rgb_space_path)); - - parse_quote!(_S) - }; - - quote!(#color_path<#rgb_space_type, #component>) - }; - - return FromColorTypeImplParameters { - generics, - alpha_path, - color_path, - color_ty, - component, - }; -} - -struct FromColorTypeImplParameters { - generics: Generics, - alpha_path: TokenStream2, - color_path: TokenStream2, - color_ty: TokenStream2, - component: Type, -} - #[derive(Default)] struct FromColorMeta { manual_implementations: Vec, diff --git a/palette_derive/src/convert/into_color.rs b/palette_derive/src/convert/into_color.rs index 85df86518..d1d93599d 100644 --- a/palette_derive/src/convert/into_color.rs +++ b/palette_derive/src/convert/into_color.rs @@ -107,16 +107,6 @@ pub fn derive(tokens: TokenStream) -> TokenStream { } }); - let into_color_ty = impl_into_color_type(&ident, &meta, &original_generics, generic_component); - let from_color_ty_other_alpha = impl_into_color_type_alpha( - &ident, - &meta, - &original_generics, - generic_component, - alpha_property.as_ref(), - &alpha_type, - ); - let result = util::bundle_impl( "IntoColor", ident.clone(), @@ -124,8 +114,6 @@ pub fn derive(tokens: TokenStream) -> TokenStream { quote! { #into_color_impl #(#into_impls)* - #into_color_ty - #from_color_ty_other_alpha }, ); @@ -271,124 +259,6 @@ struct IntoImplParameters { method_call: TokenStream2, } -fn impl_into_color_type( - ident: &Ident, - meta: &IntoColorMeta, - generics: &Generics, - generic_component: bool, -) -> TokenStream2 { - let (_, type_generics, _) = generics.split_for_impl(); - - let IntoColorTypeImplParameters { - generics, - color_path, - color_ty, - } = prepare_into_color_type_impl(meta, generics, generic_component); - - let (impl_generics, _, where_clause) = generics.split_for_impl(); - - quote!{ - #[automatically_derived] - impl #impl_generics Into<#color_ty> for #ident #type_generics #where_clause { - fn into(self) -> #color_ty { - #color_path::Rgb(self.into()) - } - } - } -} - -fn impl_into_color_type_alpha( - ident: &Ident, - meta: &IntoColorMeta, - generics: &Generics, - generic_component: bool, - alpha_property: Option<&IdentOrIndex>, - alpha_type: &Type, -) -> TokenStream2 { - let (_, type_generics, _) = generics.split_for_impl(); - let alpha_path = util::path(&["Alpha"], meta.internal); - - let IntoColorTypeImplParameters { - generics, - color_path, - color_ty, - } = prepare_into_color_type_impl(meta, generics, generic_component); - - let (impl_generics, _, where_clause) = generics.split_for_impl(); - - if let Some(alpha_property) = alpha_property { - quote!{ - #[automatically_derived] - impl #impl_generics Into<#alpha_path<#color_ty, #alpha_type>> for #ident #type_generics #where_clause { - fn into(self) -> #alpha_path<#color_ty, #alpha_type> { - #alpha_path { - alpha: self.#alpha_property.clone(), - color: #color_path::Rgb(self.into()).into(), - } - } - } - } - } else { - quote!{ - #[automatically_derived] - impl #impl_generics Into<#alpha_path<#color_ty, #alpha_type>> for #ident #type_generics #where_clause { - fn into(self) -> #alpha_path<#color_ty, #alpha_type> { - #color_path::Rgb(self.into()).into() - } - } - } - } -} - -fn prepare_into_color_type_impl( - meta: &IntoColorMeta, - generics: &Generics, - generic_component: bool, -) -> IntoColorTypeImplParameters { - let mut generics = generics.clone(); - - let color_path = util::path(&["Color"], meta.internal); - - let white_point = shared::white_point_type(meta.white_point.clone(), meta.internal); - let component = shared::component_type(meta.component.clone()); - - if meta.rgb_space.is_none() { - generics.params.push(parse_quote!(_S)); - } - - if generic_component { - shared::add_component_where_clause(&component, &mut generics, meta.internal) - } - - let color_ty = { - let rgb_space_type = if let Some(ref rgb_space) = meta.rgb_space { - rgb_space.clone() - } else { - let rgb_space_path = util::path(&["rgb", "RgbSpace"], meta.internal); - generics - .make_where_clause() - .predicates - .push(parse_quote!(_S: #rgb_space_path)); - - parse_quote!(_S) - }; - - quote!(#color_path<#rgb_space_type, #component>) - }; - - IntoColorTypeImplParameters { - generics, - color_path, - color_ty, - } -} - -struct IntoColorTypeImplParameters { - generics: Generics, - color_path: TokenStream2, - color_ty: TokenStream2, -} - #[derive(Default)] struct IntoColorMeta { manual_implementations: Vec,