primitives/foundation/colorschemes/
complementary.rs1#![allow(unused_imports)]
2use crate::foundation::colorspace::{Color, HsvColor};
3
4use super::ryb_rotate;
5
6#[derive(Debug, Clone)]
11pub struct Complementary {
12 colors: Vec<Color>,
13 primary_color: Color,
14}
15
16impl Complementary {
17 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 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 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 let complement: HsvColor = ryb_rotate(self.primary_color, 180.0);
52 self.colors.push(complement.into());
53
54 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 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 pub fn num_of_colors(&self) -> usize {
74 self.colors.len()
75 }
76
77 pub fn get_color(&self, index: usize) -> Option<Color> {
79 self.colors.get(index).copied()
80 }
81
82 pub fn primary_color(&self) -> Color {
84 self.primary_color
85 }
86
87 pub fn set_primary_color(&mut self, val: Color) {
89 self.primary_color = val;
90 self.generate();
91 }
92}