-
Notifications
You must be signed in to change notification settings - Fork 59
/
random.rs
116 lines (104 loc) · 3.86 KB
/
random.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
#[cfg(not(feature = "random"))]
fn main() {
println!("You can't use the `rand` integration without the \"random\" feature");
}
#[cfg(feature = "random")]
fn main() {
use palette::{FromColor, Hsl, Hsv, Hwb, Pixel, RgbHue, Srgb};
use image::{GenericImage, GenericImageView, RgbImage};
use rand::Rng;
let mut image = RgbImage::new(512, 256);
let mut rng = rand::rngs::ThreadRng::default();
// RGB
{
let mut sub_image = image.sub_image(0, 0, 128, 128);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let random_color = Srgb::<f32>::new(rng.gen(), rng.gen(), rng.gen());
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
}
}
}
{
let mut sub_image = image.sub_image(0, 128, 128, 128);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let random_color = rng.gen::<Srgb>();
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
}
}
}
// HSV
{
let mut sub_image = image.sub_image(128, 0, 128, 128);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let random_color =
Srgb::from_color(Hsv::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()));
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
}
}
}
{
let mut sub_image = image.sub_image(128, 128, 128, 128);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let random_color = Srgb::from_color(rng.gen::<Hsv>());
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
}
}
}
// HSL
{
let mut sub_image = image.sub_image(256, 0, 128, 128);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let random_color =
Srgb::from_color(Hsl::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()));
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
}
}
}
{
let mut sub_image = image.sub_image(256, 128, 128, 128);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let random_color = Srgb::from_color(rng.gen::<Hsl>());
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
}
}
}
// HWB
{
let mut sub_image = image.sub_image(384, 0, 128, 128);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let random_color =
Srgb::from_color(Hwb::new(rng.gen::<RgbHue>(), rng.gen(), rng.gen()));
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
}
}
}
{
let mut sub_image = image.sub_image(384, 128, 128, 128);
let (width, height) = sub_image.dimensions();
for x in 0..width {
for y in 0..height {
let random_color = Srgb::from_color(rng.gen::<Hwb>());
sub_image.put_pixel(x, y, image::Rgb(random_color.into_format().into_raw()));
}
}
}
let _ = std::fs::create_dir("example-data/output");
match image.save("example-data/output/random.png") {
Ok(()) => println!("see 'example-data/output/random.png' for the result"),
Err(e) => println!("failed to write 'example-data/output/random.png': {}", e),
}
}