forked from Ogeon/palette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient.rs
112 lines (97 loc) · 3.44 KB
/
gradient.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
extern crate image;
extern crate palette;
#[cfg(not(feature = "std"))]
fn main() {
println!("You can't use gradients without the standard library");
}
#[cfg(feature = "std")]
fn main() {
use palette::{Gradient, Lch, LinSrgb, Pixel, Srgb};
use image::{GenericImage, RgbImage};
//A gradient of evenly spaced colors
let grad1 = Gradient::new(vec![
LinSrgb::new(1.0, 0.1, 0.1),
LinSrgb::new(0.1, 0.1, 1.0),
LinSrgb::new(0.1, 1.0, 0.1),
]);
//The same colors as in grad1, but with the blue point shifted down
let grad2 = Gradient::with_domain(vec![
(0.0, LinSrgb::new(1.0, 0.1, 0.1)),
(0.25, LinSrgb::new(0.1, 0.1, 1.0)),
(1.0, LinSrgb::new(0.1, 1.0, 0.1)),
]);
//The same colors and offsets as in grad1, but in a color space where the hue
// is a component
let grad3 = Gradient::new(vec![
Lch::from(LinSrgb::new(1.0, 0.1, 0.1)),
Lch::from(LinSrgb::new(0.1, 0.1, 1.0)),
Lch::from(LinSrgb::new(0.1, 1.0, 0.1)),
]);
//The same colors and and color space as in grad3, but with the blue point
// shifted down
let grad4 = Gradient::with_domain(vec![
(0.0, Lch::from(LinSrgb::new(1.0, 0.1, 0.1))),
(0.25, Lch::from(LinSrgb::new(0.1, 0.1, 1.0))),
(1.0, Lch::from(LinSrgb::new(0.1, 1.0, 0.1))),
]);
let mut image = RgbImage::new(256, 128);
for (i, ((c1, c2), (c3, c4))) in grad1
.take(256)
.zip(grad2.take(256))
.zip(grad3.take(256).zip(grad4.take(256)))
.enumerate()
{
let c1 = Srgb::from_linear(c1).into_format().into_raw();
let c2 = Srgb::from_linear(c2).into_format().into_raw();
let c3 = Srgb::from_linear(c3.into()).into_format().into_raw();
let c4 = Srgb::from_linear(c4.into()).into_format().into_raw();
{
let mut sub_image = image.sub_image(i as u32, 0, 1, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: c1
});
}
}
}
{
let mut sub_image = image.sub_image(i as u32, 32, 1, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: c2
});
}
}
}
{
let mut sub_image = image.sub_image(i as u32, 65, 1, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: c3
});
}
}
}
{
let mut sub_image = image.sub_image(i as u32, 97, 1, 31);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
sub_image.put_pixel(x, y, image::Rgb {
data: c4
});
}
}
}
}
match image.save("examples/gradient.png") {
Ok(()) => println!("see 'examples/gradient.png' for the result"),
Err(e) => println!("failed to write 'examples/gradient.png': {}", e),
}
}