Skip to main content

primitives/foundation/colorschemes/
split_complementary.rs

1#![allow(unused_imports)]
2use crate::foundation::colorspace::{Color, HsvColor};
3
4use super::ryb_rotate;
5
6/// Split complementary is a color and the analogous colors to its complement color. 
7///
8/// Using split complementary colors can give you a design with a high degree of contrast, 
9/// yet still not as extreme as a real complementary color. 
10/// It also results in greater harmony than the use of the direct complementary.
11#[derive(Debug, Clone)]
12pub struct SplitComplementary {
13    colors: Vec<Color>,
14    primary_color: Color,
15}
16
17impl SplitComplementary {
18    /// Generate SplitComplementary scheme with your color
19    pub fn new(primary: Color) -> Self {
20        let mut instance = Self {
21            colors: Vec::new(),
22            primary_color: primary,
23        };
24        instance.generate();
25
26        instance
27    }
28
29    fn generate(&mut self) {
30        self.colors = vec![self.primary_color];
31
32        // value is brightness
33        let mut c1: HsvColor = ryb_rotate(self.primary_color, 150.0);
34        c1.value += 10.0;
35        self.colors.push(c1.into());
36
37        let mut c2: HsvColor = ryb_rotate(self.primary_color, 210.0);
38        c2.value += 10.0;
39        self.colors.push(c2.into());
40    }
41
42    /// Retrieve count colors of scheme
43    pub fn num_of_colors(&self) -> usize {
44        self.colors.len()
45    }
46
47    /// Set color by index
48    pub fn get_color(&self, index: usize) -> Option<Color> {
49        self.colors.get(index).copied()
50    }
51
52    /// Retrieve primary color of scheme
53    pub fn primary_color(&self) -> Color {
54        self.primary_color
55    }
56
57    /// Set the primary color of scheme
58    pub fn set_primary_color(&mut self, val: Color) {
59        self.primary_color = val;
60        self.generate();
61    }
62}