use lib8tion::{Fract8, scale8, scale8_video};
use crate::hsv::Chsv;
use crate::rgb::Crgb;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorBlend {
NoBlend,
LinearBlend,
LinearBlendNoWrap,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CrgbPalette16(pub [Crgb; 16]);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CrgbPalette32(pub [Crgb; 32]);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CrgbPalette256(pub [Crgb; 256]);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChsvPalette16(pub [Chsv; 16]);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChsvPalette32(pub [Chsv; 32]);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChsvPalette256(pub [Chsv; 256]);
impl CrgbPalette16 {
#[inline]
pub const fn new(entries: [Crgb; 16]) -> Self {
Self(entries)
}
}
impl CrgbPalette32 {
#[inline]
pub const fn new(entries: [Crgb; 32]) -> Self {
Self(entries)
}
}
impl CrgbPalette256 {
#[inline]
pub const fn new(entries: [Crgb; 256]) -> Self {
Self(entries)
}
}
impl ChsvPalette16 {
#[inline]
pub const fn new(entries: [Chsv; 16]) -> Self {
Self(entries)
}
}
impl ChsvPalette32 {
#[inline]
pub const fn new(entries: [Chsv; 32]) -> Self {
Self(entries)
}
}
impl ChsvPalette256 {
#[inline]
pub const fn new(entries: [Chsv; 256]) -> Self {
Self(entries)
}
}
macro_rules! impl_index {
($ty:ty, $entry:ty) => {
impl core::ops::Index<usize> for $ty {
type Output = $entry;
#[inline]
fn index(&self, i: usize) -> &$entry {
&self.0[i]
}
}
};
}
impl_index!(CrgbPalette16, Crgb);
impl_index!(CrgbPalette32, Crgb);
impl_index!(CrgbPalette256, Crgb);
impl_index!(ChsvPalette16, Chsv);
impl_index!(ChsvPalette32, Chsv);
impl_index!(ChsvPalette256, Chsv);
#[inline]
fn interpolate_rgb(entries: &[Crgb], hi: usize, f2: u8, blend: ColorBlend) -> Crgb {
let e0 = entries[hi];
if blend == ColorBlend::NoBlend || f2 == 0 {
return e0;
}
let next = if hi + 1 == entries.len() { 0 } else { hi + 1 };
let e1 = entries[next];
let f1 = 255 - f2;
Crgb::new(
scale8(e0.r, Fract8(f1)).wrapping_add(scale8(e1.r, Fract8(f2))),
scale8(e0.g, Fract8(f1)).wrapping_add(scale8(e1.g, Fract8(f2))),
scale8(e0.b, Fract8(f1)).wrapping_add(scale8(e1.b, Fract8(f2))),
)
}
#[inline]
fn apply_brightness_rgb(mut entry: Crgb, brightness: u8) -> Crgb {
if brightness == 255 {
return entry;
}
if brightness == 0 {
return Crgb::new(0, 0, 0);
}
let b2 = brightness + 1;
entry.r = scale8(entry.r, Fract8(b2));
entry.g = scale8(entry.g, Fract8(b2));
entry.b = scale8(entry.b, Fract8(b2));
entry
}
pub fn color_from_palette16(
pal: &CrgbPalette16,
mut index: u8,
brightness: u8,
blend: ColorBlend,
) -> Crgb {
if blend == ColorBlend::LinearBlendNoWrap {
index = scale8(index, Fract8(239));
}
let hi4 = (index >> 4) as usize;
let lo4 = index & 0x0F;
let f2 = lo4 << 4;
let entry = interpolate_rgb(&pal.0, hi4, f2, blend);
apply_brightness_rgb(entry, brightness)
}
pub fn color_from_palette32(
pal: &CrgbPalette32,
mut index: u8,
brightness: u8,
blend: ColorBlend,
) -> Crgb {
if blend == ColorBlend::LinearBlendNoWrap {
index = scale8(index, Fract8(247));
}
let hi5 = (index >> 3) as usize;
let lo3 = index & 0x07;
let f2 = lo3 << 5;
let entry = interpolate_rgb(&pal.0, hi5, f2, blend);
apply_brightness_rgb(entry, brightness)
}
pub fn color_from_palette256(pal: &CrgbPalette256, index: u8, brightness: u8) -> Crgb {
let mut entry = pal.0[index as usize];
if brightness != 255 {
let b2 = brightness + 1;
entry.r = scale8_video(entry.r, Fract8(b2));
entry.g = scale8_video(entry.g, Fract8(b2));
entry.b = scale8_video(entry.b, Fract8(b2));
}
entry
}
#[inline]
fn interpolate_hsv(entries: &[Chsv], hi: usize, f2: u8, blend: ColorBlend) -> Chsv {
let e0 = entries[hi];
if blend == ColorBlend::NoBlend || f2 == 0 {
return e0;
}
let next = if hi + 1 == entries.len() { 0 } else { hi + 1 };
let e1 = entries[next];
let f1 = 255 - f2;
let mut hue1 = e0.hue;
let mut hue2 = e1.hue;
if e0.sat == 0 || e0.val == 0 {
hue1 = hue2;
}
if e1.sat == 0 || e1.val == 0 {
hue2 = hue1;
}
let sat = scale8(e0.sat, Fract8(f1)).wrapping_add(scale8(e1.sat, Fract8(f2)));
let val = scale8(e0.val, Fract8(f1)).wrapping_add(scale8(e1.val, Fract8(f2)));
let deltahue = hue2.wrapping_sub(hue1);
let hue = if deltahue & 0x80 != 0 {
let reverse_delta = (256u16 - deltahue as u16) as u8;
hue1.wrapping_sub(scale8(reverse_delta, Fract8(f2)))
} else {
hue1.wrapping_add(scale8(deltahue, Fract8(f2)))
};
Chsv::new(hue, sat, val)
}
pub fn color_from_palette16_hsv(
pal: &ChsvPalette16,
mut index: u8,
brightness: u8,
blend: ColorBlend,
) -> Chsv {
if blend == ColorBlend::LinearBlendNoWrap {
index = scale8(index, Fract8(239));
}
let hi4 = (index >> 4) as usize;
let lo4 = index & 0x0F;
let f2 = lo4 << 4;
let mut entry = interpolate_hsv(&pal.0, hi4, f2, blend);
if brightness != 255 {
entry.val = scale8_video(entry.val, Fract8(brightness));
}
entry
}
pub fn color_from_palette32_hsv(
pal: &ChsvPalette32,
mut index: u8,
brightness: u8,
blend: ColorBlend,
) -> Chsv {
if blend == ColorBlend::LinearBlendNoWrap {
index = scale8(index, Fract8(247));
}
let hi5 = (index >> 3) as usize;
let lo3 = index & 0x07;
let f2 = lo3 << 5;
let mut entry = interpolate_hsv(&pal.0, hi5, f2, blend);
if brightness != 255 {
entry.val = scale8_video(entry.val, Fract8(brightness));
}
entry
}
pub fn color_from_palette256_hsv(pal: &ChsvPalette256, index: u8, brightness: u8) -> Chsv {
let mut entry = pal.0[index as usize];
if brightness != 255 {
entry.val = scale8_video(entry.val, Fract8(brightness));
}
entry
}
pub trait Palette {
type Entry: Copy;
fn color_from_palette(&self, index: u8, brightness: u8, blend: ColorBlend) -> Self::Entry;
}
impl Palette for CrgbPalette16 {
type Entry = Crgb;
#[inline]
fn color_from_palette(&self, index: u8, brightness: u8, blend: ColorBlend) -> Crgb {
color_from_palette16(self, index, brightness, blend)
}
}
impl Palette for CrgbPalette32 {
type Entry = Crgb;
#[inline]
fn color_from_palette(&self, index: u8, brightness: u8, blend: ColorBlend) -> Crgb {
color_from_palette32(self, index, brightness, blend)
}
}
impl Palette for CrgbPalette256 {
type Entry = Crgb;
#[inline]
fn color_from_palette(&self, index: u8, brightness: u8, _blend: ColorBlend) -> Crgb {
color_from_palette256(self, index, brightness)
}
}
impl Palette for ChsvPalette16 {
type Entry = Chsv;
#[inline]
fn color_from_palette(&self, index: u8, brightness: u8, blend: ColorBlend) -> Chsv {
color_from_palette16_hsv(self, index, brightness, blend)
}
}
impl Palette for ChsvPalette32 {
type Entry = Chsv;
#[inline]
fn color_from_palette(&self, index: u8, brightness: u8, blend: ColorBlend) -> Chsv {
color_from_palette32_hsv(self, index, brightness, blend)
}
}
impl Palette for ChsvPalette256 {
type Entry = Chsv;
#[inline]
fn color_from_palette(&self, index: u8, brightness: u8, _blend: ColorBlend) -> Chsv {
color_from_palette256_hsv(self, index, brightness)
}
}
pub fn fill_palette<P: Palette>(
leds: &mut [P::Entry],
start_index: u8,
inc_index: u8,
pal: &P,
brightness: u8,
blend: ColorBlend,
) {
let mut color_index = start_index;
for led in leds.iter_mut() {
*led = pal.color_from_palette(color_index, brightness, blend);
color_index = color_index.wrapping_add(inc_index);
}
}
pub fn fill_palette_circular<P: Palette>(
leds: &mut [P::Entry],
start_index: u8,
pal: &P,
brightness: u8,
blend: ColorBlend,
reversed: bool,
) {
if leds.is_empty() {
return; }
let color_change = 65535u16 / leds.len() as u16;
let mut color_index = (start_index as u16) << 8;
for led in leds.iter_mut() {
*led = pal.color_from_palette((color_index >> 8) as u8, brightness, blend);
if reversed {
color_index = color_index.wrapping_sub(color_change);
} else {
color_index = color_index.wrapping_add(color_change);
}
}
}
pub fn nblend_palette_toward_palette(
current: &mut CrgbPalette16,
target: &CrgbPalette16,
max_changes: u8,
) {
let mut changes: u8 = 0;
for (cur, tgt) in current.0.iter_mut().zip(target.0.iter()) {
for (channel, goal) in [
(&mut cur.r, tgt.r),
(&mut cur.g, tgt.g),
(&mut cur.b, tgt.b),
] {
if *channel == goal {
continue;
}
if *channel < goal {
*channel += 1;
changes += 1;
}
if *channel > goal {
*channel -= 1;
changes += 1;
if *channel > goal {
*channel -= 1;
}
}
if changes >= max_changes {
return;
}
}
}
}
pub fn upscale_palette16_to_256(pal: &CrgbPalette16) -> CrgbPalette256 {
CrgbPalette256(core::array::from_fn(|i| {
color_from_palette16(pal, i as u8, 255, ColorBlend::LinearBlend)
}))
}
pub fn upscale_palette16_to_32(pal: &CrgbPalette16) -> CrgbPalette32 {
CrgbPalette32(core::array::from_fn(|i| pal.0[i / 2]))
}
pub fn upscale_palette32_to_256(pal: &CrgbPalette32) -> CrgbPalette256 {
CrgbPalette256(core::array::from_fn(|i| {
color_from_palette32(pal, i as u8, 255, ColorBlend::LinearBlend)
}))
}
pub fn upscale_palette16_to_256_hsv(pal: &ChsvPalette16) -> ChsvPalette256 {
ChsvPalette256(core::array::from_fn(|i| {
color_from_palette16_hsv(pal, i as u8, 255, ColorBlend::LinearBlend)
}))
}
pub fn upscale_palette16_to_32_hsv(pal: &ChsvPalette16) -> ChsvPalette32 {
ChsvPalette32(core::array::from_fn(|i| pal.0[i / 2]))
}
pub fn upscale_palette32_to_256_hsv(pal: &ChsvPalette32) -> ChsvPalette256 {
ChsvPalette256(core::array::from_fn(|i| {
color_from_palette32_hsv(pal, i as u8, 255, ColorBlend::LinearBlend)
}))
}
impl From<CrgbPalette16> for CrgbPalette256 {
fn from(pal: CrgbPalette16) -> Self {
upscale_palette16_to_256(&pal)
}
}
impl From<CrgbPalette16> for CrgbPalette32 {
fn from(pal: CrgbPalette16) -> Self {
upscale_palette16_to_32(&pal)
}
}
impl From<CrgbPalette32> for CrgbPalette256 {
fn from(pal: CrgbPalette32) -> Self {
upscale_palette32_to_256(&pal)
}
}
impl From<ChsvPalette16> for ChsvPalette256 {
fn from(pal: ChsvPalette16) -> Self {
upscale_palette16_to_256_hsv(&pal)
}
}
impl From<ChsvPalette16> for ChsvPalette32 {
fn from(pal: ChsvPalette16) -> Self {
upscale_palette16_to_32_hsv(&pal)
}
}
impl From<ChsvPalette32> for ChsvPalette256 {
fn from(pal: ChsvPalette32) -> Self {
upscale_palette32_to_256_hsv(&pal)
}
}