Crate easy_color

Source
Expand description

A very simple and easy-to-use color conversion tool that can easily convert colors between Hex, RGB, RGBA, HSL, HSLA, HSV, and CMYK. And each type has its unique API, such as RGB type can set color channels, RGBA type can set transparency, HSL type can set hue, saturation, and brightness, etc.

§example:

use easy_color::{RGBA, RGB, HSL, Hex, ColorMix};
use crate::easy_color::{IntoRGB, IntoHex, IntoRGBA, IntoHSL, IntoHSLA, IntoHSV, IntoCMYK};
let hex:Hex = "#2bc48a".try_into().unwrap();

let mut rgb:RGB = hex.into();
// or
let mut rgb = hex.to_rgb();
assert_eq!(rgb.to_string(), "rgb(43,196,138)");
rgb.set_red(255);
assert_eq!(rgb.to_string(), "rgb(255,196,138)");

let mut rgba:RGBA = rgb.into();
// or
let mut rgba = rgb.to_rgba();
rgba.set_alpha(0.5);
assert_eq!(rgba.to_string(), "rgba(255,196,138,0.50)");

let mut hsl:HSL = rgba.into();
// or
let mut hsl = rgba.to_hsl();
hsl.set_hue(240);
assert_eq!(hsl.to_string(), "hsl(240,100%,88%)");

let hex:Hex = hsl.into();
// or
let hex = hsl.to_hex();
assert_eq!(hex.to_string(), "#C2C1FF");

// mix color
let hsl:HSL = (0,0,0).try_into().unwrap();
let rgba:RGBA = (255,255,255,1.0).try_into().unwrap();
rgba.mix(hsl, None).to_string(); // rgba(127,127,127,1.00)
rgba.mix(hsl, Some(0.35)).to_string(); // rgba(165,165,165,1.00)
hsl.mix(rgba, None).to_string(); // hsl(0,0%,50%)

// creat random color
let rgb = RGB::random();
let rgba = RGBA::random();
let hsl = HSL::random();

let hex:Hex = "#2bc48a".try_into().unwrap();
let hex_str = hex.to_rgb().set_blue(255).to_hsl().set_lightness(50).to_cmyk().set_cyan(100).to_hex().to_string(); // #00B5FF

Structs§

CMYK
CMYK can be parsed from a string in the format “cmyk(c,m,y,k)” or from a tuple (c,m,y,k).
HSL
HSL can be parsed from a string in the format “hsl(h, s%, l%)” or from a tuple (h,s,l).
HSLA
HSLA can be parsed from a string in the format “hsla(h, s%, l%, a)” or from a tuple (h,s,l,a).
HSV
HSV can be parsed from a string in the format “hsl(h, s%, v%)” or from a tuple (h,s,v).
Hex
Parse a hexadecimal string into a Hex object, which can be converted into RGB, RGBA, HSL, HSLA, HSV, and CMYK objects.
RGB
RGB can be parsed from a string in the format “rgb(r,g,b)” or from a tuple (r,g,b).
RGBA
RGBA can be parsed from a string in the format “rgba(r,g,b,a)” or from a tuple (r,g,b,a).

Enums§

ColorError

Traits§

Color
ColorMix
Darken
Grayscale
IntoCMYK
IntoHSL
IntoHSLA
IntoHSV
IntoHex
IntoRGB
IntoRGBA
Lighten
Negate