Module: css-color-parser

CSS Color Module Level 4 parsing and formatting utilities. Supports modern CSS color syntax including color(), lab(), lch(), oklab(), oklch(), and traditional formats (hex, rgb, hsl).
Source:
See:

Methods

(static) formatCSS(color, formatopt) → {string}

Format an sRGB color as CSS string
Parameters:
Name Type Attributes Default Description
color SrgbColor sRGB color
format string <optional>
'hex' Output format
Source:
Returns:
CSS color string
Type
string
Example
formatCSS({ r: 1, g: 0, b: 0 }, 'hex') // '#ff0000'
formatCSS({ r: 1, g: 0, b: 0 }, 'rgb') // 'rgb(255 0 0)'
formatCSS({ r: 1, g: 0, b: 0 }, 'hsl') // 'hsl(0deg 100% 50%)'

(static) parseCSS(cssString) → {SrgbColor|null}

Parse any CSS color string to sRGB Supports CSS Color Module Level 4 syntax
Parameters:
Name Type Description
cssString string CSS color string
Source:
Returns:
Parsed sRGB color or null if invalid
Type
SrgbColor | null
Example
parseCSS('rgb(255 0 0)') // { r: 1, g: 0, b: 0 }
parseCSS('#ff0000') // { r: 1, g: 0, b: 0 }
parseCSS('color(srgb 1 0 0)') // { r: 1, g: 0, b: 0 }
parseCSS('lab(50% 50 0)') // Converts Lab to sRGB
parseCSS('oklch(0.5 0.2 30deg)') // Converts OkLCh to sRGB