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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
use rand::Rng;

/// These are the percentages used to
/// make colors darker or lighter
const PERCENT_1: f64 = 10.0;
const PERCENT_2: f64 = 20.0;
const PERCENT_3: f64 = 30.0;
const PERCENT_4: f64 = 40.0;
const PERCENT_5: f64 = 50.0;

/// Gets an RGB tuple from a color name.
/// 
/// The input is lowercased and the whitespaces are removed.
/// 
/// So "Light Blue" will match "lightblue".
/// 
/// A fallback RGB tuple must be provided.
/// 
/// # Example
/// 
/// ```
/// use colorskill::color_name_to_rgb;
/// let c = color_name_to_rgb("firebrick", (0, 0, 0));
/// ```
pub fn color_name_to_rgb(name: &str, fallback: (u8, u8, u8)) -> (u8, u8, u8)
{
    let cname = clean_string(name);

    match &cname[..]
    {
        "maroon" => (128,0,0),
        "darkred" => (139,0,0),
        "brown" => (165,42,42),
        "firebrick" => (178,34,34),
        "crimson" => (220,20,60),
        "red" => (255,0,0),
        "tomato" => (255,99,71),
        "coral" => (255,127,80),
        "indianred" => (205,92,92),
        "lightcoral" => (240,128,128),
        "darksalmon" => (233,150,122),
        "salmon" => (250,128,114),
        "lightsalmon" => (255,160,122),
        "orangered" => (255,69,0),
        "darkorange" => (255,140,0),
        "orange" => (255,165,0),
        "gold" => (255,215,0),
        "darkgoldenrod" => (184,134,11),
        "goldenrod" => (218,165,32),
        "palegoldenrod" => (238,232,170),
        "darkkhaki" => (189,183,107),
        "khaki" => (240,230,140),
        "olive" => (128,128,0),
        "yellow" => (255,255,0),
        "yellowgreen" => (154,205,50),
        "darkolivegreen" => (85,107,47),
        "olivedrab" => (107,142,35),
        "lawngreen" => (124,252,0),
        "chartreuse" => (127,255,0),
        "greenyellow" => (173,255,47),
        "darkgreen" => (0,100,0),
        "green" => (0,128,0),
        "forestgreen" => (34,139,34),
        "lime" => (0,255,0),
        "limegreen" => (50,205,50),
        "lightgreen" => (144,238,144),
        "palegreen" => (152,251,152),
        "darkseagreen" => (143,188,143),
        "mediumspringgreen" => (0,250,154),
        "springgreen" => (0,255,127),
        "seagreen" => (46,139,87),
        "mediumaquamarine" => (102,205,170),
        "mediumseagreen" => (60,179,113),
        "lightseagreen" => (32,178,170),
        "darkslategray" => (47,79,79),
        "teal" => (0,128,128),
        "darkcyan" => (0,139,139),
        "aqua" => (0,255,255),
        "cyan" => (0,255,255),
        "lightcyan" => (224,255,255),
        "darkturquoise" => (0,206,209),
        "turquoise" => (64,224,208),
        "mediumturquoise" => (72,209,204),
        "paleturquoise" => (175,238,238),
        "aquamarine" => (127,255,212),
        "powderblue" => (176,224,230),
        "cadetblue" => (95,158,160),
        "steelblue" => (70,130,180),
        "cornflowerblue" => (100,149,237),
        "deepskyblue" => (0,191,255),
        "dodgerblue" => (30,144,255),
        "lightblue" => (173,216,230),
        "skyblue" => (135,206,235),
        "lightskyblue" => (135,206,250),
        "midnightblue" => (25,25,112),
        "navy" => (0,0,128),
        "darkblue" => (0,0,139),
        "mediumblue" => (0,0,205),
        "blue" => (0,0,255),
        "royalblue" => (65,105,225),
        "blueviolet" => (138,43,226),
        "indigo" => (75,0,130),
        "darkslateblue" => (72,61,139),
        "slateblue" => (106,90,205),
        "mediumslateblue" => (123,104,238),
        "mediumpurple" => (147,112,219),
        "darkmagenta" => (139,0,139),
        "darkviolet" => (148,0,211),
        "darkorchid" => (153,50,204),
        "mediumorchid" => (186,85,211),
        "purple" => (128,0,128),
        "thistle" => (216,191,216),
        "plum" => (221,160,221),
        "violet" => (238,130,238),
        "magenta" => (255,0,255),
        "fuchsia" => (255,0,255),
        "orchid" => (218,112,214),
        "mediumvioletred" => (199,21,133),
        "palevioletred" => (219,112,147),
        "deeppink" => (255,20,147),
        "hotpink" => (255,105,180),
        "lightpink" => (255,182,193),
        "pink" => (255,192,203),
        "antiquewhite" => (250,235,215),
        "beige" => (245,245,220),
        "bisque" => (255,228,196),
        "blanchedalmond" => (255,235,205),
        "wheat" => (245,222,179),
        "cornsilk" => (255,248,220),
        "lemonchiffon" => (255,250,205),
        "lightgoldenrodyellow" => (250,250,210),
        "lightyellow" => (255,255,224),
        "saddlebrown" => (139,69,19),
        "sienna" => (160,82,45),
        "chocolate" => (210,105,30),
        "peru" => (205,133,63),
        "sandybrown" => (244,164,96),
        "burlywood" => (222,184,135),
        "tan" => (210,180,140),
        "rosybrown" => (188,143,143),
        "moccasin" => (255,228,181),
        "navajowhite" => (255,222,173),
        "peachpuff" => (255,218,185),
        "mistyrose" => (255,228,225),
        "lavenderblush" => (255,240,245),
        "linen" => (250,240,230),
        "oldlace" => (253,245,230),
        "papayawhip" => (255,239,213),
        "seashell" => (255,245,238),
        "mintcream" => (245,255,250),
        "slategray" => (112,128,144),
        "lightslategray" => (119,136,153),
        "lightsteelblue" => (176,196,222),
        "lavender" => (230,230,250),
        "floralwhite" => (255,250,240),
        "aliceblue" => (240,248,255),
        "ghostwhite" => (248,248,255),
        "honeydew" => (240,255,240),
        "ivory" => (255,255,240),
        "azure" => (240,255,255),
        "snow" => (255,250,250),
        "black" => (0,0,0),
        "dimgray" => (105,105,105),
        "dimgrey" => (105,105,105),
        "gray" => (128,128,128),
        "grey" => (128,128,128),
        "darkgray" => (169,169,169),
        "darkgrey" => (169,169,169),
        "silver" => (192,192,192),
        "lightgray" => (211,211,211),
        "lightgrey" => (211,211,211),
        "gainsboro" => (220,220,220),
        "whitesmoke" => (245,245,245),
        "white" => (255,255,255),
        _ => fallback
    }
}

