Debian Codegraph @

src/convert/from_into_color.rs

raw symbols SCIP
1 use craterust-analyzer cargo palette 0.7.6 crate/::Clamprust-analyzer cargo palette 0.7.6 Clamp#;
2
3 #[cfg(feature = "alloc")]
4 use craterust-analyzer cargo palette 0.7.6 crate/::castrust-analyzer cargo palette 0.7.6 cast/::{selfrust-analyzer cargo palette 0.7.6 cast/, ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#};
5
6 use superrust-analyzer cargo palette 0.7.6 convert/::FromColorUnclampedrust-analyzer cargo palette 0.7.6 convert/from_into_color_unclamped/FromColorUnclamped#;
7
8 ///A trait for converting one color from another, in a possibly lossy way.
9 ///
10 /// `U: FromColor<T>` is implemented for every type `U: FromColorUnclamped<T> +
11 /// Clamp`, as well as for `Vec<T>` and `Box<[T]>` where `T` and `U` have the
12 /// same memory layout.
13 ///
14 /// See [`FromColorUnclamped`](crate::convert::FromColorUnclamped) for a
15 /// lossless version of this trait. See
16 /// [`TryFromColor`](crate::convert::TryFromColor) for a trait that gives an
17 /// error when the result is out of bounds.
18 ///
19 /// # The Difference Between FromColor and From
20 ///
21 /// The conversion traits, including `FromColor`, were added to gain even more
22 /// flexibility than what `From` and the other standard library traits can give.
23 /// There are a few subtle, but important, differences in their semantics:
24 ///
25 /// * `FromColor` and `IntoColor` are allowed to be lossy, meaning converting `A
26 /// -> B -> A` may result in a different value than the original. This applies
27 /// to `A -> A` as well.
28 /// * `From<Self>` and `Into<Self>` are blanket implemented, while
29 /// `FromColor<Self>` and `IntoColor<Self>` have to be manually implemented.
30 /// This allows additional flexibility, such as allowing implementing
31 /// `FromColor<Rgb<S2, T>> for Rgb<S1, T>`.
32 /// * Implementing `FromColorUnclamped`,
33 /// [`IsWithinBounds`](crate::IsWithinBounds) and [`Clamp`] is enough to get
34 /// all the other conversion traits, while `From` and `Into` would not be
35 /// possible to blanket implement in the same way. This also reduces the work
36 /// that needs to be done by macros.
37 ///
38 /// See the [`convert`](crate::convert) module for how to implement
39 /// `FromColorUnclamped` for custom colors.
40 pub trait FromColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#<T>: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
41 /// Convert from T with values clamped to the color defined bounds.
42 ///
43 /// ```
44 /// use palette::{IsWithinBounds, FromColor, Lch, Srgb};
45 ///
46 /// let rgb = Srgb::from_color(Lch::new(50.0f32, 100.0, -175.0));
47 /// assert!(rgb.is_within_bounds());
48 /// ```
49 #[must_use]
50 fn from_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#from_color().(t: T) -> Self;
51 }
52
53 impl<T, U> FromColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#<T> for U
54 where
55 U: FromColorUnclampedrust-analyzer cargo palette 0.7.6 convert/from_into_color_unclamped/FromColorUnclamped#<T> + Clamprust-analyzer cargo palette 0.7.6 Clamp#,
56 {
57 #[inline]
58 fn from_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/impl#[U][`FromColor<T>`]from_color().(t: T) -> Selfrust-analyzer cargo palette 0.7.6 convert/from_into_color/impl#[U][`FromColor<T>`] {
59 Selfrust-analyzer cargo palette 0.7.6 convert/from_into_color/impl#[U][`FromColor<T>`]::from_color_unclampedrust-analyzer cargo palette 0.7.6 convert/from_into_color_unclamped/FromColorUnclamped#from_color_unclamped().(t).clamprust-analyzer cargo palette 0.7.6 Clamp#clamp().()
60 }
61 }
62
63 #[cfg(feature = "alloc")]
64 impl<T, U> FromColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#<allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>> for allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<U>
65 where
66 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
67 U: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#<Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# = T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#> + FromColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#<T>,
68 {
69 /// Convert all colors in place, without reallocating.
70 ///
71 /// ```
72 /// use palette::{convert::FromColor, SaturateAssign, Srgb, Lch};
73 ///
74 /// let srgb = vec![Srgb::new(0.8f32, 1.0, 0.2), Srgb::new(0.9, 0.1, 0.3)];
75 /// let mut lch = Vec::<Lch>::from_color(srgb);
76 ///
77 /// lch.saturate_assign(0.1);
78 ///
79 /// let srgb = Vec::<Srgb>::from_color(lch);
80 /// ```
81 #[inline]
82 fn from_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/impl#[`Vec<U>`][`FromColor<Vec<T>>`]from_color().(color: allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/::Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>) -> Selfrust-analyzer cargo palette 0.7.6 convert/from_into_color/impl#[`Vec<U>`][`FromColor<Vec<T>>`] {
83 castrust-analyzer cargo palette 0.7.6 cast/::map_vec_in_placerust-analyzer cargo palette 0.7.6 cast/array/map_vec_in_place().(color, U::from_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#from_color().)
84 }
85 }
86
87 #[cfg(feature = "alloc")]
88 impl<T, U> FromColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#<allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::boxedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/::Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T]>> for allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::boxedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/::Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[U]>
89 where
90 T: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#,
91 U: ArrayCastrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#<Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array# = T::Arrayrust-analyzer cargo palette 0.7.6 cast/array/ArrayCast#Array#> + FromColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#<T>,
92 {
93 /// Convert all colors in place, without reallocating.
94 ///
95 /// ```
96 /// use palette::{convert::FromColor, SaturateAssign, Srgb, Lch};
97 ///
98 /// let srgb = vec![Srgb::new(0.8f32, 1.0, 0.2), Srgb::new(0.9, 0.1, 0.3)].into_boxed_slice();
99 /// let mut lch = Box::<[Lch]>::from_color(srgb);
100 ///
101 /// lch.saturate_assign(0.1);
102 ///
103 /// let srgb = Box::<[Srgb]>::from_color(lch);
104 /// ```
105 #[inline]
106 fn from_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/impl#[`Box<[U]>`][`FromColor<Box<[T]>>`]from_color().(color: allocrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc crate/::boxedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/::Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<[T]>) -> Selfrust-analyzer cargo palette 0.7.6 convert/from_into_color/impl#[`Box<[U]>`][`FromColor<Box<[T]>>`] {
107 castrust-analyzer cargo palette 0.7.6 cast/::map_slice_box_in_placerust-analyzer cargo palette 0.7.6 cast/array/map_slice_box_in_place().(color, U::from_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#from_color().)
108 }
109 }
110
111 /// A trait for converting a color into another, in a possibly lossy way.
112 ///
113 /// `U: IntoColor<T>` is implemented for every type `T: FromColor<U>`.
114 ///
115 /// See [`FromColor`](crate::convert::FromColor) for more details.
116 pub trait IntoColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/IntoColor#<T>: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# {
117 /// Convert into T with values clamped to the color defined bounds
118 ///
119 /// ```
120 /// use palette::{IsWithinBounds, IntoColor, Lch, Srgb};
121 ///
122 /// let rgb: Srgb = Lch::new(50.0, 100.0, -175.0).into_color();
123 /// assert!(rgb.is_within_bounds());
124 /// ```
125 #[must_use]
126 fn into_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/IntoColor#into_color().(self) -> T;
127 }
128
129 impl<T, U> IntoColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/IntoColor#<U> for T
130 where
131 U: FromColorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#<T>,
132 {
133 #[inline]
134 fn into_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/impl#[T][`IntoColor<U>`]into_color().(self) -> U {
135 U::from_colorrust-analyzer cargo palette 0.7.6 convert/from_into_color/FromColor#from_color().(self)
136 }
137 }