Debian Codegraph @

node/colorjs.io/src/color.d.ts

raw
1 /**
2 * @packageDocumentation
3 * This module defines the main {@link Color} class as well as the types it uses.
4 */
5 import { WHITES } from "./adapt.js";
6 import defaults from "./defaults.js";
7 import hooks from "./hooks.js";
8 import * as util from "./util.js";
9 import ColorSpace, { Ref } from "./ColorSpace.js";
10 import SpaceAccessors from "./space-coord-accessors.js";
11 import { ToGamutOptions, ParseOptions, TryColorOptions } from "./types.js";
12
13 import {
14 to as toFn,
15 parse,
16 serialize,
17 inGamut as inGamutFn,
18 toGamut as toGamutFn,
19 distance as distanceFn,
20 equals as equalsFn,
21 get,
22 getAll as getAllFn,
23 display,
24 } from "./index-fn.js";
25
26 import { uv, xy } from "./chromaticity.js";
27 import contrast from "./contrast.js";
28 import {
29 contrastWCAG21,
30 contrastAPCA,
31 contrastMichelson,
32 contrastWeber,
33 contrastLstar,
34 contrastDeltaPhi,
35 } from "./contrast/index.js";
36 import deltaE from "./deltaE.js";
37 import deltaEMethods, {
38 deltaE76,
39 deltaECMC,
40 deltaE2000,
41 deltaEJz,
42 deltaEITP,
43 deltaEOK,
44 } from "./deltaE/index.js";
45 import { range, Range, MixOptions, StepsOptions } from "./interpolation.js";
46 import { getLuminance } from "./luminance.js";
47 import { lighten, darken } from "./variations.js";
48
49 export type { SpaceAccessor } from "./space-coord-accessors.js";
50
51 export type Coords = [number | null, number | null, number | null];
52
53 export interface ColorObject {
54 spaceId?: string | ColorSpace | undefined;
55 space?: string | ColorSpace | undefined;
56 coords: Coords;
57 alpha?: number | undefined | null;
58 }
59
60 export interface PlainColorObject {
61 space: ColorSpace;
62 coords: Coords;
63 alpha: number | null;
64 }
65
66 export interface ColorConstructor {
67 spaceId: string;
68 coords: Coords;
69 alpha: number | undefined | null;
70 }
71
72 export type ColorTypes = ColorObject | ColorConstructor | string | PlainColorObject;
73
74 export type DefineFunctionCode = (...args: any[]) => any;
75
76 export interface DefineFunctionOptions {
77 instance?: boolean | undefined;
78 returns?: "color" | "function<color>" | "array<color>" | undefined;
79 }
80
81 export type DefineFunctionHybrid = DefineFunctionCode & DefineFunctionOptions;
82
83 /** Remove the first element of an array type */
84 type RemoveFirstElement<T extends any[]> = T extends [any, ...infer R] ? R : T[number][];
85
86 /** Convert a function to a prototype for Color */
87 export type ToColorPrototype<T extends (...args: any[]) => any> = T extends (
88 color: Color,
89 ...args: infer A
90 ) => infer R
91 ? T extends { returns: "color" }
92 ? (...args: A) => Color
93 : (...args: A) => R
94 : never;
95
96 /** Convert a function to a Color namespace property (returning a Color) */
97 export type ToColorNamespace<T extends (...args: any[]) => any> = T extends (
98 ...args: infer A
99 ) => infer R
100 ? T extends { returns: "color" }
101 ? (...args: A) => Color
102 : (...args: A) => R
103 : never;
104
105 declare namespace Color {
106 // Functions defined using Color.defineFunctions
107 export const getAll: ToColorNamespace<typeof getAllFn>;
108 export const to: ToColorNamespace<typeof toFn>;
109 export const equals: ToColorNamespace<typeof equalsFn>;
110 export const inGamut: ToColorNamespace<typeof inGamutFn>;
111 export const distance: ToColorNamespace<typeof distanceFn>;
112 // `get` is defined below as a static method on the Class,
113 // and `toString` is intentionally not overridden for the namespace
114
115 export { util, hooks, WHITES, ColorSpace as Space, parse, defaults };
116 export const spaces: (typeof ColorSpace)["registry"];
117
118 // Must be manually defined due to overloads
119 // These should always match the signature of the original function
120 // prettier-ignore
121 export function set (
122 color: ColorTypes,
123 prop: Ref,
124 value: number | ((coord: number) => number),
125 ): Color;
126 // prettier-ignore
127 export function set (
128 color: ColorTypes,
129 props: Record<string, number | ((coord: number) => number)>,
130 ): Color;
131 // prettier-ignore
132 export function setAll (color: ColorTypes, coords: Coords, alpha?: number): Color;
133 // prettier-ignore
134 export function setAll (
135 color: ColorTypes,
136 space: string | ColorSpace,
137 coords: Coords,
138 alpha?: number,
139 ): Color;
140 // prettier-ignore
141 export function toGamut (color: ColorTypes, options?: ToGamutOptions): Color;
142 // prettier-ignore
143 export function toGamut (color: ColorTypes, space?: string): Color;
144 }
145
146 /**
147 * Class that represents a single color.
148 * All of Color.js’s tree-shakeable methods are also available as instance methods on this class,
149 * as well as static methods that take the color as the first argument.
150 */
151 declare class Color extends SpaceAccessors implements PlainColorObject {
152 constructor (color: ColorTypes);
153 constructor (space: string | ColorSpace, coords: Coords, alpha?: number | null);
154
155 // These signatures should always be the same as the constructor
156 static get (color: ColorTypes, options?: ParseOptions): Color;
157 static get (space: string | ColorSpace, coords: Coords, alpha: number | null): Color;
158
159 static try (color: ColorTypes, options?: TryColorOptions): Color | null;
160
161 static defineFunction (name: string, code: DefineFunctionHybrid): void;
162 static defineFunction (
163 name: string,
164 code: DefineFunctionCode,
165 options: DefineFunctionOptions,
166 ): void;
167
168 static defineFunctions (objects: Record<string, DefineFunctionHybrid>): void;
169
170 static extend (
171 exports: { register: (color: typeof Color) => void } | Record<string, DefineFunctionHybrid>,
172 ): void;
173
174 get space (): ColorSpace;
175 get spaceId (): string;
176
177 alpha: number;
178 coords: Coords;
179
180 clone (): this;
181
182 // Copy parameter types from display function, except for the first one
183 display (...args: RemoveFirstElement<Parameters<typeof display>>): string & { color: Color };
184
185 toJSON (): ColorConstructor;
186
187 // Functions defined using Color.defineFunctions
188 get: ToColorPrototype<typeof get>;
189 getAll: ToColorPrototype<typeof getAllFn>;
190 to: ToColorPrototype<typeof toFn>;
191 equals: ToColorPrototype<typeof equalsFn>;
192 inGamut: ToColorPrototype<typeof inGamutFn>;
193 distance: ToColorPrototype<typeof distanceFn>;
194 toString: ToColorPrototype<typeof serialize>;
195
196 // Must be manually defined due to overloads
197 // These should always match the signature of the original function
198 set (prop: Ref, value: number | ((coord: number) => number)): Color;
199 set (props: Record<string, number | ((coord: number) => number)>): Color;
200 setAll (coords: Coords, alpha?: number): Color;
201 setAll (space: string | ColorSpace, coords: Coords, alpha?: number): Color;
202 toGamut (options?: ToGamutOptions): Color;
203 toGamut (space?: string): Color;
204
205 /*
206 * ==========================================
207 * Types for properties defined in `index.js`
208 * ==========================================
209 */
210 // chromaticity
211 uv: ToColorPrototype<typeof uv>;
212 xy: ToColorPrototype<typeof xy>;
213
214 // contrast
215 contrast: ToColorPrototype<typeof contrast>;
216 static contrast: typeof contrast;
217
218 // contrastMethods
219 contrastWCAG21: ToColorPrototype<typeof contrastWCAG21>;
220 contrastAPCA: ToColorPrototype<typeof contrastAPCA>;
221 contrastMichelson: ToColorPrototype<typeof contrastMichelson>;
222 contrastWeber: ToColorPrototype<typeof contrastWeber>;
223 contrastLstar: ToColorPrototype<typeof contrastLstar>;
224 contrastDeltaPhi: ToColorPrototype<typeof contrastDeltaPhi>;
225
226 static contrastWCAG21: typeof contrastWCAG21;
227 static contrastAPCA: typeof contrastAPCA;
228 static contrastMichelson: typeof contrastMichelson;
229 static contrastWeber: typeof contrastWeber;
230 static contrastLstar: typeof contrastLstar;
231 static contrastDeltaPhi: typeof contrastDeltaPhi;
232
233 // deltaE
234 deltaE: ToColorPrototype<typeof deltaE>;
235 deltaE76: ToColorPrototype<typeof deltaE76>;
236 deltaECMC: ToColorPrototype<typeof deltaECMC>;
237 deltaE2000: ToColorPrototype<typeof deltaE2000>;
238 deltaEJz: ToColorPrototype<typeof deltaEJz>;
239 deltaEITP: ToColorPrototype<typeof deltaEITP>;
240 deltaEOK: ToColorPrototype<typeof deltaEOK>;
241
242 static deltaE: typeof deltaE;
243 static deltaE76: typeof deltaE76;
244 static deltaECMC: typeof deltaECMC;
245 static deltaE2000: typeof deltaE2000;
246 static deltaEJz: typeof deltaEJz;
247 static deltaEITP: typeof deltaEITP;
248 static deltaEOK: typeof deltaEOK;
249 static deltaEMethods: typeof deltaEMethods;
250
251 // interpolation
252 // These signatures should always match those in interpolation.d.ts,
253 // including the static versions
254 /** Create color mixtures in any desired proportion between two colors */
255 mix (color2: ColorTypes, options?: MixOptions): Color;
256 mix (color2: ColorTypes, p: number, options?: MixOptions): Color;
257 /**
258 * Creates a function that accepts a number and returns a color.
259 * For numbers in the range 0 to 1, the function interpolates;
260 * for numbers outside that range, the function extrapolates
261 * (and thus may not return the results you expect)
262 */
263 range: ToColorPrototype<typeof range>;
264 /** Get an array of discrete steps */
265 steps (color2: ColorTypes, options?: StepsOptions): Color[];
266
267 /** Create color mixtures in any desired proportion between two colors */
268 static mix (color1: ColorTypes, color2: ColorTypes, options?: MixOptions): Color;
269 static mix (color1: ColorTypes, color2: ColorTypes, p: number, options?: MixOptions): Color;
270 /**
271 * Creates a function that accepts a number and returns a color.
272 * For numbers in the range 0 to 1, the function interpolates;
273 * for numbers outside that range, the function extrapolates
274 * (and thus may not return the results you expect)
275 */
276 static range: typeof range;
277 /** Get an array of discrete steps */
278 static steps (color1: ColorTypes, color2: ColorTypes, options?: StepsOptions): Color[];
279 static steps (range: Range, options?: StepsOptions): Color[];
280
281 // luminance
282 get luminance (): ReturnType<typeof getLuminance>;
283 // the definition for this set in the orignial code like it doesn't actually use the parameter?
284 set luminance (_: number);
285
286 // variations
287 lighten: ToColorPrototype<typeof lighten>;
288 darken: ToColorPrototype<typeof darken>;
289 static lighten: ToColorNamespace<typeof lighten>;
290 static darken: ToColorNamespace<typeof darken>;
291 }
292
293 export default Color;