[][src]Module embedded_graphics::pixelcolor::raw

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::prelude::*;
use embedded_graphics::pixelcolor::raw::RawU4;

/// RGBI color
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
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.
const IMAGE_DATA: &[u8] = &[
    0b0001_0010,
    0b0100_1111,
];

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

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

Structs

RawU1

1 bit per pixel raw data.

RawU2

2 bits per pixel raw data.

RawU4

4 bits per pixel raw data.

RawU8

8 bits per pixel raw data.

RawU16

16 bits per pixel raw data.

RawU24

24 bits per pixel raw data.

RawU32

32 bits per pixel raw data.

Enums

BigEndian

Big endian byte order marker.

LittleEndian

Little endian byte order marker.

Traits

ByteOrder

Raw data byte order.

RawData

Trait implemented by all RawUx types.