hex_color
A simple, lightweight library for working with RGB(A) hexadecimal colors.
Documentation
Module documentation with examples. The module documentation also includes a comprehensive description of the syntax supported for parsing hex colors.
Usage
This crate is on crates.io and can be used by adding hex_color
to your dependencies in your project's Cargo.toml:
[]
= "3"
Examples
Basic parsing:
use HexColor;
let cyan = parse?;
assert_eq!;
let transparent_plum = parse?;
assert_eq!;
// Strictly enforce only an RGB color through parse_rgb:
let pink = parse_rgb?;
assert_eq!;
// Strictly enforce an alpha component through parse_rgba:
let opaque_white = parse_rgba?;
assert_eq!;
Flexible constructors:
use HexColor;
let violet = rgb;
let transparent_maroon= rgba;
let transparent_gray = GRAY.with_a;
let lavender = from_u24;
let transparent_lavender = from_u32;
let floral_white = WHITE
.with_g
.with_b;
Comprehensive arithmetic:
use HexColor;
assert_eq!;
assert_eq!;
assert_eq!;
With rand
Using rand + std features to generate random colors via rand
out of the box:
use HexColor;
let random_rgb: HexColor = random;
To specify whether an RGB or RGBA color is randomly created, use
HexColor::random_rgb or HexColor::random_rgba respectively:
use HexColor;
let random_rgb = random_rgb;
let random_rgba = random_rgba;
With serde
Use serde to serialize and deserialize colors in multiple
formats: u24, u32, rgb, or rgba:
use HexColor;
use ;
use json;
let json_input = json!;
assert_eq!;
let my_color = Color ;
assert_eq!;
let json_input = json!;
assert_eq!;
let my_color = NumericColor ;
assert_eq!;
Features
randenables out-of-the-box compatability with therandcrate.serdeenables serialization and deserialization with theserdecrate.stdenablesstd::error::ErroronParseHexColorError. Otherwise, it's needed withrandforHexColor::random_rgb,HexColor::random_rgba, and, of course,rand::random.
Note: Only the std feature is enabled by default.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.