Module embedded_graphics::pixelcolor::raw
source · [−]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.
Converting colors to raw data
Colors can be converted into raw data by using two different methods. The into_storage
method is used to convert a color into a single integer value. To convert a color into a byte
array the methods provided by the ToBytes
trait can be used. By using to_be_bytes
the
color components will have the same order in memory as in the name of the type.
use embedded_graphics::{pixelcolor::Rgb888, prelude::*};
let color = Rgb888::new(0x11, 0x22, 0x33);
assert_eq!(color.into_storage(), 0x00112233);
assert_eq!(color.to_be_bytes(), [0x11, 0x22, 0x33]);
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,
];
// Create new image with RGBI colors.
let image_raw: ImageRaw<RGBI> = ImageRaw::new(IMAGE_DATA, 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.
18 bits per pixel raw data.
24 bits per pixel raw data.
32 bits per pixel raw data.
Enums
Big endian byte order marker.
Little endian byte order marker.