Crate rgb

source · []
Expand description

Basic struct for RGB and RGBA pixels. Packed, with red first, alpha last.

This crate is intended to be the lowest common denominator for sharing RGB/RGBA bitmaps between other crates.

The crate includes convenience functions for converting between the struct and bytes, and overloaded operators that work on all channels at once.

This crate intentionally doesn’t implement color management (due to complexity of the problem), but the structs can be parametrized to implement this if necessary. Other colorspaces are out of scope.

let pixel = RGB8 {r:0, g:100, b:255};

let pixel_rgba = pixel.alpha(255);
let pixel = pixel_rgba.rgb();

let pixels = vec![pixel; 100];
use rgb::ComponentBytes; // Import byte conversion trait
let bytes = pixels.as_bytes();

use rgb::ComponentMap; // Import pixel map trait
let half_bright = pixel.map(|channel| channel / 2);
let doubled = half_bright * 2;


BGR/BGRA alernative layouts & grayscale


The RGB pixel

The RGBA pixel


Casts a slice of bytes into a slice of pixels, e.g. [u8] to [RGB8].

Same as ComponentMap, but doesn’t change the alpha channel (if there’s any alpha).

Casting a slice of RGB/A values to a slice of u8

Applying operation to every component

Casting the struct to slices of its components

Cast a slice of component values (bytes) as a slice of RGB/RGBA pixels

Re-export from bytemuck crate Marker trait for “plain old data”.

Re-export from bytemuck crate Trait for types that can be safely created with zeroed.

Type Definitions

8-bit RGB

16-bit RGB in machine’s native endian

8-bit RGBA, alpha is last. 0 = transparent, 255 = opaque.

16-bit RGB in machine’s native endian. 0 = transparent, 65535 = opaque.