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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
extern crate rand;
use rand::prelude::*;
mod color;
pub use color::Color;
pub struct ColorPalette {
pub colors: Vec<Color>
}
pub enum PaletteType {
Random,
Pastel,
Dark,
}
impl ColorPalette {
pub fn new(count: u32, palette_type: PaletteType, adjacent_colors: bool) -> ColorPalette {
let mut rng = rand::thread_rng();
let mut hue: f32;
let mut saturation: f32;
let mut value: f32;
match palette_type {
PaletteType::Random => {
hue = rng.gen_range(0.0, 360.0);
saturation = rng.gen_range(0.5, 1.0);
value = rng.gen_range(0.3, 1.0);
},
PaletteType::Pastel => {
hue = rng.gen_range(0.0, 360.0);
saturation = rng.gen_range(0.1, 0.4);
value = rng.gen_range(0.7, 1.0);
},
PaletteType::Dark => {
hue = rng.gen_range(0.0, 360.0);
saturation = rng.gen_range(0.5, 1.0);
value = rng.gen_range(0.0, 0.4);
}
}
let mut palette: Vec<Color> = vec![];
let mut base_divergence = 80.0;
if adjacent_colors == true {
base_divergence = 25.0;
}
base_divergence -= (count as f32) / 2.6;
for i in 0..count {
let rgb = Color::hsv_to_rgb(hue, saturation, value);
match palette_type {
PaletteType::Random => {
ColorPalette::palette_random(&mut hue, &mut saturation, &mut value, i as f32, base_divergence);
},
PaletteType::Pastel => {
ColorPalette::palette_pastel(&mut hue, &mut saturation, &mut value, i as f32, base_divergence);
},
PaletteType::Dark => {
ColorPalette::palette_dark(&mut hue, &mut saturation, &mut value, i as f32, base_divergence);
}
}
palette.push(Color {
red: rgb.red,
green: rgb.green,
blue: rgb.blue
});
}
ColorPalette {
colors: palette
}
}
fn palette_dark(hue: &mut f32, saturation: &mut f32, value: &mut f32, iteration: f32, divergence: f32) {
let f = (iteration * 43.0).cos().abs();
let mut div = divergence;
if div < 15.0 {
div = 15.0;
}
*hue = (*hue + div + f).abs() % 360.0;
*saturation = 0.32 + ((iteration * 0.75).sin() / 2.0).abs();
*value = 0.1 + (iteration.cos() / 6.0).abs();
}
fn palette_pastel(hue: &mut f32, saturation: &mut f32, value: &mut f32, iteration: f32, divergence: f32) {
let f = (iteration * 25.0).cos().abs();
let mut div = divergence;
if div < 15.0 {
div = 15.0;
}
*hue = (*hue + div + f).abs() % 360.0;
*saturation = ((iteration * 0.35).cos() / 5.0).abs();
*value = 0.5 + (iteration.cos() / 2.0).abs();
}
fn palette_random(hue: &mut f32, saturation: &mut f32, value: &mut f32, iteration: f32, divergence: f32) {
let f = (iteration * 55.0).tan().abs();
let mut div = divergence;
if div < 15.0 {
div = 15.0;
}
*hue = (*hue + div + f).abs() % 360.0;
*saturation = (iteration * 0.35).sin().abs();
*value = ((6.33 * iteration) * 0.5).cos().abs();
if *saturation < 0.4 {
*saturation = 0.4;
}
if *value < 0.2 {
*value = 0.2;
} else if *value > 0.85 {
*value = 0.85;
}
}
}
#[cfg(test)]
mod tests {
use super::ColorPalette;
use super::PaletteType;
#[test]
fn generates_palette() {
let palette = ColorPalette::new(7, PaletteType::Random, false);
for color in palette.colors {
assert!(color.red >= 0.0);
assert!(color.red <= 1.0);
assert!(color.green >= 0.0);
assert!(color.green <= 1.0);
assert!(color.blue >= 0.0);
assert!(color.blue <= 1.0);
}
}
}