color

A library for color space conversions in MoonBit

color
color-conversion
color-space
moon add CAIMEOX/color@0.1.0
Download zip
Author
Version
0.1.0
License
Apache-2.0
Last updated
10 months ago
Downloads
25
README

#Color Convert

High‑quality color space conversion utilities for the MoonBit language. This library provides a focused, dependency‑free set of functions to convert between many common (and some modern) color spaces used in graphics, imaging, CSS, and perceptual color work.

#Highlights

  • sRGB inter‑conversion with: HSV, HSL, HCG, HWB, CMYK, Gray, XYZ (D65), CIE Lab, CIE LCH, OKLab, OKLCH, Apple 16‑bit RGB
  • Hex string parsing & formatting (#RGB, #RRGGBB, plain forms)
  • Named CSS colors → RGB (case‑insensitive) and reverse keyword lookup
  • Modern perceptual color support (OKLab / OKLCH) with XYZ bridge
  • Pure functions, no global mutation, no external deps
  • Comprehensive tests for numerical correctness

#Installation

Add the module to your MoonBit project (assuming standard MoonBit tooling):

moon add CAIMEOX/color

write simple code like:

let (h, s, v) = rgb2hsv(255.0F, 0.0F, 0.0F)
let (r, g, b) = hsl2rgb(240.0F, 100.0F, 50.0F)
let hex = rgb2hex(r, g, b) // "0000FF"

#Usage Examples

// Name lookup
let rgb = name2rgb("rebeccapurple")

// Round‑trip through OKLab
let (l, a, b) = rgb2oklab(92.0F, 191.0F, 84.0F)
let (rr, gg, bb) = oklab2rgb(L, A, B)

// Hex to RGB
let (r2, g2, b2) = hex2rgb("#ABCDEF") // (171,205,239)

// XYZ ↔ Lab ↔ LCH
let (x, y, z) = rgb2xyz(255.0F, 255.0F, 255.0F)
let (l, a, b3) = xyz2lab(x, y, z)
let (_, c, h) = lab2lch(l, a, b3)

#API Overview

All numeric channel inputs & outputs follow common conventions:

  • sRGB channels: Float 0–255 (matches test expectations)
  • Percentage style outputs: Many functions return saturation / value / lightness / chroma / gray as 0–100 floats (mirroring common JS libs like color‑convert)
  • Hex: uppercase string without leading # (add it yourself if needed)
  • XYZ: D65 white, scaled 0–100

FromToFunction
RGBHSVrgb2hsv
RGBHSLrgb2hsl
RGBCMYKrgb2cmyk
RGBHexrgb2hex
RGBHWBrgb2hwb
RGBHCGrgb2hcg
RGBXYZrgb2xyz
RGBLabrgb2lab
RGBGray%rgb2gray
RGBOKLabrgb2oklab
RGBApple 16‑bitrgb2apple
HSVRGBhsv2rgb
HSVHSLhsv2hsl
HSLRGBhsl2rgb
HSLHSVhsl2hsv
HWBRGBhwb2rgb
HCGRGBhcg2rgb
CMYKRGBcmyk2rgb
XYZRGBxyz2rgb
XYZLabxyz2lab
LabXYZlab2xyz
LabLCHlab2lch
LCHLablch2lab
OKLabXYZoklab2xyz
OKLabRGBoklab2rgb
OKLabOKLCHoklab2oklch
OKLCHOKLaboklch2oklab
Gray%RGBgray2rgb
HexRGBhex2rgb
NameRGBname2rgb (raises NameNotFound)
RGBNamergb2keyword (optional)
Apple 16‑bitRGBapple2rgb

#Error Handling

  • name2rgb raises NameNotFound if the color keyword does not exist.
  • hex2rgb raise error for malformed inputs (guard pattern); you can pre‑validate if stricter behavior is desired.

#Precision Notes

Floating point operations introduce minor rounding differences. Tests assert stringified tuple outputs produced on the reference platform. When writing new tests allow for small epsilon (≈1e‑3–1e‑2) if comparing numerically.

#Running Tests

moon test

Update expected outputs after intentional algorithm changes with:

moon test -u

#License

Apache 2.0 — see LICENSE file.

#Acknowledgments

Thanks to color-convert and color-name for reference data and inspiration