| 1 |
/** |
| 2 |
* @packageDocumentation |
| 3 |
* Defines the class and other types related to creating color spaces. |
| 4 |
* For the builtin color spaces, see the `spaces` module. |
| 5 |
*/ |
| 6 |
import { White } from "./adapt.js"; |
| 7 |
import { ColorConstructor, Coords, ColorTypes } from "./color.js"; |
| 8 |
import type FormatClass from "./Format.js"; |
| 9 |
import type { instance } from "./Format.js"; |
| 10 |
import type { Matrix3x3 } from "./types.js"; |
| 11 |
import RGBColorSpace from "./RGBColorSpace.js"; |
| 12 |
|
| 13 |
export interface Format { |
| 14 |
/** @default "function" */ |
| 15 |
type?: string | undefined; |
| 16 |
/** @default "color" */ |
| 17 |
name?: string | undefined; |
| 18 |
id?: string | undefined; |
| 19 |
ids?: string[] | undefined; |
| 20 |
coords?: string[] | undefined; |
| 21 |
coordGrammar?: (string & { range?: [number, number] })[] | undefined; |
| 22 |
space?: ColorSpace | undefined; |
| 23 |
serializeCoords?: ((coords: Coords, precision: number) => [string, string, string]) | undefined; |
| 24 |
/** Whether to adjust the coordinates to fit in the gamut */ |
| 25 |
toGamut?: boolean | undefined; |
| 26 |
/** Whether commas should separate arguments for a format */ |
| 27 |
commas?: boolean | undefined; |
| 28 |
/** Whether to always have alpha at the end (true), never (false), or auto (undefined) */ |
| 29 |
alpha?: boolean | undefined; |
| 30 |
test?: ((str: string) => boolean) | undefined; |
| 31 |
/** Function to parse a string into a color */ |
| 32 |
parse?: ((str: string) => ColorConstructor | undefined | null) | undefined; |
| 33 |
/** |
| 34 |
* Serialize coordinates and an alpha channel into a string. |
| 35 |
* Must be defined for a format to support serialization |
| 36 |
*/ |
| 37 |
serialize?: ((coords: Coords, alpha: number, opts?: Record<string, any>) => string) | undefined; |
| 38 |
[instance]?: FormatClass | undefined; |
| 39 |
} |
| 40 |
|
| 41 |
export interface CoordMeta { |
| 42 |
name?: string | undefined; |
| 43 |
type?: string | undefined; |
| 44 |
range?: [number, number] | undefined; |
| 45 |
refRange?: [number, number] | undefined; |
| 46 |
} |
| 47 |
|
| 48 |
export interface SpaceOptions { |
| 49 |
/** Id of this space, used in things such as conversions */ |
| 50 |
id: string; |
| 51 |
/** The readable name of the space, used in user-facing UI */ |
| 52 |
name: string; |
| 53 |
/** The base color space */ |
| 54 |
base?: string | ColorSpace | null | undefined; |
| 55 |
/** |
| 56 |
* Function that converts coords in the base color space to coords in this color space. |
| 57 |
* Must perform chromatic adaptation if needed |
| 58 |
*/ |
| 59 |
fromBase?: ((coords: Coords) => number[]) | undefined; |
| 60 |
/** |
| 61 |
* Function that converts coords in this color space to coords in the base color space. |
| 62 |
* Must perform chromatic adaptation if needed |
| 63 |
*/ |
| 64 |
toBase?: ((coords: Coords) => number[]) | undefined; |
| 65 |
/** |
| 66 |
* Object mapping coord ids to coord metadata |
| 67 |
* @see {@link CoordMeta} |
| 68 |
*/ |
| 69 |
coords?: Record<string, CoordMeta> | undefined; |
| 70 |
white?: string | White | undefined; |
| 71 |
/** The ID used by CSS, such as `display-p3` or `--cam16-jmh` */ |
| 72 |
cssId?: string | undefined; |
| 73 |
referred?: string | undefined; |
| 74 |
/** |
| 75 |
* Details about string formats to parse from / serialize to |
| 76 |
* @see {@link Format} |
| 77 |
*/ |
| 78 |
formats?: Record<string, Format> | undefined; |
| 79 |
gamutSpace?: "self" | string | ColorSpace | null | undefined; |
| 80 |
/** |
| 81 |
* Matrices relevant to this color space, keyed by a descriptive name. |
| 82 |
* Exposed on the resulting space as {@link ColorSpace.M} so that consumer |
| 83 |
* code can reuse them instead of duplicating the data. |
| 84 |
*/ |
| 85 |
M?: Record<string, Matrix3x3> | undefined; |
| 86 |
/** |
| 87 |
* Spaces to try if not natively supported, if different from base chain. |
| 88 |
*/ |
| 89 |
displaySpaces?: (string | ColorSpace)[] | undefined; |
| 90 |
aliases?: string[] | undefined; |
| 91 |
ε?: number | undefined; |
| 92 |
rgbGamut?: RGBColorSpace | undefined; |
| 93 |
linearGamut?: RGBColorSpace | undefined; |
| 94 |
} |
| 95 |
|
| 96 |
export type Ref = |
| 97 |
| string |
| 98 |
| [string | ColorSpace, string] |
| 99 |
| { space: string | ColorSpace; coordId: string }; |
| 100 |
|
| 101 |
/** Class for color spaces. Each color space corresponds to a `ColorSpace` instance */ |
| 102 |
export default class ColorSpace { |
| 103 |
constructor (options: SpaceOptions); |
| 104 |
|
| 105 |
static DEFAULT_FORMAT: { type: "functions"; name: "color" }; |
| 106 |
|
| 107 |
/** |
| 108 |
* @throws {TypeError} If no matching color space is found |
| 109 |
*/ |
| 110 |
static get (space: ColorSpace | string, ...alternatives: (ColorSpace | string)[]): ColorSpace; |
| 111 |
|
| 112 |
/** |
| 113 |
* @throws {TypeError} If no space or an unknown space is provided |
| 114 |
*/ |
| 115 |
static resolveCoord ( |
| 116 |
ref: Ref, |
| 117 |
workingSpace?: string | ColorSpace, |
| 118 |
): CoordMeta & { |
| 119 |
id: string; |
| 120 |
index: number; |
| 121 |
space: ColorSpace; |
| 122 |
}; |
| 123 |
|
| 124 |
/** |
| 125 |
* @throws {TypeError} If a space with the provided id already exists |
| 126 |
*/ |
| 127 |
static register (space: ColorSpace): ColorSpace; |
| 128 |
static register (id: string, space: ColorSpace): ColorSpace; |
| 129 |
|
| 130 |
static registry: Record<string, ColorSpace>; |
| 131 |
|
| 132 |
static get all (): ColorSpace[]; |
| 133 |
|
| 134 |
static findFormat (filters: object | string, spaces?: ColorSpace[]): FormatClass | null; |
| 135 |
|
| 136 |
/** The ID used by CSS, such as `display-p3` or `--cam16-jmh` */ |
| 137 |
get cssId (): string; |
| 138 |
get isPolar (): boolean; |
| 139 |
get isUnbounded (): boolean; |
| 140 |
|
| 141 |
name: string; |
| 142 |
id: string; |
| 143 |
aliases?: string[] | undefined; |
| 144 |
base: ColorSpace | null; |
| 145 |
/** This space's ancestors, ordered closest first (immediate base → root), excluding this space */ |
| 146 |
bases: ColorSpace[]; |
| 147 |
/** Spaces to try when serializing for display and this space is not natively supported */ |
| 148 |
displaySpaces?: ColorSpace[] | undefined; |
| 149 |
coords: Record<string, CoordMeta>; |
| 150 |
fromBase?(coords: Coords): Coords; |
| 151 |
toBase?(coords: Coords): Coords; |
| 152 |
formats: Record<string, Format>; |
| 153 |
/** Matrices relevant to this color space, keyed by a descriptive name */ |
| 154 |
M: Record<string, Matrix3x3>; |
| 155 |
referred?: string | undefined; |
| 156 |
white: White; |
| 157 |
gamutSpace: ColorSpace; |
| 158 |
rgbGamut?: RGBColorSpace | undefined; |
| 159 |
linearGamut?: RGBColorSpace | undefined; |
| 160 |
|
| 161 |
from (color: ColorTypes): Coords; |
| 162 |
from (space: string | ColorSpace, coords: Coords): Coords; |
| 163 |
|
| 164 |
getFormat (format?: string | Format | FormatClass): FormatClass | null; |
| 165 |
|
| 166 |
getMinCoords (): Coords; |
| 167 |
|
| 168 |
inGamut (coords: Coords, options?: { epsilon?: number }): boolean; |
| 169 |
|
| 170 |
to (color: ColorTypes): Coords; |
| 171 |
to (space: string | ColorSpace, coords: Coords): Coords; |
| 172 |
|
| 173 |
toString (): string; |
| 174 |
|
| 175 |
equals (space: string | ColorSpace): boolean; |
| 176 |
} |