• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

examples/H03-May-2022-3119

src/H03-May-2022-2,2521,700

.cargo-checksum.jsonH A D03-May-202289 11

.cargo_vcs_info.jsonH A D01-Jan-197074 65

Cargo.lockH A D01-Jan-19702.3 KiB9584

Cargo.tomlH A D01-Jan-19701.8 KiB5650

Cargo.toml.orig-cargoH A D01-Jan-19701.4 KiB4335

LICENSEH A D01-Jan-19701 KiB2217

README.mdH A D01-Jan-19702.3 KiB7145

README.md

1# `struct RGB` for [Rust](https://www.rust-lang.org)  [![crate](https://img.shields.io/crates/v/rgb.svg)](https://lib.rs/crates/rgb)
2
3Operating 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
4
5[![xkcd standards](https://imgs.xkcd.com/comics/standards.png)](https://xkcd.com/927/)
6
7## Installation
8
9Add this to your `Cargo.toml`:
10
11```toml
12[dependencies]
13rgb = "0.8"
14```
15
16## Usage
17
18### `RGB` and `RGBA` structs
19
20The structs implement common Rust traits and a few convenience functions, e.g. `map` that repeats an operation on every subpixel:
21
22```rust
23use rgb::*; // Laziest way to use traits which add extra methods to the structs
24
25let px = RGB {
26    r:255_u8,
27    g:0,
28    b:255,
29};
30let inverted = px.map(|ch| 255 - ch);
31
32println!("{}", inverted); // Display: rgb(0,255,0)
33assert_eq!(RGB8::new(0, 255, 0), inverted);
34```
35
36### Byte slices to pixel slices
37
38For interoperability with functions operating on generic arrays of bytes there are functions for safe casting to and from pixel slices.
39
40```rust
41let raw = vec![0u8; width*height*3];
42let pixels: &[RGB8] = raw.as_rgb(); /// Safe casts without copying
43let raw_again = pixels.as_bytes();
44```
45
46Note: if you get an error about "no method named `as_bytes` found", add `use rgb::ComponentBytes`. If you're using a custom component type (`RGB<CustomType>`), implement `rgb::Pod` (plain old data) and `rgb::Zeroable` trait for the component (these traits are from [`bytemuck`](//lib.rs/bytemuck) crate).
47
48----
49
50## About colorspaces
51
52*Correct* color management is a complex problem, and this crate aims to be the lowest common denominator, so it's intentionally agnostic about it.
53
54However, this library supports any subpixel type for `RGB<T>`, and `RGBA<RGBType, AlphaType>`, so you can use them with a newtype, e.g.:
55
56```rust
57struct LinearLight(u16);
58type LinearRGB = RGB<LinearLight>;
59```
60
61
62### `BGRA`, `ARGB`, `Gray`, etc.
63
64There are other color types in `rgb::alt::*`. To enable `ARGB` and `ABGR`, use the "argb" feature:
65
66```toml
67rgb = { version = "0.8", features = ["argb"] }
68```
69
70There's also an optional `serde` feature that makes all types (de)serializable.
71