From 4451917154dd74cbdebc36e086a8d633668d59ca Mon Sep 17 00:00:00 2001 From: Tobias Ollive Date: Wed, 9 Mar 2022 18:09:26 +0100 Subject: [PATCH] add fillUnstable pattern --- src/patterns.rs | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- src/runner.rs | 1 - 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/src/patterns.rs b/src/patterns.rs index cf858dd..41eeaa7 100644 --- a/src/patterns.rs +++ b/src/patterns.rs @@ -11,6 +11,7 @@ pub(crate) enum Patterns { ColorWipe(ColorWipe), Scanner(Scanner), FillRandom(FillRandom), + FillUnstable(FillUnstable) } impl Iterator for Patterns { @@ -23,6 +24,7 @@ impl Iterator for Patterns { Patterns::ColorWipe(p) => p.next(), Patterns::Scanner(p) => p.next(), Patterns::FillRandom(p) => p.next(), + Patterns::FillUnstable(p) => p.next(), } } } @@ -311,7 +313,7 @@ impl Iterator for Scanner { /// # FillRandom /// -/// fill strip with color then apply random variation +/// 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, @@ -356,3 +358,48 @@ impl Iterator for FillRandom { Some(strip) } } + +/// # FillUnstable +/// +/// fill strip with color then apply same random variation to all pixel +#[derive(Serialize, Deserialize, Debug, Eq, Hash, PartialEq, Copy, Clone)] +pub struct FillUnstable { + pub(crate) color: PixelColor, + pub(crate) stability: usize, + pub(crate) max_iteration: Option, +} + +impl Iterator for FillUnstable { + type Item = Strip; + + fn next(&mut self) -> Option { + if let Some(iteration) = self.max_iteration { + if iteration == 0 { + return None; + } else { + self.max_iteration = Some(iteration - 1); + } + } + + let mut strip = Strip::::default(); + let mut rng = rand::thread_rng(); + let red_delta = rng.gen_range(0..self.stability) as u8; + let green_delta = rng.gen_range(0..self.stability) as u8; + let blue_delta = rng.gen_range(0..self.stability) as u8; + let operation = rng.gen_bool(0.5); + let red; + let green; + let blue; + if operation { + red = self.color.red.saturating_add(red_delta); + green = self.color.green.saturating_add(green_delta); + blue = self.color.blue.saturating_add(blue_delta); + } else { + red = self.color.red.saturating_sub(red_delta); + green = self.color.green.saturating_sub(green_delta); + blue = self.color.blue.saturating_sub(blue_delta); + } + strip.fill(PixelColor { red, green, blue }); + Some(strip) + } +} diff --git a/src/runner.rs b/src/runner.rs index 998626e..57fd036 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -21,7 +21,6 @@ pub async fn runner_loop(runner: DeviceSequence, tx: Sender