@electrovir/color
Version:
A wrapper for culori with an extremely simple API.
255 lines (254 loc) • 6.92 kB
TypeScript
import { type PartialWithUndefined, type UnionToIntersection, type Values } from '@augment-vir/common';
/**
* A hex color string.
*
* @category Internal
*/
export type HexColor = `#${string}`;
/**
* An individual definition for a single color format coordinate (like `h` or `s` or `r` or `g`).
*
* @category Internal
*/
export type ColorCoordinateDefinition = {
min: number;
max: number;
} & PartialWithUndefined<{
/**
* The number of digits to round this coordinate to.
*
* @default 0
*/
digits?: number | undefined;
/**
* A factor to multiple the values in the `Color` class by.
*
* @default 1
*/
factor?: number | undefined;
suffix?: string;
}>;
/**
* A single color format definition.
*
* @category Internal
*/
export type ColorFormatDefinition<ColorSpace extends string = string> = {
coords: Record<string, ColorCoordinateDefinition>;
/** This name for this color to be used in `Colorjs.to()`. Defaults to the color format key. */
colorSpace: ColorSpace;
};
/**
* All raw supported color formats.
*
* @category Internal
*/
export declare const rawColorFormats: {
readonly rgb: {
readonly coords: {
readonly r: {
readonly min: 0;
readonly max: 255;
readonly factor: 255;
};
readonly g: {
readonly min: 0;
readonly max: 255;
readonly factor: 255;
};
readonly b: {
readonly min: 0;
readonly max: 255;
readonly factor: 255;
};
};
readonly colorSpace: "rgb";
};
readonly hsl: {
readonly coords: {
readonly h: {
readonly min: 0;
readonly max: 360;
};
readonly s: {
readonly min: 0;
readonly max: 100;
readonly factor: 100;
readonly digits: 1;
};
readonly l: {
readonly min: 0;
readonly max: 100;
readonly factor: 100;
readonly digits: 1;
};
};
readonly colorSpace: "rgb";
};
readonly hwb: {
readonly coords: {
readonly h: {
readonly min: 0;
readonly max: 360;
};
readonly w: {
readonly min: 0;
readonly max: 100;
readonly factor: 100;
readonly digits: 1;
};
readonly b: {
readonly min: 0;
readonly max: 100;
readonly factor: 100;
readonly digits: 1;
};
};
readonly colorSpace: "rgb";
};
readonly lab: {
readonly coords: {
readonly l: {
readonly min: 0;
readonly max: 100;
readonly digits: 1;
};
readonly a: {
readonly min: -128;
readonly max: 127;
};
readonly b: {
readonly min: -128;
readonly max: 127;
};
};
readonly colorSpace: "lab";
};
readonly lch: {
readonly coords: {
readonly l: {
readonly min: 0;
readonly max: 100;
readonly digits: 1;
};
readonly c: {
readonly min: 0;
readonly max: 230;
};
readonly h: {
readonly min: 0;
readonly max: 360;
};
};
readonly colorSpace: "lab";
};
readonly oklab: {
readonly coords: {
readonly l: {
readonly min: 0;
readonly max: 1;
readonly digits: 3;
};
readonly a: {
readonly min: -0.5;
readonly max: 0.5;
readonly digits: 3;
};
readonly b: {
readonly min: -0.5;
readonly max: 0.5;
readonly digits: 3;
};
};
readonly colorSpace: "oklab";
};
readonly oklch: {
readonly coords: {
readonly l: {
readonly min: 0;
readonly max: 1;
readonly digits: 3;
};
readonly c: {
readonly min: 0;
readonly max: 0.4;
readonly digits: 3;
};
readonly h: {
readonly min: 0;
readonly max: 360;
readonly digits: 1;
};
};
readonly colorSpace: "oklab";
};
};
/**
* All supported color format names. This can be used as an enum.
*
* @category Internal
*/
export declare const ColorFormatName: { [FormatName in ColorFormatName]: FormatName; };
/**
* All supported color format names. This can be used as an enum.
*
* @category Internal
*/
export type ColorFormatName = keyof typeof rawColorFormats;
/**
* List the color coordinate keys (like `r` or `g` or `b`) for each supported color format in a
* union.
*
* @category Internal
*/
export type ColorCoordsByFormat = {
[FormatName in ColorFormatName]: keyof (typeof rawColorFormats)[FormatName]['coords'];
};
/**
* Type for {@link colorFormats}.
*
* @category Internal
*/
export type ColorFormats = Readonly<{
[FormatName in ColorFormatName]: ColorFormatDefinition<'colorSpace' extends keyof (typeof rawColorFormats)[FormatName] ? Extract<(typeof rawColorFormats)[FormatName], {
colorSpace: any;
}>['colorSpace'] : FormatName>;
}>;
/**
* All supported color formats.
*
* @category Color Format
*/
export declare const colorFormats: ColorFormats;
/**
* All available color space names.
*
* @category Internal
*/
export type ColorSpaceName = Values<typeof rawColorFormats>['colorSpace'];
/**
* All value types for all supported color formats.
*
* @category Internal
*/
export type ColorValues = {
[FormatName in ColorFormatName]: Record<keyof (typeof rawColorFormats)[FormatName]['coords'], number>;
};
/**
* All color formats grouped by their color space.
*
* @category Color Format
*/
export declare const colorSpaces: Record<"rgb" | "lab" | "oklab", Record<"rgb" | "hsl" | "hwb" | "lab" | "lch" | "oklab" | "oklch", ColorFormatDefinition<string>>>;
/**
* All color format names in an array.
*
* @category Internal
*/
export declare const colorFormatNames: ColorFormatName[];
/**
* All possible coordinate names for all supported color formats in a union.
*
* @category Internal
*/
export type ColorCoordinateName = keyof UnionToIntersection<Values<typeof rawColorFormats>['coords']>;