logo
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
#![allow(unused_imports)]
use crate::foundation::colorspace::{Color, HsvColor};

use super::ryb_rotate;

/// The complementary colors are the colors which are directly opposite from one another on the color wheel. 
///
/// Complementary colors are contrasting and stand out against each other. 
/// Often it is a good idea to use a complementary color as the highlight color, as described above.
#[derive(Debug, Clone)]
pub struct Complementary {
    colors: Vec<Color>,
    primary_color: Color,
}

impl Complementary {
    /// Generate Complementary scheme with your color
    pub fn new(primary: Color) -> Self {
        let mut instance = Self {
            colors: Vec::new(),
            primary_color: primary,
        };
        instance.generate();

        instance
    }

    fn generate(&mut self) {
        self.colors = vec![self.primary_color];

        let primary_hsb: HsvColor = self.primary_color.into();

        let mut contrasting: HsvColor = primary_hsb;

        // value is brightness
        if primary_hsb.value > 40.0 {
            contrasting.value = 10.0 + primary_hsb.value * 0.25;
        } else {
            contrasting.value = 100.0 - primary_hsb.value * 0.25;
        }
        self.colors.push(contrasting.into());

        // supporting
        let mut supporting: HsvColor = primary_hsb;

        supporting.value = 30.0 + primary_hsb.value;
        supporting.saturation = 10.0 + primary_hsb.saturation * 0.3;
        self.colors.push(supporting.into());

        // complement
        let complement: HsvColor = ryb_rotate(self.primary_color, 180.0);
        self.colors.push(complement.into());

        // contrasting complement
        let mut contrasting_complement: HsvColor = complement.clone();

        if complement.value > 30.0 {
            contrasting_complement.value = 10.0 + complement.value * 0.25;
        } else {
            contrasting_complement.value = 100.0 - complement.value * 0.25;
        }
        self.colors.push(contrasting_complement.into());

        // supporting complement
        let mut supporting_complement: HsvColor = complement;

        supporting_complement.value = 30.0 + complement.value;
        supporting_complement.saturation = 10.0 + complement.saturation * 0.3;
        self.colors.push(supporting_complement.into());
    }

    /// Retrieve count colors of scheme
    pub fn num_of_colors(&self) -> usize {
        self.colors.len()
    }

    /// Set color by index
    pub fn get_color(&self, index: usize) -> Option<Color> {
        self.colors.get(index).copied()
    }

    /// Retrieve primary color of scheme
    pub fn primary_color(&self) -> Color {
        self.primary_color
    }

    /// Set the primary color of scheme
    pub fn set_primary_color(&mut self, val: Color) {
        self.primary_color = val;
        self.generate();
    }
}