1use crate::{
2 ColorAlpha, converters::*, ratio_converters::ratio_to_hsla,
3};
4
5use super::{Hsl, HslRatio, Rgb};
6
7macro_rules! from_for_hsl {
8 ($from_type: ty, $val: ident, $conv: block) => {
9 impl From<&$from_type> for Hsl {
10 fn from($val: &$from_type) -> Hsl {
11 ($conv)
12 }
13 }
14 impl From<$from_type> for Hsl {
15 fn from($val: $from_type) -> Hsl {
16 Hsl::from(&$val)
17 }
18 }
19 };
20}
21
22macro_rules! from_for_hsl_all {
23 ($t: ty) => {
24 from_for_hsl!(($t, $t, $t), v, {
25 let (h, s, l) = *v;
26 Hsl::new(h as f64, s as f64, l as f64, None)
27 });
28 from_for_hsl!([$t; 3], v, {
29 let [h, s, l] = *v;
30 Hsl::new(h as f64, s as f64, l as f64, None)
31 });
32 };
33}
34
35macro_rules! from_for_hsl_all_with_alpha {
36 ($t: ty) => {
37 from_for_hsl!(($t, $t, $t, $t), v, {
38 let (h, s, l, a) = *v;
39 Hsl::new(h as f64, s as f64, l as f64, Some(a as f64))
40 });
41 from_for_hsl!([$t; 4], v, {
42 let [h, s, l, a] = *v;
43 Hsl::new(h as f64, s as f64, l as f64, Some(a as f64))
44 });
45 };
46}
47
48from_for_hsl_all!(f32);
49from_for_hsl_all!(f64);
50from_for_hsl_all_with_alpha!(f32);
51from_for_hsl_all_with_alpha!(f64);
52from_for_hsl_all!(i16);
53from_for_hsl_all!(i32);
54from_for_hsl_all!(i64);
55from_for_hsl_all!(u16);
56from_for_hsl_all!(u32);
57from_for_hsl_all!(u64);
58
59fn from_rgb(rgb: &Rgb) -> Hsl {
60 let a = rgb.alpha();
61 let mut hsl = Hsl::from_units(rgb_to_hsl(rgb));
62 hsl.set_alpha(a);
63 hsl
64}
65
66impl From<&Rgb> for Hsl {
67 fn from(rgb: &Rgb) -> Self {
75 from_rgb(rgb)
76 }
77}
78
79impl From<&mut Rgb> for Hsl {
80 fn from(rgb: &mut Rgb) -> Self {
88 from_rgb(rgb)
89 }
90}
91
92impl From<Rgb> for Hsl {
93 fn from(rgb: Rgb) -> Self {
101 from_rgb(&rgb)
102 }
103}
104
105fn from_hsl_ratio(ratio: &HslRatio) -> Hsl {
106 let ru = &ratio.units;
107 let t = ratio_to_hsla(&(ru[0], ru[1], ru[2], ru.alpha.get_f64()));
108 Hsl::new(t.0, t.1, t.2, Some(t.3))
109}
110
111impl From<&HslRatio> for Hsl {
112 fn from(r: &HslRatio) -> Self {
113 from_hsl_ratio(r)
114 }
115}
116
117impl From<&mut HslRatio> for Hsl {
118 fn from(r: &mut HslRatio) -> Self {
119 from_hsl_ratio(r)
120 }
121}
122
123impl From<HslRatio> for Hsl {
124 fn from(r: HslRatio) -> Self {
125 from_hsl_ratio(&r)
126 }
127}
128
129macro_rules! into_for_hsl_all {
136 ($t: ty) => {
137 into_for_some!(($t, $t, $t), Hsl, self, {
138 let u = &self.units;
139 (u[0] as $t, u[1] as $t, u[2] as $t)
140 });
141 into_for_some!([$t; 3], Hsl, self, {
142 let u = &self.units;
143 [u[0] as $t, u[1] as $t, u[2] as $t]
144 });
145 };
146}
147
148macro_rules! into_for_hsl_all_with_alpha {
149 ($t: ty) => {
150 into_for_some!(($t, $t, $t, $t), Hsl, self, {
151 let u = &self.units;
152 (u[0] as $t, u[1] as $t, u[2] as $t, self.units.alpha.get_f64() as $t)
153 });
154 into_for_some!([$t; 4], Hsl, self, {
155 let u = &self.units;
156 [u[0] as $t, u[1] as $t, u[2] as $t, self.units.alpha.get_f64() as $t]
157 });
158 };
159}
160
161into_for_hsl_all!(f32);
162into_for_hsl_all!(f64);
163into_for_hsl_all_with_alpha!(f32);
164into_for_hsl_all_with_alpha!(f64);
165into_for_hsl_all!(i16);
166into_for_hsl_all!(i32);
167into_for_hsl_all!(i64);
168into_for_hsl_all!(u16);
169into_for_hsl_all!(u32);
170into_for_hsl_all!(u64);