Comprehensive color manipulation library for MoonBit - color space conversions, interpolation, contrast calculation, and palette generation
moon add weedmind-6812/moonbit-color// From RGB integers (0-255)
let orange = @color.RGB::from_int(255, 165, 0)
// From hex string
let blue = @color.RGB::from_hex("#0080FF")
// Convert to hex
println(orange.to_hex()) // "#FFA500"let rgb = @color.RGB::from_int(255, 87, 51)
// RGB to HSL
let hsl = rgb.to_hsl()
println("H: " + hsl.h.to_string()) // Hue: 0-360
// HSL back to RGB
let rgb2 = hsl.to_rgb()
// RGB to HSV
let hsv = rgb.to_hsv()let red = @color.RGB::from_int(255, 0, 0)
let blue = @color.RGB::from_int(0, 0, 255)
// Linear interpolation
let purple = @color.RGB::lerp(red, blue, 0.5)
// Generate gradient
let gradient = @color.RGB::gradient(red, blue, 10)
for i = 0; i < gradient.length(); i = i + 1 {
println(gradient[i].to_hex())
}
// Multi-stop gradient
let rainbow = @color.RGB::multi_gradient([
@color.RGB::from_hex("#FF0000"),
@color.RGB::from_hex("#00FF00"),
@color.RGB::from_hex("#0000FF")
], 20)let white = @color.RGB::from_int(255, 255, 255)
let black = @color.RGB::from_int(0, 0, 0)
// Calculate contrast ratio
let ratio = @color.RGB::contrast_ratio(white, black)
println("Contrast: " + ratio.to_string() + ":1") // ~21:1
// Check WCAG compliance
if @color.RGB::meets_wcag_aa(white, black) {
println("Passes WCAG AA (4.5:1)")
}
if @color.RGB::meets_wcag_aaa(white, black) {
println("Passes WCAG AAA (7:1)")
}let base = @color.RGB::from_int(255, 100, 50)
// Complementary color (opposite on color wheel)
let comp = base.complementary()
// Analogous palette (adjacent colors)
let analogous = base.analogous(5)
// Triadic palette (120° apart)
let triadic = base.triadic()
// Tetradic/square palette (90° apart)
let tetradic = base.tetradic()
// Monochromatic (same hue, varying lightness)
let mono = base.monochromatic(7)
// Split complementary
let split = base.split_complementary()| Method | Description |
|---|---|
| RGB::from_int(r, g, b) | Create from 0-255 integers |
| RGB::from_hex(hex) | Parse hex string (#RRGGBB) |
| to_hex() | Convert to hex string |
| to_hsl() | Convert to HSL |
| to_hsv() | Convert to HSV |
| lerp(a, b, t) | Linear interpolation |
| gradient(start, end, steps) | Generate gradient |
| multi_gradient(colors, steps) | Multi-stop gradient |
| luminance() | Calculate relative luminance |
| contrast_ratio(a, b) | WCAG contrast ratio |
| meets_wcag_aa(fg, bg) | Check AA compliance (4.5:1) |
| meets_wcag_aaa(fg, bg) | Check AAA compliance (7:1) |
| complementary() | Generate complement |
| analogous(count) | Analogous palette |
| triadic() | Triadic palette |
| tetradic() | Tetradic palette |
| monochromatic(count) | Monochromatic palette |
| split_complementary() | Split complementary |
| Method | Description |
|---|---|
| HSL::to_rgb() | Convert to RGB |
| HSV::to_rgb() | Convert to RGB |
| HSL::lerp(a, b, t) | Interpolate in HSL space |
moonbit-color/
├── color.mbt # Core types & conversions
├── interpolation.mbt # Color interpolation & gradients
├── contrast.mbt # WCAG contrast calculations
├── palette.mbt # Palette generation algorithms
├── color_test.mbt # 18 unit tests
├── moon.mod.json # Package metadata
├── moon.pkg.json # Package config
└── README.mdmoon test # Run 18 unit tests
moon check # Type check
moon build # Compilepub struct HSL {
h : Double
s : Double
l : Double
}pub struct HSV {
h : Double
s : Double
v : Double
}pub struct RGB {
r : Double
g : Double
b : Double
}Install
Download zipComprehensive color manipulation library for MoonBit - color space conversions, interpolation, contrast calculation, and palette generation