2018-12-26 01:54:30 +00:00
|
|
|
/// Represents the state of a pixel in the display
|
2018-11-14 07:02:33 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
|
|
|
pub enum Color {
|
|
|
|
Black,
|
|
|
|
White,
|
|
|
|
Red,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "graphics")]
|
|
|
|
extern crate embedded_graphics;
|
|
|
|
#[cfg(feature = "graphics")]
|
2020-07-19 00:42:24 +00:00
|
|
|
use self::embedded_graphics::pixelcolor::raw::RawU8;
|
|
|
|
#[cfg(feature = "graphics")]
|
2022-08-02 19:49:13 +00:00
|
|
|
use self::embedded_graphics::pixelcolor::{BinaryColor, Rgb888, RgbColor};
|
|
|
|
#[cfg(feature = "graphics")]
|
2018-11-14 07:02:33 +00:00
|
|
|
use self::embedded_graphics::prelude::*;
|
|
|
|
#[cfg(feature = "graphics")]
|
2020-07-19 00:42:24 +00:00
|
|
|
impl PixelColor for Color {
|
|
|
|
type Raw = RawU8;
|
|
|
|
}
|
2018-11-14 07:02:33 +00:00
|
|
|
|
|
|
|
impl From<u8> for Color {
|
|
|
|
fn from(value: u8) -> Self {
|
|
|
|
match value {
|
|
|
|
0 => Color::Black,
|
|
|
|
1 => Color::White,
|
|
|
|
2 => Color::Red,
|
2018-12-25 21:43:04 +00:00
|
|
|
_ => panic!("invalid color value"),
|
2018-11-14 07:02:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 19:49:13 +00:00
|
|
|
#[cfg(feature = "graphics")]
|
|
|
|
impl From<BinaryColor> for Color {
|
|
|
|
fn from(value: BinaryColor) -> Self {
|
|
|
|
match value {
|
|
|
|
BinaryColor::On => Color::Black,
|
|
|
|
BinaryColor::Off => Color::White,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "graphics")]
|
|
|
|
impl From<Rgb888> for Color {
|
|
|
|
fn from(value: Rgb888) -> Self {
|
|
|
|
// NOTE(feliix42): This is admittedly a very simplistic approximation, it'd be more
|
|
|
|
// accurate to map the colors accordingly, but since that's an injective mapping anyways,
|
|
|
|
// why not keep it simple for now.
|
|
|
|
match value {
|
|
|
|
Rgb888::WHITE => Color::White,
|
|
|
|
Rgb888::BLACK => Color::Black,
|
|
|
|
_ => Color::Red,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "graphics")]
|
|
|
|
impl Into<Rgb888> for Color {
|
|
|
|
fn into(self) -> Rgb888 {
|
|
|
|
match self {
|
|
|
|
Color::White => Rgb888::WHITE,
|
|
|
|
Color::Black => Rgb888::BLACK,
|
|
|
|
Color::Red => Rgb888::RED,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:02:33 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_u8() {
|
|
|
|
assert_eq!(Color::Black, Color::from(0u8));
|
|
|
|
assert_eq!(Color::White, Color::from(1u8));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn from_u8_panic() {
|
|
|
|
for val in 3..=u8::max_value() {
|
|
|
|
extern crate std;
|
|
|
|
let result = std::panic::catch_unwind(|| Color::from(val));
|
|
|
|
assert!(result.is_err());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|