rgb
Shared Rust pixel structs (Rgb, Rgba, Bgr, grayscale) so image crates can exchange pixel data reliably.
Repository Health
Technical Analysis
rgb provides a small set of common pixel structs for Rust, including Rgb, Rgba, Argb, Bgr, Bgra, Abgr, Grb, and grayscale types, so that different crates can share strongly-typed pixel data instead of passing around weakly-typed vectors of u8. Because your Rgb struct and someone else’s are otherwise incompatible in Rust’s type system, this crate acts as a neutral common type used across the ecosystem.
All functionality lives in traits like Pixel, HetPixel, HasAlpha, and GainAlpha, giving channel-wise map and conversion helpers without imposing inherent methods. It is intentionally color-space agnostic and low-overhead, with optional integrations for bytemuck, serde, num-traits, and defmt for zero-cost byte casts and interoperability.
What You Get
- Common pixel structs: Rgb, Rgba, Argb, Bgr, Bgra, Abgr, Grb, Gray, and GrayA
- Trait-based API (Pixel, HetPixel, HasAlpha, GainAlpha) for channel-wise map and alpha handling
- Heterogeneous pixels with separate color and alpha component types (e.g. Rgba<f32, u8>)
- Zero-cost byte conversions via optional bytemuck integration
- Optional serde, num-traits, and defmt feature flags for broader ecosystem interop
Common Use Cases
- Sharing pixel buffers between independent Rust image crates using one agreed type
- Mapping over all channels of a pixel without hand-writing r/g/b/a boilerplate
- Casting a Vec<u8> to a Vec<Rgb<u8>> with zero copies via bytemuck
- Adding or manipulating an alpha channel on existing RGB pixels
Under The Hood
Architecture - The crate defines plain generic pixel structs (Rgb<T>, Rgba<T, A = T>, Bgr, Grb, Gray, GrayA, etc.) with named fields, and moves all behavior into a trait hierarchy: HetPixel is the foundational trait (allowing distinct color/alpha types), Pixel narrows it to homogeneous pixels, HasAlpha covers alpha-bearing types, and GainAlpha adds alpha to alpha-less pixels. Keeping methods in traits rather than as inherent methods lets callers choose which helpers are in scope. Byte-level conversions are delegated to bytemuck via Pod/Zeroable impls for zero-cost cast_slice/cast_vec.
Tech Stack - Pure Rust (100%), distributed as the rgb Cargo crate. All dependencies are optional and exist only for interoperability: bytemuck (byte casts), serde (serialization), num-traits (checked arithmetic), and defmt (embedded formatting).
Code Quality - This is one of the most-used crates in the Rust ecosystem (nearly 100 million downloads) with 270+ commits since 2016 and 25 contributors. The code is stable, carefully versioned (0.8 line with a documented path to 0.9/1.0), and pays close attention to backwards compatibility and deprecation migration.
API Design - The API is ergonomic and consistent: struct literals for construction, Display/LowerHex formatting (rgb(0, 100, 255), #0064FF), and trait methods like map, map_colors, with_alpha, to_color_array, and each_mut. The README documents the trait split and a detailed migration guide, though the current transition period (deprecated inherent methods vs trait methods) adds some temporary naming friction.