All Projects → kornelski → rust-rgb

kornelski / rust-rgb

Licence: MIT license
struct RGB for sharing pixels between crates

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to rust-rgb

imgref
A trivial Rust struct for interchange of pixel buffers with width, height & stride
Stars: ✭ 45 (-35.71%)
Mutual labels:  pixel-layout, rust-library
Curio
A Blazing Fast HTTP Client
Stars: ✭ 35 (-50%)
Mutual labels:  rust-library
kul
A unique textual notation that can be used as both a data format and a markup language and that has powerful extensibility of both lexical syntax and semantics, and a Rust library for parsing it.
Stars: ✭ 12 (-82.86%)
Mutual labels:  rust-library
Colour-Learning
A simple machine learning webpage that understands & changes the text 🌈 according to the background.
Stars: ✭ 21 (-70%)
Mutual labels:  rgb-color
ColorTranslator
A JavaScript library, written in TypeScript, to convert among different color models
Stars: ✭ 34 (-51.43%)
Mutual labels:  rgb-color
font8x8-rs
8x8 monochrome bitmap fonts for rendering. Implemented in Rust.
Stars: ✭ 15 (-78.57%)
Mutual labels:  rust-library
dupe-krill
A fast file deduplicator
Stars: ✭ 147 (+110%)
Mutual labels:  rust-library
blinkt
A Rust library for the Pimoroni Blinkt!, and any similar APA102 or SK9822 LED strips or boards, on a Raspberry Pi.
Stars: ✭ 18 (-74.29%)
Mutual labels:  rust-library
simple redis
Simple and resilient redis client for rust.
Stars: ✭ 21 (-70%)
Mutual labels:  rust-library
thread-priority
A simple thread schedule and priority library for rust
Stars: ✭ 48 (-31.43%)
Mutual labels:  rust-library
unicode-linebreak
󠁼💔 Implementation of the Unicode Line Breaking Algorithm in Rust
Stars: ✭ 14 (-80%)
Mutual labels:  rust-library
daemonize-me
Rust library to ease the task of creating daemons
Stars: ✭ 34 (-51.43%)
Mutual labels:  rust-library
soap-rs
SOAP client for Rust programming language
Stars: ✭ 37 (-47.14%)
Mutual labels:  rust-library
version-compare
↔️ Rust library to easily compare version strings. Mirror from https://gitlab.com/timvisee/version-compare
Stars: ✭ 32 (-54.29%)
Mutual labels:  rust-library
rust-pkcs11
Rust PKCS#11 Library
Stars: ✭ 70 (+0%)
Mutual labels:  rust-library
inline-c-rs
Write and execute C code inside Rust.
Stars: ✭ 121 (+72.86%)
Mutual labels:  rust-library
healthchecks-rs
Simple Rust library to interact with healthchecks.io
Stars: ✭ 16 (-77.14%)
Mutual labels:  rust-library
requests-rs
Rust HTTP client library styled after awesome Python requests
Stars: ✭ 37 (-47.14%)
Mutual labels:  rust-library
harsh
Hashids implementation in Rust
Stars: ✭ 48 (-31.43%)
Mutual labels:  rust-library
telnet-rs
A simple implementation of Telnet in Rust.
Stars: ✭ 35 (-50%)
Mutual labels:  rust-library

struct RGB for Rust crate

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

Installation

Add this to your Cargo.toml:

[dependencies]
rgb = "0.8"

Usage

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:

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

let px = RGB {
    r:255_u8,
    g:0,
    b:255,
};
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 functions 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();

Note: 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 crate).


About colorspaces

Correct color management is a complex problem, and this crate aims to be the lowest common denominator, so it's intentionally agnostic about it.

However, this library 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].