embedded_graphics

Module pixelcolor

Source
Expand description

Pixel color types.

This module contains structs for commonly used pixel color formats and conversions between them. The raw module provides additional functions to convert colors to and from raw data for use with images and displays.

§Implementing custom color types

Custom color types can be added by implementing the PixelColor trait. The following example shows how to implement a new color type for a 3 color EPD display.

use embedded_graphics::{egrectangle, geometry::Size, prelude::*, primitive_style};

/// Color with 3 states.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum EpdColor {
    White,
    Black,
    Red,
}

/// The `Raw` can be is set to `()` because `EpdColor` doesn't need to be
/// converted to raw data for the display and isn't stored in images.
impl PixelColor for EpdColor {
    type Raw = ();
}

/// Mock EPD display.
pub struct EpdDisplay {}

impl DrawTarget<EpdColor> for EpdDisplay {
    type Error = core::convert::Infallible;

    fn draw_pixel(&mut self, item: Pixel<EpdColor>) -> Result<(), Self::Error> {
        let Pixel(point, color) = item;
        match color {
            EpdColor::White => {} // draw white pixel at `point`
            EpdColor::Black => {} // draw black pixel at `point`
            EpdColor::Red => {}   // draw red pixel at `point`
        }

        Ok(())
    }

    fn size(&self) -> Size {
        Size::zero()
    }
}

let mut display = EpdDisplay {};

egrectangle!(
    top_left = (0, 0),
    bottom_right = (100, 100),
    style = primitive_style!(fill_color = EpdColor::White)
)
.draw(&mut display)?;

egrectangle!(
    top_left = (100, 0),
    bottom_right = (200, 100),
    style = primitive_style!(fill_color = EpdColor::Black)
)
.draw(&mut display)?;

egrectangle!(
    top_left = (200, 0),
    bottom_right = (300, 100),
    style = primitive_style!(fill_color = EpdColor::Red)
)
.draw(&mut display)?;

The implementation of the DrawTarget trait uses a match statement to draw the specified color and doesn’t depend on the raw data conversions, see the raw module documentation for an example that uses this feature.

Modules§

  • Raw color types.

Structs§

Enums§

Traits§