/// Checks if a color name exists.
/// 
/// # Example
/// 
/// ```
/// use colorskill::check_color_name;
/// let exists = check_color_name("silver");
/// ```
pub fn check_color_name(name: &str) -> bool
{
    let cname = clean_string(name);
    if cname == "white" {return true}
    let rgb = color_name_to_rgb(name, (255, 255, 255));
    rgb != (255, 255, 255)
}

/// Turns a color darker or lighter.
/// 
/// The amount represents a HSL lightness percentage
/// to increase or decrease.
/// 
/// 
/// The bigger the amount, the more it gets
/// darker or lighter.
/// 
/// # Example
/// 
/// ```
/// use colorskill::change_color_lightness;
/// let c = change_color_lightness((43, 56, 84), true, 20.0);
/// ```
pub fn change_color_lightness(t: (u8, u8, u8), darker: bool, amount: f64) -> (u8, u8, u8)
{
    // Convert to HSL
    let mut hsl = get_hsl(t);
    let current_lightness = hsl.get_lightness();

    if darker
    {
        // Decrease lightness by the specified percentage
        let mut lightness = current_lightness - amount;
        if lightness < 0.0 {lightness = 0.0}
        hsl.set_lightness(lightness);
    }

    else
    {
        // Increase lightness by the specified percentage
        let mut lightness = current_lightness + amount;
        if lightness > 100.0 {lightness = 100.0}
        hsl.set_lightness(lightness);
    }

    get_rgb_tuple_from_hsl(&hsl)
}

