Skip to content

Commit

Permalink
feat: Implement the rest of Rgb conversion traits for Color
Browse files Browse the repository at this point in the history
tinybmp and other crates require Color to implement all Rgb transformations
  • Loading branch information
bemyak committed Dec 5, 2024
1 parent c551ff0 commit 80d364d
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,68 @@ impl From<Color> for embedded_graphics_core::pixelcolor::Rgb888 {
fn from(color: Color) -> Self {
use embedded_graphics_core::pixelcolor::RgbColor;
match color {
Color::Black => embedded_graphics_core::pixelcolor::Rgb888::BLACK,
Color::White => embedded_graphics_core::pixelcolor::Rgb888::WHITE,
Color::Black => embedded_graphics_core::pixelcolor::Rgb888::WHITE,
Color::White => embedded_graphics_core::pixelcolor::Rgb888::BLACK,
}
}
}

#[cfg(feature = "graphics")]
impl From<embedded_graphics_core::pixelcolor::Rgb565> for Color {
fn from(rgb: embedded_graphics_core::pixelcolor::Rgb565) -> Self {
use embedded_graphics_core::pixelcolor::RgbColor;
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
}
}
}
}

#[cfg(feature = "graphics")]
impl From<Color> for embedded_graphics_core::pixelcolor::Rgb565 {
fn from(color: Color) -> Self {
use embedded_graphics_core::pixelcolor::RgbColor;
match color {
Color::Black => Self::WHITE,
Color::White => Self::BLACK,
}
}
}

#[cfg(feature = "graphics")]
impl From<embedded_graphics_core::pixelcolor::Rgb555> for Color {
fn from(rgb: embedded_graphics_core::pixelcolor::Rgb555) -> Self {
use embedded_graphics_core::pixelcolor::RgbColor;
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
}
}
}
}

#[cfg(feature = "graphics")]
impl From<Color> for embedded_graphics_core::pixelcolor::Rgb555 {
fn from(color: Color) -> Self {
use embedded_graphics_core::pixelcolor::RgbColor;
match color {
Color::Black => Self::WHITE,
Color::White => Self::BLACK,
}
}
}
Expand Down

0 comments on commit 80d364d

Please sign in to comment.