use alloc::borrow::Cow;
use alloc::vec::Vec;
use core::{error, fmt};
use crate::colors::RgbColor;
const COLOR_LENGTH: usize = 3;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ColorPaletteError {
InvalidLength,
}
impl fmt::Display for ColorPaletteError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidLength => {
f.write_str("the length of the palette is not a multiple of three")
}
}
}
}
impl error::Error for ColorPaletteError {}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ColorPalette<'a> {
bytes: Cow<'a, [u8]>,
}
impl ColorPalette<'static> {
#[must_use]
pub const fn new() -> Self {
Self {
bytes: Cow::Borrowed(&[]),
}
}
pub fn from_vec(bytes: Vec<u8>) -> Result<Self, ColorPaletteError> {
Self::from_cow(Cow::Owned(bytes))
}
}
impl<'a> ColorPalette<'a> {
pub fn from_buffer(bytes: &'a [u8]) -> Result<Self, ColorPaletteError> {
Self::from_cow(Cow::Borrowed(bytes))
}
fn from_cow(bytes: Cow<'a, [u8]>) -> Result<Self, ColorPaletteError> {
if bytes.len().is_multiple_of(COLOR_LENGTH) {
Ok(Self { bytes })
} else {
Err(ColorPaletteError::InvalidLength)
}
}
#[must_use]
pub fn into_owned(self) -> ColorPalette<'static> {
ColorPalette {
bytes: Cow::Owned(self.bytes.into_owned()),
}
}
pub fn clear(&mut self) {
*self = ColorPalette::new();
}
#[must_use]
pub fn color(&self, index: usize) -> Option<RgbColor> {
let start = index.checked_mul(COLOR_LENGTH)?;
match *self.bytes.get(start..start.checked_add(COLOR_LENGTH)?)? {
[red, green, blue] => Some(RgbColor::new(red, green, blue)),
_ => None,
}
}
#[must_use]
pub fn color_or(&self, index: usize, default: RgbColor) -> RgbColor {
self.color(index).unwrap_or(default)
}
#[must_use]
pub fn color_or_default(&self, index: usize) -> RgbColor {
self.color(index).unwrap_or_default()
}
#[must_use]
pub fn len(&self) -> usize {
self.bytes.len() / COLOR_LENGTH
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
pub fn iter(&self) -> impl ExactSizeIterator<Item = RgbColor> + '_ {
let (colors, _) = self.bytes.as_chunks::<COLOR_LENGTH>();
colors
.iter()
.map(|&[red, green, blue]| RgbColor::new(red, green, blue))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn colors_are_looked_up_by_index() {
let palette = ColorPalette::from_buffer(&[1, 2, 3, 4, 5, 6]).unwrap_or_default();
assert_eq!(palette.len(), 2);
assert_eq!(palette.color(1), Some(RgbColor::new(4, 5, 6)));
assert_eq!(palette.color(2), None);
assert_eq!(palette.color(usize::MAX), None);
assert_eq!(palette.color_or(0, RgbColor::WHITE), RgbColor::new(1, 2, 3));
assert_eq!(palette.color_or(2, RgbColor::WHITE), RgbColor::WHITE);
assert_eq!(palette.color_or_default(2), RgbColor::BLACK);
assert_eq!(palette.iter().count(), 2);
}
#[test]
fn partial_colors_are_rejected() {
assert_eq!(
ColorPalette::from_buffer(&[1, 2, 3, 4]),
Err(ColorPaletteError::InvalidLength)
);
}
}