use smart_leds_trait::RGB8;
use crate::segment::{EffectConfig, EffectState};
use crate::utils::{color_blend, color_wheel, next_rand, sine8};
pub fn random_color(pixels: &mut [RGB8], state: &mut EffectState, _config: &EffectConfig) {
state.aux = next_rand(state.aux);
let color = color_wheel(state.aux as u8);
for pixel in pixels.iter_mut() {
*pixel = color;
}
}
pub fn single_dynamic(pixels: &mut [RGB8], state: &mut EffectState, _config: &EffectConfig) {
if state.counter == 0 {
let mut rng = state.aux;
for p in pixels.iter_mut() {
rng = next_rand(rng);
*p = color_wheel(rng as u8);
}
state.aux = rng;
}
let rng = next_rand(state.aux);
let idx = (rng % pixels.len() as u32) as usize;
let rng2 = next_rand(rng);
pixels[idx] = color_wheel(rng2 as u8);
state.aux = rng2;
state.counter = state.counter.wrapping_add(1);
}
pub fn multi_dynamic(pixels: &mut [RGB8], state: &mut EffectState, _config: &EffectConfig) {
let mut rng = state.aux;
for pixel in pixels.iter_mut() {
rng = next_rand(rng);
*pixel = color_wheel(rng as u8);
}
state.aux = rng;
}
pub fn running_lights(pixels: &mut [RGB8], state: &mut EffectState, config: &EffectConfig) {
let len = pixels.len();
let sine_incr = ((256 / len) as u8).max(1);
for (i, pixel) in pixels.iter_mut().enumerate() {
let phase = ((i as u32).wrapping_add(state.counter) * sine_incr as u32) as u8;
let lum = sine8(phase);
*pixel = color_blend(config.colors[0], config.colors[1], lum);
}
state.counter = state.counter.wrapping_add(1);
}
pub fn block_dissolve(pixels: &mut [RGB8], state: &mut EffectState, config: &EffectConfig) {
let len = pixels.len() as u32;
let color_idx = (state.aux & 0x3) as usize;
let target = config.colors[color_idx];
let rng = next_rand(state.aux >> 2);
let idx = (rng % len) as usize;
if pixels[idx] != target {
pixels[idx] = target;
state.aux = (state.aux & 0x3) | (rng << 2);
return;
}
let start = (rng % len) as usize;
for offset in 0..pixels.len() {
let i = (start + offset) % pixels.len();
if pixels[i] != target {
pixels[i] = target;
state.aux = (state.aux & 0x3) | (next_rand(rng) << 2);
return;
}
}
let next_idx = (color_idx + 1) % 3;
state.aux = next_idx as u32 | (next_rand(rng) << 2);
}