Module embedded_graphics::pixelcolor[][src]

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::{
    geometry::Size, prelude::*, primitives::{Rectangle, PrimitiveStyle},
};

/// 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 for EpdDisplay {
    type Color = EpdColor;
    type Error = core::convert::Infallible;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Self::Color>>,
    {
        for Pixel(point, color) in pixels.into_iter() {
            match color {
                EpdColor::White => {} // draw white pixel at `point`
                EpdColor::Black => {} // draw black pixel at `point`
                EpdColor::Red => {}   // draw red pixel at `point`
            }
        }

        Ok(())
    }
}

impl OriginDimensions for EpdDisplay {
    fn size(&self) -> Size {
        Size::new(300, 300)
    }
}

let mut display = EpdDisplay {};

Rectangle::new(Point::new(0, 0), Size::new(100, 100))
    .into_styled(PrimitiveStyle::with_fill(EpdColor::White))
    .draw(&mut display)?;

Rectangle::new(Point::new(100, 0), Size::new(100, 100))
    .into_styled(PrimitiveStyle::with_fill(EpdColor::Black))
    .draw(&mut display)?;

Rectangle::new(Point::new(200, 0), Size::new(100, 100))
    .into_styled(PrimitiveStyle::with_fill(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

Raw color types.

Structs

Bgr555

Bgr555 color.

Bgr565

Bgr565 color.

Bgr888

Bgr888 color.

Gray2

2 bit grayscale color.

Gray4

4 bit grayscale color.

Gray8

8 bit grayscale color.

Rgb555

Rgb555 color.

Rgb565

Rgb565 color.

Rgb888

Rgb888 color.

Enums

BinaryColor

Binary color.

Traits

GrayColor

Grayscale color.

IntoStorage

Convert a PixelColor into its underlying storage type

PixelColor

Pixel color trait.

RgbColor

RGB color.

WebColors

Named colors as defined in the CSS specification.