Skip to content

Commit

Permalink
feat: Add additional color conversions for tinybmp support (#16)
Browse files Browse the repository at this point in the history
feat: add additional color conversions for tinybmp support
  • Loading branch information
thedevleon authored Aug 22, 2024
1 parent bdb2825 commit 712d3b5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion examples/rp2040/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/stm32g431/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 61 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[cfg(feature = "graphics")]
use embedded_graphics::pixelcolor::{BinaryColor, Rgb888, RgbColor};
use embedded_graphics::pixelcolor::{BinaryColor, Rgb888, RgbColor, Rgb565, Rgb555};
use sealed::sealed;

/// Color definition for B/W displays
Expand Down Expand Up @@ -50,6 +50,66 @@ impl From<Rgb888> for Color {
}
}

/// Conversion from Rgb565 to use `Color` with tinybmp
#[cfg(feature = "graphics")]
impl From<Rgb565> for Color {
fn from(rgb: Rgb565) -> Self {
if rgb == RgbColor::BLACK {
Color::White
} else if rgb == RgbColor::WHITE {
Color::Black
} else {
// choose closest color
if (rgb.r() as u16 + rgb.g() as u16 + rgb.b() as u16) > 255 * 3 / 2 {
Color::Black
} else {
Color::White
}
}
}
}

/// Conversion to Rgb565 to use `Color` with tinybmp
#[cfg(feature = "graphics")]
impl From<Color> for Rgb565 {
fn from(color: Color) -> Self {
match color {
Color::Black => Self::WHITE,
Color::White => Self::BLACK,
}
}
}

/// Conversion from Rgb555 to use `Color` with tinybmp
#[cfg(feature = "graphics")]
impl From<Rgb555> for Color {
fn from(rgb: Rgb555) -> Self {
if rgb == RgbColor::BLACK {
Color::White
} else if rgb == RgbColor::WHITE {
Color::Black
} else {
// choose closest color
if (rgb.r() as u16 + rgb.g() as u16 + rgb.b() as u16) > 255 * 3 / 2 {
Color::Black
} else {
Color::White
}
}
}
}

/// Conversion to Rgb555 to use `Color` with tinybmp
#[cfg(feature = "graphics")]
impl From<Color> for Rgb555 {
fn from(color: Color) -> Self {
match color {
Color::Black => Self::WHITE,
Color::White => Self::BLACK,
}
}
}

/// Color for tri-color displays
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum TriColor {
Expand Down

0 comments on commit 712d3b5

Please sign in to comment.