UNPKG

23.3 kBTypeScriptView Raw
1// Type definitions for D3JS d3-color module 2.0
2// Project: https://github.com/d3/d3-color/, https://d3js.org/d3-color
3// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
4// Alex Ford <https://github.com/gustavderdrache>
5// Boris Yankov <https://github.com/borisyankov>
6// denisname <https://github.com/denisname>
7// Hugues Stefanski <https://github.com/ledragon>
8// Nathan Bierema <https://github.com/Methuselah96>
9// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
10
11// Last module patch version validated against: 2.0.0
12
13// ---------------------------------------------------------------------------
14// Shared Type Definitions and Interfaces
15// ---------------------------------------------------------------------------
16
17/**
18 * Type allowing for color objects from a specified color space
19 */
20export type ColorSpaceObject = RGBColor | HSLColor | LabColor | HCLColor | CubehelixColor;
21
22/**
23 * A helper interface of methods common to color objects (including colors defined outside the d3-color standard module,
24 * e.g. in d3-hsv). This interface
25 */
26export interface ColorCommonInstance {
27 /**
28 * Returns true if and only if the color is displayable on standard hardware.
29 * For example, this returns false for an RGB color if any channel value is less than zero or greater than 255, or if the opacity is not in the range [0, 1].
30 */
31 displayable(): boolean;
32 /**
33 * Returns a string representing this color according to the CSS Object Model specification,
34 * such as rgb(247, 234, 186) or rgba(247, 234, 186, 0.2).
35 * If this color is not displayable, a suitable displayable color is returned instead.
36 * For example, RGB channel values greater than 255 are clamped to 255.
37 */
38 toString(): string;
39 /**
40 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
41 * If k is not specified, it defaults to 1. The behavior of this method is dependent on the implementing color space.
42 *
43 * @param k A color space dependent number to determine, how much brighter the returned color should be.
44 */
45 brighter(k?: number): this;
46 /**
47 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
48 * If k is not specified, it defaults to 1. The behavior of this method is dependent on the implementing color space.
49 *
50 * @param k A color space dependent number to determine, how much darker the returned color should be.
51 */
52 darker(k?: number): this;
53 /**
54 * Returns the RGB equivalent of this color. For RGB colors, that’s "this".
55 */
56 rgb(): RGBColor;
57 /**
58 * Returns a hexadecimal string representing this color.
59 * If this color is not displayable, a suitable displayable color is returned instead.
60 * For example, RGB channel values greater than 255 are clamped to 255.
61 */
62 hex(): string;
63}
64
65/**
66 * A Color object which serves as a base class for
67 * colorspace-specific sub-class implementations.
68 */
69export interface Color {
70 /**
71 * Returns true if and only if the color is displayable on standard hardware.
72 * For example, this returns false for an RGB color if any channel value is less than zero or greater than 255, or if the opacity is not in the range [0, 1].
73 */
74 displayable(): boolean; // Note: While this method is used in prototyping for colors of specific colorspaces, it should not be called directly, as 'this.rgb' would not be implemented on Color
75 /**
76 * Returns a string representing this color according to the CSS Object Model specification,
77 * such as rgb(247, 234, 186) or rgba(247, 234, 186, 0.2).
78 * If this color is not displayable, a suitable displayable color is returned instead.
79 * For example, RGB channel values greater than 255 are clamped to 255.
80 */
81 toString(): string; // Note: While this method is used in prototyping for colors of specific colorspaces, it should not be called directly, as 'this.rgb' would not be implemented on Color
82 /**
83 * Returns a hexadecimal string representing this color in RGB space, such as #f7eaba.
84 * If this color is not displayable, a suitable displayable color is returned instead.
85 * For example, RGB channel values greater than 255 are clamped to 255.
86 */
87 formatHex(): string;
88 /**
89 * Returns a string representing this color according to the CSS Color Module Level 3 specification, such as hsl(257, 50%, 80%) or hsla(257, 50%, 80%, 0.2).
90 * If this color is not displayable, a suitable displayable color is returned instead by clamping S and L channel values to the interval [0, 100].
91 */
92 formatHsl(): string;
93 /**
94 * Returns a string representing this color according to the CSS Object Model specification, such as rgb(247, 234, 186) or rgba(247, 234, 186, 0.2).
95 * If this color is not displayable, a suitable displayable color is returned instead by clamping RGB channel values to the interval [0, 255].
96 */
97 formatRgb(): string;
98 /**
99 * @deprecated Use color.formatHex.
100 */
101 hex(): string;
102}
103
104/**
105 * A Color factory object, which may also be used with instanceof to test if an object is a color instance.
106 */
107export interface ColorFactory extends Function {
108 /**
109 * Parses the specified CSS Color Module Level 3 specifier string, returning an RGB or HSL color.
110 * If the specifier was not valid, null is returned.
111 *
112 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
113 */
114 (cssColorSpecifier: string): RGBColor | HSLColor | null;
115 /**
116 * Converts the provided color instance and returns an RGB or HSL color.
117 *
118 * @param color A permissible color space instance.
119 */
120 (color: ColorSpaceObject | ColorCommonInstance): RGBColor | HSLColor;
121 /**
122 * Prototype of the factory, which can be used for instanceof testing
123 */
124 readonly prototype: Color;
125}
126
127/**
128 * An RGB color object.
129 */
130export interface RGBColor extends Color {
131 /**
132 * Value of red channel
133 */
134 r: number;
135 /**
136 * Value of green channel
137 */
138 g: number;
139 /**
140 * Value of blue channel
141 */
142 b: number;
143 /**
144 * Opacity value
145 */
146 opacity: number;
147 /**
148 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
149 * If k is not specified, it defaults to 1.
150 *
151 * @param k A color space dependent number to determine, how much brighter the returned color should be.
152 */
153 brighter(k?: number): this;
154 /**
155 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
156 * If k is not specified, it defaults to 1.
157 *
158 * @param k A color space dependent number to determine, how much darker the returned color should be.
159 */
160 darker(k?: number): this;
161 /**
162 * Returns the RGB equivalent of this color.
163 */
164 rgb(): this;
165 /**
166 * Returns a copy of this color.
167 *
168 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
169 */
170 copy(values?: { r?: number; g?: number; b?: number; opacity?: number }): this;
171}
172
173/**
174 * An RGB color factory object, which may also be used with instanceof to test if an object
175 * is an RGB color instance.
176 */
177export interface RGBColorFactory extends Function {
178 /**
179 * Constructs a new RGB color based on the specified channel values and opacity.
180 *
181 * @param r Red channel value.
182 * @param g Green channel value.
183 * @param b Blue channel value.
184 * @param opacity Optional opacity value, defaults to 1.
185 */
186 (r: number, g: number, b: number, opacity?: number): RGBColor;
187 /**
188 * Parses the specified CSS Color Module Level 3 specifier string, returning an RGB color.
189 * If the specifier was not valid, null is returned.
190 *
191 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
192 */
193 (cssColorSpecifier: string): RGBColor;
194 /**
195 * Converts the provided color instance and returns an RGB color. The color instance is converted to the RGB color space using color.rgb.
196 * Note that unlike color.rgb this method always returns a new instance, even if color is already an RGB color.
197 *
198 * @param color A permissible color space instance.
199 */
200 (color: ColorSpaceObject | ColorCommonInstance): RGBColor;
201 /**
202 * Prototype of the factory, which can be used for instanceof testing
203 */
204 readonly prototype: RGBColor;
205}
206
207/**
208 * An HSL color object.
209 */
210export interface HSLColor extends Color {
211 /**
212 * Hue channel value.
213 */
214 h: number;
215 /**
216 * Saturation channel value.
217 */
218 s: number;
219 /**
220 * Lightness channel value.
221 */
222 l: number;
223 /**
224 * Opacity value.
225 */
226 opacity: number;
227 /**
228 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
229 * If k is not specified, it defaults to 1.
230 *
231 * @param k A color space dependent number to determine, how much brighter the returned color should be.
232 */
233 brighter(k?: number): this;
234 /**
235 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
236 * If k is not specified, it defaults to 1.
237 *
238 * @param k A color space dependent number to determine, how much darker the returned color should be.
239 */
240 darker(k?: number): this;
241 /**
242 * Returns the RGB color equivalent of this color.
243 */
244 rgb(): RGBColor;
245 /**
246 * Returns a copy of this color.
247 *
248 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
249 */
250 copy(values?: { h?: number; s?: number; l?: number; opacity?: number }): this;
251}
252
253/**
254 * An HSL color factory object, which may also be used with instanceof to test if an object
255 * is an HSL color instance.
256 */
257export interface HSLColorFactory extends Function {
258 /**
259 * Constructs a new HSL color based on the specified channel values and opacity.
260 *
261 * @param h Hue channel value.
262 * @param s Saturation channel value.
263 * @param l Lightness channel value.
264 * @param opacity Optional opacity value, defaults to 1.
265 */
266 (h: number, s: number, l: number, opacity?: number): HSLColor;
267 /**
268 * Parses the specified CSS Color Module Level 3 specifier string, returning an HSL color.
269 * If the specifier was not valid, null is returned.
270 *
271 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
272 */
273 (cssColorSpecifier: string): HSLColor;
274 /**
275 * Converts the provided color instance and returns an HSL color.
276 * The color instance is converted to the RGB color space using color.rgb and then converted to HSL.
277 * (Colors already in the HSL color space skip the conversion to RGB.)
278 *
279 * @param color A permissible color space instance.
280 */
281 (color: ColorSpaceObject | ColorCommonInstance): HSLColor;
282 /**
283 * Prototype of the factory, which can be used for instanceof testing
284 */
285 readonly prototype: HSLColor;
286}
287
288/**
289 * A Lab (CIELAB) color object.
290 */
291export interface LabColor extends Color {
292 /**
293 * Lightness typically in the range [0, 100].
294 */
295 l: number;
296 /**
297 * Position between red/magenta and green typically in [-160, +160].
298 */
299 a: number;
300 /**
301 * Position between yellow and blue typically in [-160, +160].
302 */
303 b: number;
304 /**
305 * Opacity value
306 */
307 opacity: number;
308 /**
309 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
310 * If k is not specified, it defaults to 1.
311 *
312 * @param k A color space dependent number to determine, how much brighter the returned color should be.
313 */
314 brighter(k?: number): this;
315 /**
316 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
317 * If k is not specified, it defaults to 1.
318 *
319 * @param k A color space dependent number to determine, how much darker the returned color should be.
320 */
321 darker(k?: number): this;
322 /**
323 * Returns the RGB color equivalent of this color.
324 */
325 rgb(): RGBColor;
326 /**
327 * Returns a copy of this color.
328 *
329 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
330 */
331 copy(values?: { l?: number; a?: number; b?: number; opacity?: number }): this;
332}
333
334/**
335 * A Lab (CIELAB) color factory object, which may also be used with instanceof to test if an object
336 * is a Lab color instance.
337 */
338export interface LabColorFactory extends Function {
339 /**
340 * Constructs a new CIELAB color based on the specified channel values and opacity.
341 *
342 * @param l Lightness typically in the range [0, 100].
343 * @param a Position between red/magenta and green typically in [-160, +160].
344 * @param b Position between yellow and blue typically in [-160, +160].
345 * @param opacity Optional opacity value, defaults to 1.
346 */
347 (l: number, a: number, b: number, opacity?: number): LabColor;
348 /**
349 * Parses the specified CSS Color Module Level 3 specifier string, returning a Lab color.
350 * If the specifier was not valid, null is returned.
351 *
352 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
353 */
354 (cssColorSpecifier: string): LabColor;
355 /**
356 * Converts the provided color instance and returns a Lab color.
357 * The color instance is converted to the RGB color space using color.rgb and then converted to CIELAB.
358 * (Colors already in the Lab color space skip the conversion to RGB,
359 * and colors in the HCL color space are converted directly to CIELAB.)
360 *
361 * @param color A permissible color space instance.
362 */
363 (color: ColorSpaceObject | ColorCommonInstance): LabColor;
364 /**
365 * Prototype of the factory, which can be used for instanceof testing
366 */
367 readonly prototype: LabColor;
368}
369
370/**
371 * A gray color factory for Lab (CIELAB) colors.
372 */
373export type GrayColorFactory =
374 /**
375 * Constructs a new CIELAB color with the specified l value and a = b = 0.
376 *
377 * @param l Lightness typically in the range [0, 100].
378 * @param opacity Optional opacity value, defaults to 1.
379 */
380 (l: number, opacity?: number) => LabColor;
381
382/**
383 * An HCL (CIELCH) color object.
384 */
385export interface HCLColor extends Color {
386 /**
387 * Hue channel value typically in [0, 360).
388 */
389 h: number;
390 /**
391 * Chroma channel value typically in [0, 230].
392 */
393 c: number;
394 /**
395 * Luminance channel value typically in the range [0, 100].
396 */
397 l: number;
398 /**
399 * Opacity value
400 */
401 opacity: number;
402 /**
403 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
404 * If k is not specified, it defaults to 1.
405 *
406 * @param k A color space dependent number to determine, how much brighter the returned color should be.
407 */
408 brighter(k?: number): this;
409 /**
410 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
411 * If k is not specified, it defaults to 1.
412 *
413 * @param k A color space dependent number to determine, how much darker the returned color should be.
414 */
415 darker(k?: number): this;
416 /**
417 * Returns the RGB color equivalent of this color.
418 */
419 rgb(): RGBColor;
420 /**
421 * Returns a copy of this color.
422 *
423 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
424 */
425 copy(values?: { h?: number; c?: number; l?: number; opacity?: number }): this;
426}
427
428/**
429 * An HCL (CIELCH) color factory object, which may also be used with instanceof to test if an object
430 * is an HCL color instance.
431 */
432export interface HCLColorFactory extends Function {
433 /**
434 * Constructs a new HCL color based on the specified channel values and opacity.
435 *
436 * @param h Hue channel value typically in [0, 360).
437 * @param c Chroma channel value typically in [0, 230].
438 * @param l Luminance channel value typically in the range [0, 100].
439 * @param opacity Optional opacity value, defaults to 1.
440 */
441 (h: number, c: number, l: number, opacity?: number): HCLColor;
442 /**
443 * Parses the specified CSS Color Module Level 3 specifier string, returning an HCL color.
444 * If the specifier was not valid, null is returned.
445 *
446 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
447 */
448 (cssColorSpecifier: string): HCLColor;
449 /**
450 * Converts the provided color instance and returns an HCL color.
451 * The color instance is converted to the RGB color space using color.rgb and then converted to HCL.
452 * (Colors already in the HCL color space skip the conversion to RGB,
453 * and colors in the Lab color space are converted directly to HCL.)
454 *
455 * @param color A permissible color space instance.
456 */
457 (color: ColorSpaceObject | ColorCommonInstance): HCLColor;
458 /**
459 * Prototype of the factory, which can be used for instanceof testing
460 */
461 readonly prototype: HCLColor;
462}
463
464/**
465 * An LCH (CIELCH) color factory function to create an HCL color object.
466 */
467export interface LCHColorFactory {
468 /**
469 * Constructs a new HCL color based on the specified channel values and opacity.
470 *
471 * @param l Luminance channel value typically in the range [0, 100].
472 * @param c Chroma channel value typically in [0, 230].
473 * @param h Hue channel value typically in [0, 360).
474 * @param opacity Optional opacity value, defaults to 1.
475 */
476 (l: number, c: number, h: number, opacity?: number): HCLColor;
477 /**
478 * Parses the specified CSS Color Module Level 3 specifier string, returning an HCL color.
479 * If the specifier was not valid, null is returned.
480 *
481 * @param cssColorSpecifier A CSS color Module Level 3 specifier string.
482 */
483 (cssColorSpecifier: string): HCLColor;
484 /**
485 * Converts the provided color instance and returns an HCL color.
486 * The color instance is converted to the RGB color space using color.rgb and then converted to HCL.
487 * (Colors already in the HCL color space skip the conversion to RGB,
488 * and colors in the Lab color space are converted directly to HCL.)
489 *
490 * @param color A permissible color space instance.
491 */
492 (color: ColorSpaceObject | ColorCommonInstance): HCLColor;
493}
494
495/**
496 * Dave Green’s Cubehelix color object.
497 */
498export interface CubehelixColor extends Color {
499 /**
500 * Hue channel value.
501 */
502 h: number;
503 /**
504 * Saturation channel value.
505 */
506 s: number;
507 /**
508 * Lightness channel value.
509 */
510 l: number;
511 /**
512 * Opacity value.
513 */
514 opacity: number;
515 /**
516 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
517 * If k is not specified, it defaults to 1.
518 *
519 * @param k A color space dependent number to determine, how much brighter the returned color should be.
520 */
521 brighter(k?: number): this;
522 /**
523 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
524 * If k is not specified, it defaults to 1.
525 *
526 * @param k A color space dependent number to determine, how much darker the returned color should be.
527 */
528 darker(k?: number): this;
529 /**
530 * Returns the RGB color equivalent of this color.
531 */
532 rgb(): RGBColor;
533 /**
534 * Returns a copy of this color.
535 *
536 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
537 */
538 copy(values?: { h?: number; s?: number; l?: number; opacity?: number }): this;
539}
540
541/**
542 * A color factory object for Dave Green's Cubehelix colors, which may also be used with instanceof to test if an object
543 * is a Cubehelix color instance.
544 */
545export interface CubehelixColorFactory extends Function {
546 /**
547 * Constructs a new Cubehelix color based on the specified channel values and opacity.
548 *
549 * @param h Hue channel value.
550 * @param s Saturation channel value.
551 * @param l Lightness channel value.
552 * @param opacity Optional opacity value, defaults to 1.
553 */
554 (h: number, s: number, l: number, opacity?: number): CubehelixColor;
555 /**
556 * Parses the specified CSS Color Module Level 3 specifier string, returning an Cubehelix color.
557 * If the specifier was not valid, null is returned.
558 *
559 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
560 */
561 (cssColorSpecifier: string): CubehelixColor;
562 /**
563 * Converts the provided color instance and returns a Cubehelix color.
564 * The color instance is specified, it is converted to the RGB color space using color.rgb and then converted to Cubehelix.
565 * (Colors already in the Cubehelix color space skip the conversion to RGB.)
566 *
567 * @param color A permissible color space instance.
568 */
569 (color: ColorSpaceObject | ColorCommonInstance): CubehelixColor;
570 /**
571 * Prototype of the factory, which can be used for instanceof testing
572 */
573 readonly prototype: CubehelixColor;
574}
575
576// --------------------------------------------------------------------------
577// Color object factories
578// --------------------------------------------------------------------------
579
580/**
581 * A Color factory object, which may also be used with instanceof to test if an object is a color instance.
582 */
583export const color: ColorFactory;
584
585/**
586 * An RGB color factory object, which may also be used with instanceof to test if an object
587 * is an RGB color instance.
588 */
589export const rgb: RGBColorFactory;
590
591/**
592 * An HSL color factory object, which may also be used with instanceof to test if an object
593 * is an HSL color instance.
594 */
595export const hsl: HSLColorFactory;
596
597/**
598 * A Lab (CIELAB) color factory object, which may also be used with instanceof to test if an object
599 * is a Lab color instance.
600 */
601export const lab: LabColorFactory;
602
603/**
604 * A gray color factory for Lab (CIELAB) colors.
605 */
606export const gray: GrayColorFactory;
607
608/**
609 * An HCL (CIELCH) color factory object, which may also be used with instanceof to test if an object
610 * is an HCL color instance.
611 */
612export const hcl: HCLColorFactory;
613
614/**
615 * An LCH (CIELCH) color factory function to create an HCL color object.
616 */
617export const lch: LCHColorFactory;
618
619/**
620 * A color factory object for Dave Green's Cubehelix colors, which may also be used with instanceof to test if an object
621 * is a Cubehelix color instance.
622 */
623export const cubehelix: CubehelixColorFactory;