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

use super::ryb_rotate;

/// Triad colors are three hues equidistant on the color wheel. 
///
/// When you want a design that is colorful and yet balanced, a triad color scheme might be the way to go.
#[derive(Debug, Clone)]
pub struct Triad {
    angle: f32,

    colors: Vec<Color>,
    primary_color: Color,
}

impl Triad {
    /// Generate Triad scheme with your color and 120 degree angle
    pub fn new(primary: Color) -> Self {
        Self::with_parameters(primary, Some(120.0))
    }

    /// Generate Triad scheme with your parameters
    pub fn with_parameters(primary: Color, angle: Option<f32>) -> Self {
        let mut instance = Self {
            colors: Vec::new(),
            primary_color: primary,
            angle: angle.unwrap_or(120.0),
        };
        instance.generate();

        instance
    }

    /// Retrieve angle of scheme
    pub fn angle(&self) -> f32 {
        self.angle
    }

    /// Set the angle of scheme
    pub fn set_angle(&mut self, value: f32) {
        self.angle = value;
        self.generate();
    }

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

        // value is brightness
        let mut c1: HsvColor = ryb_rotate(self.primary_color, self.angle);

        c1.value += 10.0;
        self.colors.push(c1.into());

        let mut c2: HsvColor = ryb_rotate(self.primary_color, -self.angle);

        c2.value += 10.0;
        self.colors.push(c2.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();
    }
}