Expand description

Raw color types.

This module contains structs to represent the raw data used to store color information. Colors that implement the PixelColor trait can use the associated Raw type to define their raw data representation.

Specifying a Raw type for a PixelColor is required to use that color with the Image struct.

§Implementing PixelColor with Raw support

This example shows how to implement a new PixelColor that can be used with images.

The RGBI color type uses 4 bits per pixel, one for each color channel and an additional intensity bit.

use embedded_graphics::{image::ImageRaw, pixelcolor::raw::RawU4, prelude::*};

/// RGBI color
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct RGBI(RawU4);

impl RGBI {
    /// Creates a RGBI color.
    pub fn new(red: bool, green: bool, blue: bool, intensity: bool) -> Self {
        let mut value = 0;

        if red {
            value |= 0b0100;
        }
        if green {
            value |= 0b0010;
        }
        if blue {
            value |= 0b0001;
        }
        if intensity {
            value |= 0b1000;
        }

        Self(RawU4::new(value))
    }
}

/// Implement `PixelColor` to associate a raw data type with the `RGBI` struct.
impl PixelColor for RGBI {
    type Raw = RawU4;
}

/// `From<RawU4>` is used by `Image` to construct RGBI colors.
impl From<RawU4> for RGBI {
    fn from(data: RawU4) -> Self {
        Self(data)
    }
}

/// Raw image data with 2 pixels per byte.
#[rustfmt::skip]
const IMAGE_DATA: &[u8] = &[
    0b0001_0010,
    0b0100_1111,
];

fn main() {
    // Create new image with RGBI colors.
    let image: ImageRaw<RGBI> = ImageRaw::new(IMAGE_DATA, 2, 2);

    // In a real application the image could now be drawn to a display:
    // display.draw(&image);
}

Structs§

  • 1 bit per pixel raw data.
  • 2 bits per pixel raw data.
  • 4 bits per pixel raw data.
  • 8 bits per pixel raw data.
  • 16 bits per pixel raw data.
  • 24 bits per pixel raw data.
  • 32 bits per pixel raw data.

Enums§

Traits§