diff --git a/palette/examples/random.rs b/palette/examples/random.rs new file mode 100644 index 000000000..486cbcac8 --- /dev/null +++ b/palette/examples/random.rs @@ -0,0 +1,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::::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::(); + 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::(), 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::()); + 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::(), 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::()); + 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::(), 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::()); + 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), + } +}