Resolve and convert CSS colors.
npm i @asamuzakjp/css-color
import { convert, resolve } from '@asamuzakjp/css-color';
const resolvedValue =
resolve('color-mix(in oklab, rgb(255 0 0), color(srgb 0 0.5 0 / 0.5))');
// 'oklab(0.5914 0.103273 0.119688 / 0.75)'
const convertedValue = covert.colorToHex('lab(46.2775% -47.5621 48.5837)');
// '#008000'
resolves CSS color
color
string color value
opt
object? options (optional, default {}
)
opt.currentColor
string? color to use for currentcolor
keywordopt.format
string? output format, one of computedValue
(default), specifiedValue
, rgb
, hex
, hexAlpha
computedValue
is a computed value of the colorspecifiedValue
is a specified value of the colorhexAlpha
is a hex color notation with alpha channel, i.e. #rrggbbaa
opt.key
any? key e.g. CSS property background-color
Returns (string? | Array) returns one of rgba?()
, color(space r g b / a)
, color(space x y z / a)
, lab(l a b / A)
, lch(l c h / a)
, oklab(l a b / A)
, oklch(l c h / a)
, \#rrggbb(aa)?
, color-name?
, null
, and, if key
is specified, [key, rgba?()]
etc.
computedValue
, values are numbers, however rgb()
values are integersspecifiedValue
, returns empty string
for unknown and/or invalid colorrgb
, values are rounded to integers, and returns rgba(0, 0, 0, 0)
for unknown colorshex
, returns null
for transparent
, and also returns null
if any of r
, g
, b
, a
is not a numberhexAlpha
, returns #00000000
for transparent
, however returns null
if any of r
, g
, b
, a
is not a numberContains various color conversion functions.
convert number to hex string
value
number color valueReturns string hex string: 00..ff
convert color to hex
value
string color valueopt
object? options (optional, default {}
)
opt.alpha
boolean? return in #rrggbbaa notationReturns string #rrggbb|#rrggbbaa
convert color to hsl
value
string color valueReturns Array<number> [h, s, l, a]
convert color to hwb
value
string color valueReturns Array<number> [h, w, b, a]
convert color to lab
value
string color valueReturns Array<number> [l, a, b, aa]
convert color to lch
value
string color valueReturns Array<number> [l, c, h, aa]
convert color to oklab
value
string color valueReturns Array<number> [l, a, b, aa]
convert color to oklch
value
string color valueReturns Array<number> [l, c, h, aa]
convert color to rgb
value
string color valueReturns Array<number> [r, g, b, a]
convert color to xyz
value
string color valueopt
object? options (optional, default {}
)
opt.d50
boolean? xyz in d50 white pointReturns Array<number> [x, y, z, a]
convert color to xyz-d50
value
string color valueReturns Array<number> [x, y, z, a]
Copyright (c) 2024 asamuzaK (Kazz)