UNPKG

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