colorsys
A module for color conversion and mutation written in Rust. For now works with next color models:
- RGB(a)( as hexadecimal too)
- HSL(a)
- CMYK(a)
- ANSI256 codes
What It Can Do
getters & setters
use ;
let rgb = from;
let r = rgb.red;
// 57.3
let mut hsl = default;
// Hsl { h: 0, s: 0, l: 0, a: 1 }
hsl.set_saturation;
hsl.set_saturation;
hsl.set_alpha;
// Hsl { h: 305.71, s: 13.98, l: 0, a: 0.75 }
conversion
See From/FromStr/Into
traits implementation in docs for more info
use ;
let rbga_tuple = ;
let rgba = from;
let hsla: Hsl = rgba.as_ref.into;
// ~Hsl { h: 305.78, s: 63.71, l: 13.73, a: 0.33 }
let rgb_arr: = from.into;
// ~[57, 13, 53]
let hsla_tuple: = from.into;
// ~Hsl { h: 305.78, s: 63.71, l: 13.73, a: 1 }
let hex: String = rgba.to_hex_string;
// #390d35
//
// From/Into
//
let rgb1 = from_hex_str.unwrap;
let rgb2 = from;
assert_eq!;
//
// Ratio
//
use ;
let blue = from;
let ratio: = blue.as_ratio.into;
// ~[0.133, 0.435, 0.922, 1.0]
let converted: Rgb = from.into;
assert!;
modification
See ColorTransform/Add*/Sub*..
traits in docs for more
use ;
let mut rgb: Rgb = .into;
rgb.lighten;
// ~Rgb { r: 249.83, g: 201.80, b: 150.67 }
rgb.opacify;
rgb.saturate;
// ~Rgb { r: 230.29, g: 201.19, b: 170.21, a: 0.3 }
rgb.grayscale_simple;
// ~Rgb { r: 200.255, g: 200.255, b: 200.255, a: 0.3 }
let mut hsl = from;
hsl.opacify;
// ~Hsl { h: 0.0, s: 0.0, l: 78.53 }
hsl.adjust_hue;
hsl.saturate;
let mut rgb2: Rgb = hsl.as_ref.into;
// ~Rgb { r: 169.76, g: 177.9, b: 230.75}
rgb2.invert;
// ~Rgb { r: 85.24, g: 77.09, b: 24.25 }
let rgb3 = rgb - rgb2;
// ~Rgb { r: 115.01, g: 123.16, b: 176.0 }
let hsl2 = hsl + rgb3.into;
// ~Hsl { h: 0.0, s: 83.55, l: 100.0 }
parsing from string & css string representation
use ;
use FromStr;
let s = "rgb(177, 255, 176)";
let rgb: Rgb = s.parse.unwrap;
rgb.to_css_string;
// String: "rgb(177,255,176)"
rgb.to_hex_string;
// #b1ffb0
from_str.unwrap.to_css_string;
// String: hsl(168,52%,42%)
no_std
Crate has a Cargo feature named "std"
that is enabled by default.
In order to use colorsys
in a no_std
context this feature needs to be disabled.
Modify your dependency to opt out of enabled-by-default features.
[]
= { = "*", = false }
Color unit ranges
All color units is f64. Here are their ranges:
- red: 0.0 - 255.0
- green: 0.0 - 255.0
- blue: 0.0 - 255.0
- hue: 0.0 - 360.0
- saturation: 0.0 - 100.0
- lightness: 0.0 - 100.0
- all in cmyk are: 0.0 - 100.0
- alpha: 0.0 - 1.0
- ansi256 code is
u8
If you specify a value that does not fit within these ranges, they are replaced with a minimum or maximum value.
Enjoy using!
License
This module is MIT licensed.