1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use crate::*;

pub struct RGB
{
    red: u8,
    green: u8,
    blue: u8
}

impl RGB
{
    /// Makes a new RGB from three u8 values.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// ```
    pub fn new(red: u8, green: u8, blue: u8) -> RGB
    {
        RGB
        {
            red, green, blue
        }
    }

    /// Makes a new RGB from a u8 tuple.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::from_tuple((22, 95, 83));
    /// ```
    pub fn from_tuple(t: (u8, u8, u8)) -> RGB
    {
        RGB
        {
            red: t.0,
            green: t.1,
            blue: t.2
        }
    }

    /// Gets the red RGB value.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let red = c.get_red();
    /// ```
    pub fn get_red(&self) -> u8
    {
        self.red
    }

    /// Gets the green RGB value.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let green = c.get_green();
    /// ```
    pub fn get_green(&self) -> u8
    {
        self.green
    }

    /// Gets the blue RGB value.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let blue = c.get_blue();
    /// ```
    pub fn get_blue(&self) -> u8
    {
        self.blue
    }

    /// Sets the red RGB value.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let mut c = RGB::new(34, 66, 94);
    /// c.set_red(80);
    /// ```
    pub fn set_red(&mut self, value: u8)
    {
        self.red = value;
    }

    /// Sets the green RGB value.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let mut c = RGB::new(34, 66, 94);
    /// c.set_green(80);
    /// ```
    pub fn set_green(&mut self, value: u8)
    {
        self.green = value;
    }

    /// Sets the blue RGB value.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let mut c = RGB::new(34, 66, 94);
    /// c.set_blue(80);
    /// ```
    pub fn set_blue(&mut self, value: u8)
    {
        self.blue = value;
    }

    /// Sets all the RGB values from a  tuple.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let mut c = RGB::new(34, 66, 94);
    /// c.set_from_tuple((19, 23, 129));
    /// ```
    pub fn set_from_tuple(&mut self, t: (u8, u8, u8))
    {
        self.set_red(t.0); self.set_green(t.1); self.set_blue(t.2);
    }

    /// Gets the RGB values in a tuple.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let t = c.get_tuple();
    /// ```
    pub fn get_tuple(&self) -> (u8, u8, u8)
    {
        (self.get_red(), self.get_green(), self.get_blue())
    }

    /// Makes the RGB darker by an amount.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let mut c = RGB::new(34, 66, 94);
    /// c.make_darker(20.0);
    /// ```
    pub fn make_darker(&mut self, amount: f64)
    {
        self.set_from_tuple(make_color_darker(self.get_tuple(), amount));
    }

    /// Makes the RGB lighter by an amount.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let mut c = RGB::new(34, 66, 94);
    /// c.make_lighter(20.0);
    /// ```
    pub fn make_lighter(&mut self, amount: f64) 
    {
        self.set_from_tuple(make_color_lighter(self.get_tuple(), amount));
    }

    /// Randomizes the RGB values.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let mut c = RGB::new(34, 66, 94);
    /// c.randomize();
    /// ```
    pub fn randomize(&mut self)
    {
        self.set_from_tuple(random_color());
    }

    /// Returns the HSL hue value
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let hue = c.get_hue();
    /// ```
    pub fn get_hue(&self) -> f64
    {
        get_color_hue(self.get_tuple())
    }

    /// Returns the HSL saturation value
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let saturation = c.get_saturation();
    /// ```
    pub fn get_saturation(&self) -> f64
    {
        get_color_saturation(self.get_tuple())
    }

    /// Returns the HSL lightness value
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let lightness = c.get_lightness();
    /// ```
    pub fn get_lightness(&self) -> f64
    {
        get_color_lightness(self.get_tuple())
    }

    /// Turns the RGB into a string.
    /// 
    /// See the to_string definition 
    /// to check what the output is.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let s = c.to_string();
    /// ```
    pub fn to_string(&self) -> String
    {
        color_to_string(self.get_tuple())
    }

    /// Turns the RGB into a string.
    /// 
    /// See the to_string_2 definition 
    /// to check what the output is.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let s = c.to_string_2();
    /// ```
    pub fn to_string_2(&self) -> String
    {
        color_to_string_2(self.get_tuple())
    }

    /// Turns the RGB into a string.
    /// 
    /// See the to_string_3 definition 
    /// to check what the output is.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let s = c.to_string_3("RGB");
    /// ```
    pub fn to_string_3(&self, prepend: &str) -> String
    {
        color_to_string_3(self.get_tuple(), prepend)
    }

    /// Turns the RGB into a string.
    /// 
    /// See the to_string_4 definition 
    /// to check what the output is.
    /// 
    /// # Example
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let c = RGB::new(34, 66, 94);
    /// let s = c.to_string_4("RGB");
    /// ```
    pub fn to_string_4(&self, prepend: &str) -> String
    {
        color_to_string_4(self.get_tuple(), prepend)
    }

    /// Uses the parse function to 
    /// change the values of the RGB.
    /// 
    /// Check the parse_color definition
    /// to check how to use it.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use colorskill::RGB;
    /// let mut c = RGB::new(34, 66, 94);
    /// c.change("red");
    /// c.change("46,39,199");
    /// c.change("darker");
    /// c.change("lighter");
    /// c.change("random");
    /// ```
    pub fn change(&mut self, s: &str)
    {
        self.set_from_tuple(parse_color(s, self.get_tuple()));
    }
}

// Unit Tests

#[cfg(test)]
mod tests 
{
    use super::*;

    #[test]
    fn rgb_test()
    {
        let mut c = RGB::new(0, 0, 0);

        c.make_lighter(45.0);
        assert_eq!(c.get_red(), 115);
        assert_eq!(c.get_green(), 115);
        assert_eq!(c.get_blue(), 115);

        c.change("red");
        c.make_darker(45.0);
        assert_eq!(c.get_red(), 26);
        assert_eq!(c.get_green(), 0);
        assert_eq!(c.get_blue(), 0);

        c.set_red(100); 
        c.set_green(83); 
        c.set_blue(193);
        assert_eq!(c.get_red(), 100);
        assert_eq!(c.get_green(), 83);
        assert_eq!(c.get_blue(), 193);

        c.set_from_tuple((55, 129, 90));
        assert_eq!(c.get_red(), 55);
        assert_eq!(c.get_green(), 129);
        assert_eq!(c.get_blue(), 90);

        let t = c.get_tuple();
        assert_eq!(t.0, 55);
        assert_eq!(t.1, 129);
        assert_eq!(t.2, 90);

        assert_eq!(c.to_string(), "55,129,90");
        assert_eq!(c.to_string_2(), "55, 129, 90");
        assert_eq!(c.to_string_3(""), "(55,129,90)");
        assert_eq!(c.to_string_3("RGB"), "RGB(55,129,90)");
        assert_eq!(c.to_string_4(""), "(55, 129, 90)");
        assert_eq!(c.to_string_4("RGB"), "RGB(55, 129, 90)");

        let mut c2 = RGB::from_tuple((100, 90, 89));

        c2.change("lighter2");
        assert_eq!(c2.get_red(), 152);
        assert_eq!(c2.get_green(), 140);
        assert_eq!(c2.get_blue(), 139);

        assert_eq!(c2.get_hue(), 4.62);
        assert_eq!(c2.get_saturation(), 5.94);
        assert_eq!(c2.get_lightness(), 57.06);

        c2.randomize();
    }
}