forked from Ogeon/palette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
color_scheme.rs
210 lines (187 loc) · 6.99 KB
/
color_scheme.rs
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
extern crate clap;
extern crate image;
extern crate palette;
use palette::{Hue, Pixel, Shade, Lch, Srgb, LinSrgb};
use image::{GenericImage, RgbImage, SubImage};
use clap::{App, AppSettings, Arg, SubCommand};
const SWATCH_SIZE: u32 = 128;
fn main() {
let matches = App::new("color_scheme")
.about("Generates a very simple color scheme from an RGB8 color.")
.setting(AppSettings::ArgRequiredElseHelp)
.arg(
Arg::with_name("red")
.required(true)
.empty_values(false)
.index(1)
.help("[0-255] The red channel of the primary color."),
)
.arg(
Arg::with_name("green")
.required(true)
.empty_values(false)
.index(2)
.help("[0-255] The green channel of the primary color."),
)
.arg(
Arg::with_name("blue")
.required(true)
.empty_values(false)
.index(3)
.help("[0-255] The blue channel of the primary color."),
)
.subcommand(
SubCommand::with_name("triad")
.about("A three point scheme, centered around the complementary.")
.arg(
Arg::with_name("distance")
.help("The distance between the secondary colors.")
.long("distance")
.short("d")
.value_name("degrees")
.takes_value(true)
.empty_values(false),
),
)
.subcommand(
SubCommand::with_name("analogous")
.about("Like triad, but centered around the primary.")
.arg(
Arg::with_name("distance")
.help("The distance between the secondary colors.")
.long("distance")
.short("d")
.value_name("degrees")
.takes_value(true)
.empty_values(false),
),
)
.subcommand(
SubCommand::with_name("rectangle")
.about("A four point scheme.")
.arg(
Arg::with_name("distance")
.help("The distance to the closest secondary colors.")
.long("distance")
.short("d")
.value_name("degrees")
.takes_value(true)
.empty_values(false),
),
)
.subcommand(
SubCommand::with_name("complementary").about("A simple two point color scheme."),
)
.get_matches();
//Get the components of the primary color
let red: u8 = matches
.value_of("red")
.and_then(|r| r.parse().ok())
.expect("the red channel must be a number in the range [0-255]");
let green: u8 = matches
.value_of("green")
.and_then(|r| r.parse().ok())
.expect("the green channel must be a number in the range [0-255]");
let blue: u8 = matches
.value_of("blue")
.and_then(|r| r.parse().ok())
.expect("the blue channel must be a number in the range [0-255]");
let primary: Lch = Srgb::new(red, green, blue)
.into_format::<f32>()
.into_linear()
.into();
//Generate the secondary colors, depending on the input arguments
let secondary = match matches.subcommand() {
("triad", matches) | ("", matches) => {
//Two secondary colors that are close to the complementary, or evenly spaced
let distance: f32 = matches
.and_then(|m| m.value_of("distance"))
.and_then(|d| d.parse().ok())
.unwrap_or(120.0);
let shift = 180.0 - (distance / 2.0);
vec![
primary.shift_hue(shift),
primary.shift_hue(-shift),
]
}
("analogous", matches) => {
//Two secondary colors that are close to the primary
let distance: f32 = matches
.and_then(|m| m.value_of("distance"))
.and_then(|d| d.parse().ok())
.unwrap_or(60.0);
let shift = distance / 2.0;
vec![
primary.shift_hue(shift),
primary.shift_hue(-shift),
]
}
("rectangle", matches) => {
//Three secondary colors that forms a rectangle or a square, together with the
// primary
let distance: f32 = matches
.and_then(|m| m.value_of("distance"))
.and_then(|d| d.parse().ok())
.unwrap_or(90.0);
let shift1 = distance;
let shift2 = 180.0 + distance;
vec![
primary.shift_hue(shift1),
primary.shift_hue(180.0),
primary.shift_hue(shift2),
]
}
("complementary", _) => vec![primary.shift_hue(180.0)], /* Simply the
* complementary color */
(name, _) => panic!("unknown subcommand: {}", name),
};
//Create an image for the swatches
let mut image = RgbImage::new((secondary.len() as u32 + 1) * SWATCH_SIZE, SWATCH_SIZE);
//Draw the primary swatches
blit_shades(primary.into(), image.sub_image(0, 0, SWATCH_SIZE, SWATCH_SIZE));
//Draw the secondary swatches
for (n, color) in secondary.into_iter().enumerate() {
blit_shades(
color.into(),
image.sub_image((n as u32 + 1) * SWATCH_SIZE, 0, SWATCH_SIZE, SWATCH_SIZE),
);
}
match image.save("examples/color_scheme.png") {
Ok(()) => println!("see 'examples/color_scheme.png' for the result"),
Err(e) => println!("failed to write 'examples/color_scheme.png': {}", e),
}
}
fn blit_shades<I>(
color: LinSrgb<f32>,
mut canvas: SubImage<I>,
) where I: GenericImage<Pixel = image::Rgb<u8>> + 'static {
let width = canvas.width();
let height = canvas.height();
let primary = Srgb::from_linear(color).into_format().into_raw();
//Generate one lighter and two darker versions of the color
let light = Srgb::from_linear(color.lighten(0.1).into())
.into_format()
.into_raw();
let dark1 = Srgb::from_linear(color.darken(0.1).into())
.into_format()
.into_raw();
let dark2 = Srgb::from_linear(color.darken(0.2).into())
.into_format()
.into_raw();
for x in 0..width {
for y in 0..height {
canvas.put_pixel(x, y, image::Rgb {
data:
if y < height / 2 {
primary
} else if x < width / 3 {
light
} else if x < (width / 3) * 2 {
dark1
} else {
dark2
}
});
}
}
}