Preset colored noise.

This commit is contained in:
Juergen Stuber 2018-12-28 14:57:41 +01:00
parent eb7ac1c1f1
commit eff04faee4

View File

@ -1,7 +1,7 @@
use std::env::args;
use std::io::stdout;
use std::io::Write;
use std::iter::repeat;
use std::iter::repeat_with;
use std::thread::sleep;
use std::time::Duration;
@ -21,6 +21,17 @@ fn send<T: Write>(w: &mut T, f: &Frame) -> std::io::Result<()> {
w.flush()
}
fn pick_color<R>(c0: Color, c1: Color, rng: &mut R) -> Color
where
R: Rng,
{
if rng.gen::<f64>() < 0.5 {
c0.interpolate(c1, rng.gen::<f64>().powf(2.0))
} else {
Color::black()
}
}
fn main() -> std::io::Result<()> {
let args = args().collect::<Vec<_>>();
eprintln!("executing {}", args[0]);
@ -37,19 +48,17 @@ fn main() -> std::io::Result<()> {
let mut rng = thread_rng();
let mut frame = repeat(repeat(Color::black()).take(x_size).collect::<Vec<_>>())
.take(y_size)
.collect::<Vec<_>>();
let mut frame = repeat_with(|| {
repeat_with(|| pick_color(c0, c1, &mut rng))
.take(x_size)
.collect::<Vec<_>>()
})
.take(y_size)
.collect::<Vec<_>>();
loop {
let x = rng.gen_range(0, x_size);
let y = rng.gen_range(0, y_size);
let c = {
if rng.gen::<f64>() < 0.5 {
c0.interpolate(c1, rng.gen::<f64>().powf(2.0))
} else {
Color::black()
}
};
let c = pick_color(c0, c1, &mut rng);
frame[y][x] = c;
let mut buf = Vec::with_capacity(x_size * y_size * 3);