primitives/foundation/colorschemes/
complementary.rs

1#![allow(unused_imports)]
2use crate::foundation::colorspace::{Color, HsvColor};
3
4use super::ryb_rotate;
5
6/// The complementary colors are the colors which are directly opposite from one another on the color wheel. 
7///
8/// Complementary colors are contrasting and stand out against each other. 
9/// Often it is a good idea to use a complementary color as the highlight color, as described above.
10#[derive(Debug, Clone)]
11pub struct Complementary {
12    colors: Vec<Color>,
13    primary_color: Color,
14}
15
16impl Complementary {
17    /// Generate Complementary scheme with your color
18    pub fn new(primary: Color) -> Self {
19        let mut instance = Self {
20            colors: Vec::new(),
21            primary_color: primary,
22        };
23        instance.generate();
24
25        instance
26    }
27
28    fn generate(&mut self) {
29        self.colors = vec![self.primary_color];
30
31        let primary_hsb: HsvColor = self.primary_color.into();
32
33        let mut contrasting: HsvColor = primary_hsb;
34
35        // value is brightness
36        if primary_hsb.value > 40.0 {
37            contrasting.value = 10.0 + primary_hsb.value * 0.25;
38        } else {
39            contrasting.value = 100.0 - primary_hsb.value * 0.25;
40        }
41        self.colors.push(contrasting.into());
42
43        // supporting
44        let mut supporting: HsvColor = primary_hsb;
45
46        supporting.value = 30.0 + primary_hsb.value;
47        supporting.saturation = 10.0 + primary_hsb.saturation * 0.3;
48        self.colors.push(supporting.into());
49
50        // complement
51        let complement: HsvColor = ryb_rotate(self.primary_color, 180.0);
52        self.colors.push(complement.into());
53
54        // contrasting complement
55        let mut contrasting_complement: HsvColor = complement.clone();
56
57        if complement.value > 30.0 {
58            contrasting_complement.value = 10.0 + complement.value * 0.25;
59        } else {
60            contrasting_complement.value = 100.0 - complement.value * 0.25;
61        }
62        self.colors.push(contrasting_complement.into());
63
64        // supporting complement
65        let mut supporting_complement: HsvColor = complement;
66
67        supporting_complement.value = 30.0 + complement.value;
68        supporting_complement.saturation = 10.0 + complement.saturation * 0.3;
69        self.colors.push(supporting_complement.into());
70    }
71
72    /// Retrieve count colors of scheme
73    pub fn num_of_colors(&self) -> usize {
74        self.colors.len()
75    }
76
77    /// Set color by index
78    pub fn get_color(&self, index: usize) -> Option<Color> {
79        self.colors.get(index).copied()
80    }
81
82    /// Retrieve primary color of scheme
83    pub fn primary_color(&self) -> Color {
84        self.primary_color
85    }
86
87    /// Set the primary color of scheme
88    pub fn set_primary_color(&mut self, val: Color) {
89        self.primary_color = val;
90        self.generate();
91    }
92}