Validated sRGB and HSL conversion with WCAG contrast utilities
git clone https://github.com/elianue/leya-colors.git
cd leya-colors
moon check
moon test
moon run cmd/mainmoon add elianue/leya-colors@0.1.0import {
"elianue/leya-colors" @colors,
}members = [ ".", "../leya-colors" ]import {
"elianue/leya-colors@0.1.0",
}import {
"elianue/leya-colors" @colors,
}let color = @colors.from_hex("#369").unwrap() // Known-valid literal
let (r, g, b) = color.channels() // (51, 102, 153)
let hex = color.to_hex() // "#336699"
let hsl = color.to_hsl()
let original = hsl.to_rgb()
let white = @colors.rgb(255, 255, 255).unwrap()
let ratio = @colors.contrast_ratio(color, white)
let readable = @colors.passes_contrast(color, white, @colors.AA, @colors.Normal)match @colors.from_hex(user_input) {
Ok(color) => println(color.to_hex())
Err(@colors.InvalidHex) => println("Expected 3 or 6 hexadecimal digits")
Err(_) => println("Invalid color")
}cd examples/consumer
moon check
moon test
moon run .| API | Behavior |
|---|---|
| rgb(r, g, b) | Result[Rgb, ColorError]; integer channels 0–255 |
| hsl(h, s, l) | Result[Hsl, ColorError]; degrees 0–360, percentages 0–100 |
| from_hex(text) | Result[Rgb, ColorError]; 3 or 6 ASCII hex digits, optional hash |
| hex_to_hsl(text) | HEX directly to Result[Hsl, ColorError] |
| Rgb::channels() | (Int, Int, Int) |
| Rgb::to_hex() | Canonical uppercase #RRGGBB |
| Rgb::to_hsl() | HSL with fractional precision retained |
| Hsl::components() | (Double, Double, Double) degrees/percent/percent |
| Hsl::to_rgb() / to_hex() | RGB rounded to the nearest integer |
| Rgb::relative_luminance() | Double in 0–1 |
| contrast_ratio(a, b) | Symmetric ratio in 1–21 |
| required_contrast(level, size) | Required text contrast ratio |
| passes_contrast(a, b, level, size) | Compare the unrounded ratio to its threshold |
| Level | Normal text | Large text |
|---|---|---|
| AA | 4.5:1 | 3:1 |
| AAA | 7:1 | 4.5:1 |
moon fmt --check
moon check
moon test
moon build
moon run cmd/main
moon info