/// Wrapper function to make a color darker.
/// 
/// Receives a tuple and the amount to make darker.
/// 
/// # Example
/// 
/// ```
/// use colorskill::make_color_darker;
/// let c = make_color_darker((43, 56, 84), 20.0);
/// ```
pub fn make_color_darker(t: (u8, u8, u8), amount: f64) -> (u8, u8, u8)
{
    change_color_lightness(t, true, amount)
}

/// Wrapper function to make a color lighter.
/// 
/// Receives a tuple and the amount to make lighter.
/// 
/// # Example
/// 
/// ```
/// use colorskill::make_color_lighter;
/// let c = make_color_lighter((43, 56, 84), 20.0);
/// ```
pub fn make_color_lighter(t: (u8, u8, u8), amount: f64) -> (u8, u8, u8)
{
    change_color_lightness(t, false, amount)
}

/// Converts the RGB to a HSL
/// and returns the hue value.
/// 
/// # Example
/// 
/// use colorskill::get_color_hue;
/// let hue = get_color_hue((34, 84, 39));
pub fn get_color_hue(t: (u8, u8, u8)) -> f64
{
    let hsl = get_hsl(t);
    round_float(hsl.get_hue())
}

/// Converts the RGB to a HSL
/// and returns the saturation value.
/// 
/// # Example
/// 
/// use colorskill::get_color_saturation;
/// let saturation = get_color_saturation((34, 84, 39));
pub fn get_color_saturation(t: (u8, u8, u8)) -> f64
{
    let hsl = get_hsl(t);
    round_float(hsl.get_saturation())
}

/// Converts the RGB to a HSL
/// and returns the lightness value.
/// 
/// # Example
/// 
/// use colorskill::get_color_lightness;
/// let lightness = get_color_lightness((34, 84, 39));
pub fn get_color_lightness(t: (u8, u8, u8)) -> f64
{
    let hsl = get_hsl(t);
    round_float(hsl.get_lightness())
}

/// Generates a random RGB tuple.
/// 
/// # Example
/// 
/// ```
/// use colorskill::random_color;
/// let c = random_color();
/// ```
pub fn random_color() -> (u8, u8, u8)
{
    let mut rng = rand::thread_rng();
    let mut v: Vec<u8> = vec![];

    for _ in 1..=3
    {
        let n: u8 = rng.gen(); v.push(n);
    }

    (v[0], v[1], v[2])
}

/// Converts an RGB tuple
/// into a comma separated string.
/// 
/// (0, 0, 0) -> "0,0,0"
/// 
/// # Example
/// 
/// ```
/// use colorskill::color_to_string;
/// let cs = color_to_string((100, 143, 49));
/// ```
pub fn color_to_string(c: (u8, u8, u8)) -> String
{
    format!("{},{},{}", c.0, c.1, c.2)
}

/// Converts an RGB tuple
/// into a comma separated string
/// with spaces after commas.
/// 
/// (0, 0, 0) -> "0, 0, 0"
/// 
/// # Example
/// 
/// ```
/// use colorskill::color_to_string_2;
/// let cs = color_to_string_2((100, 143, 49));
/// ```
pub fn color_to_string_2(c: (u8, u8, u8)) -> String
{
    format!("{}, {}, {}", c.0, c.1, c.2)
}

/// Converts an RGB tuple
/// into a comma separated string
/// with added parenthesis.
/// 
/// Takes a prepend argument
/// to add a string to the start.
/// 
/// (0, 0, 0) -> "(0,0,0)"
/// (0, 0, 0) -> "RGB(0,0,0)"
/// 
/// # Example
/// 
/// ```
/// use colorskill::color_to_string_3;
/// let cs = color_to_string_3((100, 143, 49), "RGB");
/// ```
pub fn color_to_string_3(c: (u8, u8, u8), prepend: &str) -> String
{
    format!("{}({},{},{})", prepend, c.0, c.1, c.2)
}

