A Millwork codec for colors with culori.
pnpm add @millwork-ts/color @millwork-ts/core culori
| Codec | Value | Raw |
|---|---|---|
color |
a frozen culori color object | a CSS color string or an rgb object |
The codec reads these raw forms:
| Raw | Example | Value mode |
|---|---|---|
| hex | "#1e90ff", "#1e90ff80", "#fff" |
rgb |
rgb() |
"rgb(30 144 255)" |
rgb |
hsl() |
"hsl(210 50% 40%)" |
hsl |
oklch() |
"oklch(0.65 0.18 250)" |
oklch |
| rgb object | { "r": 30, "g": 144, "b": 255, "a": 0.5 } |
rgb |
r, g and b are 0 to 255. The alpha a is 0 to 1."red".| Option | Type | Use |
|---|---|---|
format |
'hex' | 'rgb' | 'hsl' | 'oklch' |
The written form. The default is 'hex'. |
format |
Written form |
|---|---|
'hex' |
"#1e90ff", or "#1e90ff80" when alpha is less than 1 |
'rgb' |
"rgb(30, 144, 255)" |
'hsl' |
"hsl(210, 50%, 40%)" |
'oklch' |
"oklch(0.65 0.18 250)" |
Hex, rgb() and hsl() are in the sRGB gamut. An oklch() color out of that gamut is
clipped when you write it in one of these forms.
equals converts the two colors to rgb. It compares each channel and the alpha with a
tolerance of half of one 8-bit step. Thus "#336699" equals "hsl(210 50% 40%)", and
"#336698" does not equal "#336699".
Register the codecs before the store calls transformations(), because transformations()
reads the registry when it runs. See the
@millwork-ts/warp-drive README.
import { codecs } from '@millwork-ts/color'
import { registerCodecs, valueField } from '@millwork-ts/warp-drive'
import { withDefaults } from '@warp-drive/core/reactive'
registerCodecs(codecs)
export const ThemeSchema = withDefaults({
type: 'theme',
fields: [valueField('accent', 'color', { format: 'oklch' })],
})