#rgb #pixel #color #rgba #bgra


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

31 releases

✓ Uses Rust 2018 edition

0.8.16 Jan 5, 2020
0.8.14 Aug 20, 2019
0.8.13 Feb 26, 2019
0.8.11 Jul 19, 2018
0.4.0 Jul 5, 2016

#2 in Images

Download history 18460/week @ 2019-10-13 18052/week @ 2019-10-20 19658/week @ 2019-10-27 21975/week @ 2019-11-03 13210/week @ 2019-11-10 10484/week @ 2019-11-17 8226/week @ 2019-11-24 9907/week @ 2019-12-01 9395/week @ 2019-12-08 9164/week @ 2019-12-15 3937/week @ 2019-12-22 4602/week @ 2019-12-29 8864/week @ 2020-01-05 8819/week @ 2020-01-12 9606/week @ 2020-01-19

53,479 downloads per month
Used in 89 crates (31 directly)

MIT license

1.5K SLoC

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>;

BGRA, ARGB, Gray, etc.

There are other color types in rgb::alt::*. To enable ARGB and ABGR, use the "argb" feature:

rgb = { version = "0.8", features = ["argb"] }

There's also an optional serde feature that makes all types (de)serializable.