Skip to content

Commit

Permalink
Backport examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Aug 4, 2024
1 parent 51d0fc3 commit 043d8fe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rgb"
version = "0.8.47"
authors = ["Kornel Lesiński <kornel@geekhood.net>"]
authors = ["Kornel Lesiński <kornel@geekhood.net>", "James Forster <james.forsterer@gmail.com>"]
include = ["src/**/*", "Cargo.toml", "README.md", "examples/*.rs", "LICENSE"]
description = "`struct RGB/RGBA/etc.` for sharing pixels between crates + convenience methods for color manipulation.\nAllows 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."
documentation = "https://docs.rs/rgb"
Expand Down
24 changes: 18 additions & 6 deletions examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
use rgb::*;

#[cfg(feature = "as-bytes")]
fn main() {
use rgb::{ComponentBytes, ComponentSlice, ComponentMap};
use rgb::Rgb;

let px = RGB{r:255_u8,g:0,b:100};
let px = Rgb {
r: 255_u8,
g: 0,
b: 100,
};
assert_eq!([px].as_bytes()[0], 255);

let bigpx = RGB16{r:65535_u16,g:0,b:0};
let bigpx = Rgb::<u16> {
r: 65535_u16,
g: 0,
b: 0,
};
assert_eq!(bigpx.as_slice()[0], 65535);

let px = RGB8::new(255, 0, 255);
let inverted: RGB8 = px.map(|ch| 255 - ch);
let px = Rgb::<u8>::new(255, 0, 255);
let inverted: Rgb<u8> = px.map(|ch| 255 - ch);

println!("{inverted}"); // rgb(0,255,0)
}

#[cfg(not(feature = "as-bytes"))]
fn main() {}
4 changes: 2 additions & 2 deletions examples/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use rgb::*;
// Run using: cargo run --features=serde --example serde

fn main() {
let color = RGB { r: 255_u8, g: 0, b: 100 };
let color = Rgb { r:255_u8, g:0, b:100 };
println!("{}", serde_json::to_string(&color).unwrap());

let color: RGB8 = serde_json::from_str("{\"r\":10,\"g\":20,\"b\":30}").unwrap();
let color: Rgb<u8> = serde_json::from_str("{\"r\":10,\"g\":20,\"b\":30}").unwrap();
println!("{}", color);
}

0 comments on commit 043d8fe

Please sign in to comment.