use crate::hsv::{Chsv, hsv2rgb_rainbow};
use crate::rgb::Crgb;
pub trait FromHsv: Copy {
fn from_hsv(hsv: Chsv) -> Self;
}
impl FromHsv for Chsv {
#[inline]
fn from_hsv(hsv: Chsv) -> Self {
hsv
}
}
impl FromHsv for Crgb {
#[inline]
fn from_hsv(hsv: Chsv) -> Self {
hsv2rgb_rainbow(hsv)
}
}
pub fn fill_solid<T: Copy>(leds: &mut [T], color: T) {
for led in leds.iter_mut() {
*led = color;
}
}
pub fn fill_rainbow<T: FromHsv>(leds: &mut [T], initial_hue: u8, delta_hue: u8) {
let mut hsv = Chsv::new(initial_hue, 240, 255);
for led in leds.iter_mut() {
*led = T::from_hsv(hsv);
hsv.hue = hsv.hue.wrapping_add(delta_hue);
}
}
pub fn fill_rainbow_circular<T: FromHsv>(leds: &mut [T], initial_hue: u8, reversed: bool) {
if leds.is_empty() {
return; }
let mut hsv = Chsv::new(initial_hue, 240, 255);
let hue_change = 65535u16 / leds.len() as u16;
let mut hue_offset = 0u16;
for led in leds.iter_mut() {
*led = T::from_hsv(hsv);
if reversed {
hue_offset = hue_offset.wrapping_sub(hue_change);
} else {
hue_offset = hue_offset.wrapping_add(hue_change);
}
hsv.hue = initial_hue.wrapping_add((hue_offset >> 8) as u8);
}
}
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub enum GradientDirection {
Forward,
Backward,
#[default]
Shortest,
Longest,
}
pub fn fill_gradient_rgb_range(
leds: &mut [Crgb],
start_pos: u16,
start_color: Crgb,
end_pos: u16,
end_color: Crgb,
) {
let (start_pos, end_pos, start_color, end_color) = if end_pos < start_pos {
(end_pos, start_pos, end_color, start_color)
} else {
(start_pos, end_pos, start_color, end_color)
};
let rdistance87 = (end_color.r as i16 - start_color.r as i16) << 7;
let gdistance87 = (end_color.g as i16 - start_color.g as i16) << 7;
let bdistance87 = (end_color.b as i16 - start_color.b as i16) << 7;
let pixeldistance = end_pos - start_pos;
let divisor = if pixeldistance != 0 {
pixeldistance as i16
} else {
1
};
let rdelta87 = (rdistance87 / divisor).wrapping_mul(2);
let gdelta87 = (gdistance87 / divisor).wrapping_mul(2);
let bdelta87 = (bdistance87 / divisor).wrapping_mul(2);
let mut r88 = (start_color.r as u16) << 8;
let mut g88 = (start_color.g as u16) << 8;
let mut b88 = (start_color.b as u16) << 8;
for i in start_pos..=end_pos {
if let Some(led) = leds.get_mut(i as usize) {
*led = Crgb::new((r88 >> 8) as u8, (g88 >> 8) as u8, (b88 >> 8) as u8);
}
r88 = r88.wrapping_add(rdelta87 as u16);
g88 = g88.wrapping_add(gdelta87 as u16);
b88 = b88.wrapping_add(bdelta87 as u16);
}
}
pub fn fill_gradient_rgb(leds: &mut [Crgb], c1: Crgb, c2: Crgb) {
if leds.is_empty() {
return;
}
let last = (leds.len() - 1) as u16;
fill_gradient_rgb_range(leds, 0, c1, last, c2);
}
pub fn fill_gradient_rgb3(leds: &mut [Crgb], c1: Crgb, c2: Crgb, c3: Crgb) {
if leds.is_empty() {
return;
}
let num_leds = leds.len() as u16;
let half = num_leds / 2;
let last = num_leds - 1;
fill_gradient_rgb_range(leds, 0, c1, half, c2);
fill_gradient_rgb_range(leds, half, c2, last, c3);
}
pub fn fill_gradient_rgb4(leds: &mut [Crgb], c1: Crgb, c2: Crgb, c3: Crgb, c4: Crgb) {
if leds.is_empty() {
return;
}
let num_leds = leds.len() as u16;
let onethird = num_leds / 3;
let twothirds = (num_leds * 2) / 3;
let last = num_leds - 1;
fill_gradient_rgb_range(leds, 0, c1, onethird, c2);
fill_gradient_rgb_range(leds, onethird, c2, twothirds, c3);
fill_gradient_rgb_range(leds, twothirds, c3, last, c4);
}
pub fn fill_gradient_range<T: FromHsv>(
leds: &mut [T],
start_pos: u16,
start_color: Chsv,
end_pos: u16,
end_color: Chsv,
direction: GradientDirection,
) {
let (start_pos, end_pos, mut start_color, mut end_color) = if end_pos < start_pos {
(end_pos, start_pos, end_color, start_color)
} else {
(start_pos, end_pos, start_color, end_color)
};
if end_color.val == 0 || end_color.sat == 0 {
end_color.hue = start_color.hue;
}
if start_color.val == 0 || start_color.sat == 0 {
start_color.hue = end_color.hue;
}
let satdistance87 = (end_color.sat as i16 - start_color.sat as i16) << 7;
let valdistance87 = (end_color.val as i16 - start_color.val as i16) << 7;
let huedelta8 = end_color.hue.wrapping_sub(start_color.hue);
let direction = match direction {
GradientDirection::Shortest => {
if huedelta8 > 127 {
GradientDirection::Backward
} else {
GradientDirection::Forward
}
}
GradientDirection::Longest => {
if huedelta8 < 128 {
GradientDirection::Backward
} else {
GradientDirection::Forward
}
}
other => other,
};
let huedistance87 = if direction == GradientDirection::Forward {
(huedelta8 as i16) << 7
} else {
let back = (256u16.wrapping_sub(huedelta8 as u16)) as u8;
-((back as i16) << 7)
};
let pixeldistance = end_pos - start_pos;
let divisor = if pixeldistance != 0 {
pixeldistance as i16
} else {
1
};
let huedelta87 = (huedistance87 / divisor).wrapping_mul(2);
let satdelta87 = (satdistance87 / divisor).wrapping_mul(2);
let valdelta87 = (valdistance87 / divisor).wrapping_mul(2);
let mut hue88 = (start_color.hue as u16) << 8;
let mut sat88 = (start_color.sat as u16) << 8;
let mut val88 = (start_color.val as u16) << 8;
for i in start_pos..=end_pos {
if let Some(led) = leds.get_mut(i as usize) {
*led = T::from_hsv(Chsv::new(
(hue88 >> 8) as u8,
(sat88 >> 8) as u8,
(val88 >> 8) as u8,
));
}
hue88 = hue88.wrapping_add(huedelta87 as u16);
sat88 = sat88.wrapping_add(satdelta87 as u16);
val88 = val88.wrapping_add(valdelta87 as u16);
}
}
pub fn fill_gradient<T: FromHsv>(leds: &mut [T], c1: Chsv, c2: Chsv, direction: GradientDirection) {
if leds.is_empty() {
return;
}
let last = (leds.len() - 1) as u16;
fill_gradient_range(leds, 0, c1, last, c2, direction);
}
pub fn fill_gradient3<T: FromHsv>(
leds: &mut [T],
c1: Chsv,
c2: Chsv,
c3: Chsv,
direction: GradientDirection,
) {
if leds.is_empty() {
return;
}
let num_leds = leds.len() as u16;
let half = num_leds / 2;
let last = num_leds - 1;
fill_gradient_range(leds, 0, c1, half, c2, direction);
fill_gradient_range(leds, half, c2, last, c3, direction);
}
pub fn fill_gradient4<T: FromHsv>(
leds: &mut [T],
c1: Chsv,
c2: Chsv,
c3: Chsv,
c4: Chsv,
direction: GradientDirection,
) {
if leds.is_empty() {
return;
}
let num_leds = leds.len() as u16;
let onethird = num_leds / 3;
let twothirds = (num_leds * 2) / 3;
let last = num_leds - 1;
fill_gradient_range(leds, 0, c1, onethird, c2, direction);
fill_gradient_range(leds, onethird, c2, twothirds, c3, direction);
fill_gradient_range(leds, twothirds, c3, last, c4, direction);
}
pub fn fade_to_black_by(leds: &mut [Crgb], fade_by: u8) {
for led in leds.iter_mut() {
led.nscale8(255 - fade_by);
}
}
pub fn fade_light_by(leds: &mut [Crgb], fade_by: u8) {
for led in leds.iter_mut() {
led.nscale8_video(255 - fade_by);
}
}
pub fn nscale8(leds: &mut [Crgb], scale: u8) {
for led in leds.iter_mut() {
led.nscale8(scale);
}
}
pub fn nscale8_video(leds: &mut [Crgb], scale: u8) {
for led in leds.iter_mut() {
led.nscale8_video(scale);
}
}
#[inline]
pub fn fade_video(leds: &mut [Crgb], fade_by: u8) {
fade_light_by(leds, fade_by);
}
#[inline]
pub fn fade_raw(leds: &mut [Crgb], fade_by: u8) {
fade_to_black_by(leds, fade_by);
}
#[inline]
pub fn nscale8_raw(leds: &mut [Crgb], scale: u8) {
nscale8(leds, scale);
}
pub fn fade_using_color(leds: &mut [Crgb], colormask: Crgb) {
for led in leds.iter_mut() {
led.nscale8_rgb(colormask);
}
}