A Rust library for sorting and filtering colors in HEX, RGB(A) and HSL formats. This library does not follow any conventions, it justworks™ but may do so differently from what you would expect.
- Parse colors from multiple formats
- Hex
- 3-digit:
#f00
RGB - 4-digit:
#f00a
RGBA - 6-digit:
#ff0000
full RGB - 8-digit:
#ff0000aa
full RGBA
- 3-digit:
- RGB/RGBA:
rgb(255,0,0)
,rgba(255,0,0,0.5)
- HSL:
hsl(0,100%,50%)
- Hex
- Sort colors by spectrum, luminance, or opacity
- Filter colors by hue, saturation, or transparency
- Convert between color formats
use color_sort::*;
// Parse mixed color formats
let colors = parse_colors(&[
"#f00".to_string(),
"#ff0000".to_string(),
"#ff0000aa".to_string(),
"rgb(0, 255, 0)".to_string(),
"rgba(255, 0, 0, 0.5)".to_string(),
"hsl(240, 100%, 50%)".to_string(),
])?;
// Sort by spectrum (red -> yellow -> green -> cyan -> blue -> magenta)
let mut sorted = colors.clone();
sort_colors(&mut sorted);
// Sort by specific criteria
let mut by_luminance = colors.clone();
sort_colors_by(&mut by_luminance, SortOption::Luminance);
let mut by_opacity = colors.clone();
sort_colors_by(&mut by_opacity, SortOption::Opacity);
// Convert to format
let hex_colors = convert_colors(&colors, TargetFormat::Hex);
// Filter by hue
let filter = FilterOptions::default().with_hues([HueFilter::Red]);
let red_colors = filter_colors(&colors, &filter);
cat examples/colors.json | cargo run --example sort_hex_stdin