forked from Ogeon/palette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shade.rs
103 lines (89 loc) · 3.23 KB
/
shade.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
extern crate image;
extern crate palette;
use palette::{Hsv, Lab, LinSrgb, Pixel, Shade, Srgb};
use image::{GenericImage, RgbImage};
fn main() {
//The same color in linear RGB, CIE L*a*b*, and HSV
let rgb = LinSrgb::new(0.5, 0.0, 0.0);
let lab = Lab::from(rgb);
let hsv = Hsv::from(rgb);
let mut image = RgbImage::new(220, 193);
for i in 0..11 {
let rgb1 = Srgb::from_linear(rgb.darken(0.05 * i as f32))
.into_format()
.into_raw();
let rgb2 = Srgb::from_linear(rgb.lighten(0.05 * i as f32))
.into_format()
.into_raw();
{
let mut sub_image = image.sub_image(i as u32 * 20, 0, 20, 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: rgb1 });
}
}
}
{
let mut sub_image = image.sub_image(i as u32 * 20, 32, 20, 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: rgb2 });
}
}
}
let lab1 = Srgb::from_linear(lab.darken(0.05 * i as f32).into())
.into_format()
.into_raw();
let lab2 = Srgb::from_linear(lab.lighten(0.05 * i as f32).into())
.into_format()
.into_raw();
{
let mut sub_image = image.sub_image(i as u32 * 20, 65, 20, 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: lab1 });
}
}
}
{
let mut sub_image = image.sub_image(i as u32 * 20, 97, 20, 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: lab2 });
}
}
}
let hsv1 = Srgb::from_linear(hsv.darken(0.05 * i as f32).into())
.into_format()
.into_raw();
let hsv2 = Srgb::from_linear(hsv.lighten(0.05 * i as f32).into())
.into_format()
.into_raw();
{
let mut sub_image = image.sub_image(i as u32 * 20, 130, 20, 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: hsv1 });
}
}
}
{
let mut sub_image = image.sub_image(i as u32 * 20, 162, 20, 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: hsv2 });
}
}
}
}
match image.save("examples/shade.png") {
Ok(()) => println!("see 'examples/shade.png' for the result"),
Err(e) => println!("failed to write 'examples/shade.png': {}", e),
}
}