A pure MoonBit library for manipulating colors in different color spaces (RGB, LinearRGB, XYZ, LUV)
///|
test "trait-based conversions" {
let rgb_color = @colors.rgb(255, 128, 64)
let xyz_color = @colors.xyz(50.0, 60.0, 70.0)
let luv_color = @colors.luv(75.0, 25.0, -15.0)
// All types can convert to any other type using the same method names
let _rgb_from_rgb = rgb_color.to_rgb() // Identity conversion
let _rgb_from_xyz = xyz_color.to_rgb() // XYZ -> RGB
let _rgb_from_luv = luv_color.to_rgb() // LUV -> RGB
let _linear_from_rgb = rgb_color.to_linear_rgb()
let _xyz_from_rgb = rgb_color.to_xyz()
let _luv_from_rgb = rgb_color.to_luv()
// Blending works with trait methods too
let red = @colors.rgb(255, 0, 0)
let blue = @colors.rgb(0, 0, 255)
let _purple = red.blend(blue, 0.5)
}{
"deps": {
"bobzhang/colors": "*"
}
}moon testlet luv_white = @colors.luv(100.0, 0.0, 0.0) // L*=100 for white
inspect(luv_white, content="{l: 100, u: 0, v: 0}")impl ToLinearRGB for LUVColorlet linear_red = @colors.linear_rgb(1.0, 0.0, 0.0)
let _clamped = @colors.linear_rgb(2.0, -0.5, 0.8) // becomes LinearRGBColor(1.0, 0.0, 0.8)
inspect(linear_red, content="{lr: 1, lg: 0, lb: 0}")impl Blend for LinearRGBColorimpl ToLUV for LinearRGBColorimpl ToLinearRGB for LinearRGBColorimpl ToRGB for LinearRGBColorimpl ToXYZ for LinearRGBColorimpl Show for LinearRGBColorlet red = @colors.rgb(255, 0, 0)
let _clamped = @colors.rgb(300, -50, 128) // becomes RGBColor(255, 0, 128)
inspect(red, content="{r: 255, g: 0, b: 0}")impl ToLinearRGB for RGBColorlet xyz_white = @colors.xyz(95.047, 100.0, 108.883) // D65 white point
inspect(xyz_white, content="{x: 95.047, y: 100, z: 108.883}")impl ToLinearRGB for XYZColorlet linear_red = @colors.linear_rgb(1.0, 0.0, 0.0)
let _clamped = @colors.linear_rgb(2.0, -0.5, 0.8)
inspect(linear_red, content="{lr: 1, lg: 0, lb: 0}")let red = @colors.rgb(255, 0, 0)
let _clamped = @colors.rgb(300, -50, 128) // becomes RGBColor(255, 0, 128)
inspect(red, content="{r: 255, g: 0, b: 0}")A pure MoonBit library for manipulating colors in different color spaces (RGB, LinearRGB, XYZ, LUV)