Crate vizia::vg::rgb

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 =|channel| channel / 2);
let doubled = half_bright * 2;


  • BGR/BGRA alernative layouts & grayscale


  • An Red + Green + Blue pixel.
  • An Red + Green + Blue + Alpha 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
  • Marker trait for “plain old data”.
  • Trait for types that can be safely created with zeroed.

Type Aliases§

  • 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.

Derive Macros§

  • Derive the Pod trait for a struct
  • Derive the Zeroable trait for a struct