56 lines
1.4 KiB
Rust
56 lines
1.4 KiB
Rust
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<const N: usize> {
|
|
pub(crate) color: PixelColor,
|
|
pub(crate) stability: usize,
|
|
pub(crate) max_iteration: Option<usize>,
|
|
#[serde(default)]
|
|
pub(crate) current_iteration: usize,
|
|
}
|
|
|
|
impl<const N: usize> Pattern for FillRandom<N> {
|
|
type Strip = Strip<N>;
|
|
|
|
fn init(&mut self) -> Option<Self::Strip> {
|
|
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<const N: usize> Iterator for FillRandom<N> {
|
|
type Item = Strip<N>;
|
|
|
|
fn next(&mut self) -> Option<Self::Item> {
|
|
if self.is_last_iteration() {
|
|
return None;
|
|
}
|
|
|
|
let mut strip = Strip::<N>::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)
|
|
}
|
|
}
|