@electrovir/color
Version:
A wrapper for culori with an extremely simple API.
176 lines (175 loc) • 3.81 kB
JavaScript
import { getObjectTypedEntries, getObjectTypedKeys, getOrSet, mapObjectValues, } from '@augment-vir/common';
/**
* All raw supported color formats.
*
* @category Internal
*/
export const rawColorFormats = {
rgb: {
coords: {
r: {
min: 0,
max: 255,
factor: 255,
},
g: {
min: 0,
max: 255,
factor: 255,
},
b: {
min: 0,
max: 255,
factor: 255,
},
},
colorSpace: 'rgb',
},
hsl: {
coords: {
h: {
min: 0,
max: 360,
},
s: {
min: 0,
max: 100,
factor: 100,
digits: 1,
},
l: {
min: 0,
max: 100,
factor: 100,
digits: 1,
},
},
colorSpace: 'rgb',
},
hwb: {
coords: {
h: {
min: 0,
max: 360,
},
w: {
min: 0,
max: 100,
factor: 100,
digits: 1,
},
b: {
min: 0,
max: 100,
factor: 100,
digits: 1,
},
},
colorSpace: 'rgb',
},
lab: {
coords: {
l: {
min: 0,
max: 100,
digits: 1,
},
a: {
min: -128,
max: 127,
},
b: {
min: -128,
max: 127,
},
},
colorSpace: 'lab',
},
lch: {
coords: {
l: {
min: 0,
max: 100,
digits: 1,
},
c: {
min: 0,
max: 230,
},
h: {
min: 0,
max: 360,
},
},
colorSpace: 'lab',
},
oklab: {
coords: {
l: {
min: 0,
max: 1,
digits: 3,
},
a: {
min: -0.5,
max: 0.5,
digits: 3,
},
b: {
min: -0.5,
max: 0.5,
digits: 3,
},
},
colorSpace: 'oklab',
},
oklch: {
coords: {
l: {
min: 0,
max: 1,
digits: 3,
},
c: {
min: 0,
max: 0.4,
digits: 3,
},
h: {
min: 0,
max: 360,
digits: 1,
},
},
colorSpace: 'oklab',
},
};
/**
* All supported color format names. This can be used as an enum.
*
* @category Internal
*/
export const ColorFormatName = mapObjectValues(rawColorFormats, (colorName) => colorName);
/**
* All supported color formats.
*
* @category Color Format
*/
export const colorFormats = rawColorFormats;
/**
* All color formats grouped by their color space.
*
* @category Color Format
*/
export const colorSpaces = getObjectTypedEntries(colorFormats).reduce((accum, [colorFormatName, colorFormatDefinition,]) => {
getOrSet(accum, colorFormatDefinition.colorSpace, () => {
return {};
})[colorFormatName] = colorFormatDefinition;
return accum;
}, {});
/**
* All color format names in an array.
*
* @category Internal
*/
export const colorFormatNames = getObjectTypedKeys(colorFormats);