rust_lum/src/patterns/fade_random.rs

61 lines
1.7 KiB
Rust

use crate::patterns::fade::Fade;
use crate::patterns::Pattern;
use crate::{impl_pattern_none, Strip};
use serde::{Deserialize, Serialize};
/// # FadeRandom
///
/// fade from one color to an other with random variations applied to each pixel
#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)]
pub struct FadeRandom<const N: usize> {
#[serde(default)]
pub(crate) current_iteration: usize,
pub(crate) max_iteration: Option<usize>,
pub(crate) stability: usize,
fade: Fade<N>,
}
impl<const N: usize> Pattern for FadeRandom<N> {
impl_pattern_none!(FadeRandom, N);
}
//
// impl<const N: usize> Pattern for FadeRandom<N> {
// type Strip = Strip<N>;
//
// 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 FadeRandom<N> {
type Item = Strip<N>;
fn next(&mut self) -> Option<Self::Item> {
let strip = self.fade.next();
match strip {
None => None,
Some(mut fade) => {
let rng = rand::thread_rng();
for i in 0..N {
match fade[i] {
None => {}
Some(pixel) => {
fade[i] = Some(pixel.random_delta(self.stability, &mut rng.clone()))
}
}
}
Some(fade)
}
}
}
}