color_gradient/blenders/rgb/mod.rs
1use super::*;
2
3/// A color interpolator that interpolates between colors in the [RGB Color Space](https://en.wikipedia.org/wiki/RGB_color_space).
4#[derive(Clone, Debug)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub struct RgbGradient {
7 red: Interpolator,
8 green: Interpolator,
9 blue: Interpolator,
10 alpha: Interpolator,
11 range: Range<f32>,
12}
13
14impl RgbGradient {
15 /// # Arguments
16 ///
17 /// * `min`:
18 /// * `max`:
19 ///
20 /// returns: RgbGradient
21 ///
22 /// # Examples
23 ///
24 /// ```
25 /// # use color_gradient::HsvGradient;
26 /// ```
27 pub fn new(min: f32, max: f32) -> Self {
28 debug_assert!(max > min, "max must be greater than min");
29 Self {
30 red: Interpolator::new(0.0, 1.0),
31 green: Interpolator::new(0.0, 1.0),
32 blue: Interpolator::new(0.0, 1.0),
33 alpha: Interpolator::new(0.0, 1.0),
34 range: Range { start: min, end: max },
35 }
36 }
37 /// # Arguments
38 ///
39 /// * `min`:
40 /// * `max`:
41 ///
42 /// returns: RgbGradient
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// # use color_gradient::HsvGradient;
48 /// ```
49 pub fn rescale(&mut self, min: f32, max: f32) {
50 debug_assert!(max > min, "max must be greater than min");
51 self.range = Range { start: min, end: max };
52 }
53 /// # Arguments
54 ///
55 /// * `min`:
56 /// * `max`:
57 ///
58 /// returns: RgbGradient
59 ///
60 /// # Examples
61 ///
62 /// ```
63 /// # use color_gradient::HsvGradient;
64 /// ```
65 pub fn insert_color<RGB>(&mut self, key: f32, color: RGB)
66 where
67 RGB: Into<RGBA32>,
68 {
69 let rgba = color.into();
70 let ratio = self.get_ratio(key);
71 self.red.insert(ratio, rgba.r);
72 self.green.insert(ratio, rgba.g);
73 self.blue.insert(ratio, rgba.b);
74 self.alpha.insert(ratio, rgba.a);
75 }
76 /// # Arguments
77 ///
78 /// * `min`:
79 /// * `max`:
80 ///
81 /// returns: RgbGradient
82 ///
83 /// # Examples
84 ///
85 /// ```
86 /// # use color_gradient::HsvGradient;
87 /// ```
88 pub fn remove_color(&mut self, key: f32) {
89 let ratio = self.get_ratio(key);
90 self.red.remove(ratio);
91 self.green.remove(ratio);
92 self.blue.remove(ratio);
93 self.alpha.remove(ratio);
94 }
95 /// # Arguments
96 ///
97 /// * `min`:
98 /// * `max`:
99 ///
100 /// returns: RgbGradient
101 ///
102 /// # Examples
103 ///
104 /// ```
105 /// # use color_gradient::HsvGradient;
106 /// ```
107 pub fn insert_red(&mut self, key: f32, value: f32) {
108 let ratio = self.get_ratio(key);
109 self.red.insert(ratio, value);
110 }
111 /// # Arguments
112 ///
113 /// * `min`:
114 /// * `max`:
115 ///
116 /// returns: RgbGradient
117 ///
118 /// # Examples
119 ///
120 /// ```
121 /// # use color_gradient::HsvGradient;
122 /// ```
123 pub fn remove_red(&mut self, key: f32) {
124 let ratio = self.get_ratio(key);
125 self.red.remove(ratio);
126 }
127 /// # Arguments
128 ///
129 /// * `min`:
130 /// * `max`:
131 ///
132 /// returns: RgbGradient
133 ///
134 /// # Examples
135 ///
136 /// ```
137 /// # use color_gradient::HsvGradient;
138 /// ```
139 pub fn insert_green(&mut self, key: f32, value: f32) {
140 let ratio = self.get_ratio(key);
141 self.green.insert(ratio, value);
142 }
143 /// # Arguments
144 ///
145 /// * `min`:
146 /// * `max`:
147 ///
148 /// returns: RgbGradient
149 ///
150 /// # Examples
151 ///
152 /// ```
153 /// # use color_gradient::HsvGradient;
154 /// ```
155 pub fn remove_green(&mut self, key: f32) {
156 let ratio = self.get_ratio(key);
157 self.green.remove(ratio);
158 }
159 /// # Arguments
160 ///
161 /// * `min`:
162 /// * `max`:
163 ///
164 /// returns: RgbGradient
165 ///
166 /// # Examples
167 ///
168 /// ```
169 /// # use color_gradient::HsvGradient;
170 /// ```
171 pub fn insert_blue(&mut self, key: f32, value: f32) {
172 let ratio = self.get_ratio(key);
173 self.blue.insert(ratio, value);
174 }
175 /// # Arguments
176 ///
177 /// * `min`:
178 /// * `max`:
179 ///
180 /// returns: RgbGradient
181 ///
182 /// # Examples
183 ///
184 /// ```
185 /// # use color_gradient::HsvGradient;
186 /// ```
187 pub fn remove_blue(&mut self, key: f32) {
188 let ratio = self.get_ratio(key);
189 self.blue.remove(ratio);
190 }
191 /// # Arguments
192 ///
193 /// * `min`:
194 /// * `max`:
195 ///
196 /// returns: RgbGradient
197 ///
198 /// # Examples
199 ///
200 /// ```
201 /// # use color_gradient::HsvGradient;
202 /// ```
203 pub fn insert_alpha(&mut self, key: f32, value: f32) {
204 let ratio = self.get_ratio(key);
205 self.alpha.insert(ratio, value);
206 }
207 /// # Arguments
208 ///
209 /// * `min`:
210 /// * `max`:
211 ///
212 /// returns: RgbGradient
213 ///
214 /// # Examples
215 ///
216 /// ```
217 /// # use color_gradient::HsvGradient;
218 /// ```
219 pub fn remove_alpha(&mut self, key: f32) {
220 let ratio = self.get_ratio(key);
221 self.alpha.remove(ratio);
222 }
223 /// # Arguments
224 ///
225 /// * `min`:
226 /// * `max`:
227 ///
228 /// returns: RgbGradient
229 ///
230 /// # Examples
231 ///
232 /// ```
233 /// # use color_gradient::HsvGradient;
234 /// ```
235 pub fn clear_alpha(&mut self) {
236 self.alpha.clear();
237 }
238}
239
240impl RgbGradient {
241 fn get_ratio(&self, value: f32) -> u16 {
242 Interpolator::get_ratio(&self.range, value)
243 }
244 /// Creates a new HSVGradient with the given min and max values.
245 ///
246 /// # Arguments
247 ///
248 /// * `min`:
249 /// * `max`:
250 ///
251 /// returns: HSVGradient
252 ///
253 /// # Examples
254 ///
255 /// ```
256 /// # use color_gradient;
257 /// ```
258 pub fn get_step(&self, value: f32) -> RGBA32 {
259 let ratio = self.get_ratio(value);
260 RGBA32 {
261 r: self.red.get_step(ratio),
262 g: self.green.get_step(ratio),
263 b: self.blue.get_step(ratio),
264 a: self.alpha.get_step(ratio),
265 }
266 }
267 /// Creates a new HSVGradient with the given min and max values.
268 ///
269 /// # Arguments
270 ///
271 /// * `min`:
272 /// * `max`:
273 ///
274 /// returns: HSVGradient
275 ///
276 /// # Examples
277 ///
278 /// ```
279 /// # use color_gradient;
280 /// ```
281 pub fn get_linear(&self, value: f32) -> RGBA32 {
282 let ratio = self.get_ratio(value);
283 RGBA32 {
284 r: self.red.get_linear(ratio),
285 g: self.green.get_linear(ratio),
286 b: self.blue.get_linear(ratio),
287 a: self.alpha.get_linear(ratio),
288 }
289 }
290}