/// Converts an RGB tuple
/// into a comma separated string
/// with added parenthesis
/// and spaces after commas.
/// 
/// Takes a prepend argument
/// to add a string to the start.
/// 
/// (0, 0, 0) -> "(0, 0, 0)"
/// (0, 0, 0) -> "RGB(0, 0, 0)"
/// 
/// # Example
/// 
/// ```
/// use colorskill::color_to_string_4;
/// let cs = color_to_string_4((100, 143, 49), "RGB");
/// ```
pub fn color_to_string_4(c: (u8, u8, u8), prepend: &str) -> String
{
    format!("{}({}, {}, {})", prepend, c.0, c.1, c.2)
}

/// Parses a color string.
/// 
/// Useful for interpreting user input.
/// 
/// Valid inputs can be:
/// 
/// "red", "0,0,0", "0, 0, 0",
/// 
/// "darker", "darker2", "darker3",
/// 
/// "darker4", "darker5",
/// 
/// "lighter", "lighter2", "lighter3",
/// 
/// "lighter4", "lighter5",
/// 
/// or "random".
/// 
/// The input is lowercased and the whitespaces are removed.
/// 
/// darker3 turns it 3 times darker than darker.
/// 
/// Percentages for darker and lighter are hardcoded:
/// 
/// PERCENT_1: f64 = 10.0;
/// 
/// PERCENT_2: f64 = 20.0;
/// 
/// And so on... up to PERCENT_5.
/// 
/// # Examples
/// 
/// ```
/// use colorskill::parse_color;
/// let c = parse_color("blue", (0, 0, 0));
/// let c = parse_color("34,65,39", (0, 0, 0));
/// let c = parse_color("darker", (10, 34, 50));
/// let c = parse_color("lighter3", (210, 87, 130));
/// let c = parse_color("random", (0, 0, 0));
/// ```
pub fn parse_color(s: &str, reference: (u8, u8, u8)) -> (u8, u8, u8)
{
    let cs = clean_string(s);

    match &cs[..]
    {
        // Check if color should be darker or lighter
        "darker" | "darker1" => make_color_darker(reference, PERCENT_1),
        "darker2" => make_color_darker(reference, PERCENT_2),
        "darker3" => make_color_darker(reference, PERCENT_3),
        "darker4" => make_color_darker(reference, PERCENT_4),
        "darker5" => make_color_darker(reference, PERCENT_5),
        "lighter" | "lighter1" => make_color_lighter(reference, PERCENT_1),
        "lighter2" => make_color_lighter(reference, PERCENT_2),
        "lighter3" => make_color_lighter(reference, PERCENT_3),
        "lighter4" => make_color_lighter(reference, PERCENT_4),
        "lighter5" => make_color_lighter(reference, PERCENT_5),
        "random" => random_color(),
        _ => 
        {
            // If not then check if it's an RGB value
            if cs.contains(',')
            {
                let v: Vec<u8> = cs.split(',')
                    .map(|n| n.parse::<u8>().unwrap_or(0)).collect();

                if v.len() != 3 {return reference} (v[0], v[1], v[2])
            }

            else
            {
                // If not then check if it's a color name
                color_name_to_rgb(&cs, reference)
            }
        }
    }
}

// Structs

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()));
    }
}

// Utility Functions

// Lowercase and remove whitespace
fn clean_string(s: &str) -> String
{
    s.to_lowercase().replace(" ", "")
}

// Converts a tuple to a colorsys Hsl
fn get_hsl(t: (u8, u8, u8)) -> colorsys::Hsl
{
    let rgb = colorsys::Rgb::from
    (
        (
            f64::from(t.0),
            f64::from(t.1),
            f64::from(t.2)
        )
    );

    colorsys::Hsl::from(&rgb)
}

// Returns an RGB tuple from an Hsl
fn get_rgb_tuple_from_hsl(hsl: &colorsys::Hsl) -> (u8, u8, u8)
{
    // Convert to RGB
    let rgb = colorsys::Rgb::from(hsl);

    // Return RGB tuple
    (
        rgb.get_red().round() as u8, 
        rgb.get_green().round() as u8, 
        rgb.get_blue().round() as u8
    )
}

