use crate::patterns::{Pattern, PixelColor}; use crate::Strip; use serde::{Deserialize, Serialize}; /// # FillRandom /// /// fill strip with color then apply random variation to each pixel #[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)] pub struct FillRandom { pub(crate) color: PixelColor, pub(crate) stability: usize, pub(crate) max_iteration: Option, #[serde(default)] pub(crate) current_iteration: usize, } impl Pattern for FillRandom { type Strip = Strip; fn init(&mut self) -> Option { None } fn is_last_iteration(&self) -> bool { match self.max_iteration { None => false, Some(max_iter) => { if self.current_iteration >= max_iter { true } else { false } } } } } impl Iterator for FillRandom { type Item = Strip; fn next(&mut self) -> Option { if self.is_last_iteration() { return None; } let mut strip = Strip::::default(); let rng = rand::thread_rng(); for i in 0..N { let c = self.color.random_delta(self.stability, &mut rng.clone()); strip[i] = Some(c); } self.current_iteration += 1; Some(strip) } }