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