// Rounds a float to 2 decimal numbers
fn round_float(n: f64) -> f64
{
    (n * 100.0).round() / 100.0
}

// Unit Tests

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

    #[test]
    fn name_test()
    {
        assert_eq!(color_name_to_rgb("tomato", (0, 0, 0)), (255, 99, 71));
        assert_eq!(color_name_to_rgb("Tomato", (0, 0, 0)), (255, 99, 71));
        assert_eq!(color_name_to_rgb("TomAto", (0, 0, 0)), (255, 99, 71));
        assert_eq!(color_name_to_rgb("tom ato", (0, 0, 0)), (255, 99, 71));
        assert_eq!(color_name_to_rgb("Tom atO", (0, 0, 0)), (255, 99, 71));
        assert_eq!(color_name_to_rgb("Tom   Ato", (0, 0, 0)), (255, 99, 71));
        assert_eq!(color_name_to_rgb("InvalidColor", (10, 10, 10)), (10, 10, 10));
    }

    #[test]
    fn name_exists_test()
    {
        assert_eq!(check_color_name("red"), true);
        assert_eq!(check_color_name("Golden Rod"), true);
        assert_eq!(check_color_name("wHi   te"), true);
        assert_eq!(check_color_name("InvalidColor"), false);
    }

    #[test]
    fn string_test()
    {
        assert_eq!(color_to_string((255, 99, 71)), "255,99,71".to_string());
        assert_eq!(color_to_string_2((255, 99, 71)), "255, 99, 71".to_string());
        assert_eq!(color_to_string_3((255, 99, 71), ""), "(255,99,71)".to_string());
        assert_eq!(color_to_string_3((255, 99, 71), "RGB"), "RGB(255,99,71)".to_string());
        assert_eq!(color_to_string_4((255, 99, 71), ""), "(255, 99, 71)".to_string());
        assert_eq!(color_to_string_4((255, 99, 71), "RGB"), "RGB(255, 99, 71)".to_string());
    }

    #[test]
    fn random_test()
    {
        random_color();
    }

    #[test]
    fn parse_test()
    {
        assert_eq!(parse_color("cadetblue", (0, 0, 0)), (95, 158, 160));

        assert_eq!(parse_color("darker", (95, 158, 160)), (76, 126, 128));
        assert_eq!(parse_color("darker1", (95, 158, 160)), (76, 126, 128));
        assert_eq!(parse_color("darker2", (95, 158, 160)), (57, 95, 96));
        assert_eq!(parse_color("darker3", (95, 158, 160)), (38, 63, 64));
        assert_eq!(parse_color("darker4", (95, 158, 160)), (19, 32, 32));
        assert_eq!(parse_color("darker5", (95, 158, 160)), (0, 0, 0));

        assert_eq!(parse_color("lighter", (95, 158, 160)), (127, 177, 179));
        assert_eq!(parse_color("lighter1", (95, 158, 160)), (127, 177, 179));
        assert_eq!(parse_color("lighter2", (95, 158, 160)), (159, 197, 198));
        assert_eq!(parse_color("lighter3", (95, 158, 160)), (191, 216, 217));
        assert_eq!(parse_color("lighter4", (95, 158, 160)), (223, 236, 236));
        assert_eq!(parse_color("lighter5", (95, 158, 160)), (255, 255, 255));
    }

    #[test]
    fn lightness_test()
    {
        assert_eq!(make_color_darker((107, 142, 35), 30.0), (15, 19, 5));
        assert_eq!(make_color_lighter((107, 142, 35), 30.0), (184, 219, 111));
        assert_eq!(change_color_lightness((184,134,11), true, 15.0), (112, 81, 7));
        assert_eq!(change_color_lightness((184,134,11), false, 15.0), (242, 180, 30));
    }

    #[test]
    fn hsl_test()
    {
        assert_eq!(get_color_hue((120, 239, 64)), 100.80);
        assert_eq!(get_color_saturation((120, 239, 64)), 84.54);
        assert_eq!(get_color_lightness((120, 239, 64)), 59.41);
    }

    #[test]
    fn tuple_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();
    }
}