#rgb #pixel #rgba #bgra #interoperability


struct RGB (etc.) for sharing pixels between crates. Allows no-copy high-level interoperability. Also adds common convenience methods and implements standard Rust traits to make RGB/RGBA pixels first-class Rust objects

28 releases

0.8.11 Jul 19, 2018
0.8.9 May 11, 2018
0.8.7 Dec 31, 2017
0.8.5 Nov 27, 2017
0.4.0 Jul 5, 2016

#9 in Images

Download history 165/week @ 2018-05-27 385/week @ 2018-06-03 177/week @ 2018-06-10 142/week @ 2018-06-17 107/week @ 2018-06-24 463/week @ 2018-07-01 175/week @ 2018-07-08 193/week @ 2018-07-15 221/week @ 2018-07-22 234/week @ 2018-07-29 296/week @ 2018-08-05 257/week @ 2018-08-12 213/week @ 2018-08-19

945 downloads per month

struct RGB for Rust v

Operating on pixels as weakly-typed vectors of u8 is error-prone and inconvenient. It's better to use vectors of pixel structs. However, Rust is so strongly typed that your RGB pixel struct is not compatible with my RGB pixel struct. So let's all use mine :P

xkcd standards


Add this to your Cargo.toml:

rgb = "0.8"


RGB and RGBA structs

The structs implement common Rust traits and a few convenience functions, e.g. map that repeats an operation on every subpixel:

extern crate rgb;
use rgb::*; // Laziest way to use traits which add extra methods to the structs

let px = RGB {
let inverted = px.map(|ch| 255 - ch);

println!("{}", inverted); // Display: rgb(0,255,0)
assert_eq!(RGB8::new(0, 255, 0), inverted);

Byte slices to pixel slices

For interoperability with functions operating on generic arrays of bytes there are functinos for safe casting to and from pixel slices.

let raw = vec![0u8; width*height*3];
let pixels: &[RGB8] = raw.as_rgb(); /// Safe casts without copying
let raw_again = pixels.as_bytes();

About colorspaces

This crate is intentionally ignorant about flavors of RGB color spaces. Correct color management is a complex problem, and this crate aims to be the lowest common denominator.

However, it supports any subpixel type for RGB<T>, and RGBA<RGBType, AlphaType>, so you can use them with a newtype, e.g.:

struct LinearLight(u16);
type LinearRGB = RGB<LinearLight>;
MIT license


  • optional serde 1.0+derive

Reverse deps