UNPKG

23.8 kBTypeScriptView Raw
1// Type definitions for D3JS d3-color module 3.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: 3.0.1
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 | undefined; g?: number | undefined; b?: number | undefined; opacity?: number | undefined }): 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 // tslint:disable-next-line:unified-signatures
201 (color: ColorSpaceObject | ColorCommonInstance): RGBColor;
202 /**
203 * Prototype of the factory, which can be used for instanceof testing
204 */
205 readonly prototype: RGBColor;
206}
207
208/**
209 * An HSL color object.
210 */
211export interface HSLColor extends Color {
212 /**
213 * Hue channel value.
214 */
215 h: number;
216 /**
217 * Saturation channel value.
218 */
219 s: number;
220 /**
221 * Lightness channel value.
222 */
223 l: number;
224 /**
225 * Opacity value.
226 */
227 opacity: number;
228 /**
229 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
230 * If k is not specified, it defaults to 1.
231 *
232 * @param k A color space dependent number to determine, how much brighter the returned color should be.
233 */
234 brighter(k?: number): this;
235 /**
236 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
237 * If k is not specified, it defaults to 1.
238 *
239 * @param k A color space dependent number to determine, how much darker the returned color should be.
240 */
241 darker(k?: number): this;
242 /**
243 * Returns the RGB color equivalent of this color.
244 */
245 rgb(): RGBColor;
246 /**
247 * Returns a copy of this color.
248 *
249 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
250 */
251 copy(values?: { h?: number | undefined; s?: number | undefined; l?: number | undefined; opacity?: number | undefined }): this;
252}
253
254/**
255 * An HSL color factory object, which may also be used with instanceof to test if an object
256 * is an HSL color instance.
257 */
258export interface HSLColorFactory extends Function {
259 /**
260 * Constructs a new HSL color based on the specified channel values and opacity.
261 *
262 * @param h Hue channel value.
263 * @param s Saturation channel value.
264 * @param l Lightness channel value.
265 * @param opacity Optional opacity value, defaults to 1.
266 */
267 (h: number, s: number, l: number, opacity?: number): HSLColor;
268 /**
269 * Parses the specified CSS Color Module Level 3 specifier string, returning an HSL color.
270 * If the specifier was not valid, null is returned.
271 *
272 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
273 */
274 (cssColorSpecifier: string): HSLColor;
275 /**
276 * Converts the provided color instance and returns an HSL color.
277 * The color instance is converted to the RGB color space using color.rgb and then converted to HSL.
278 * (Colors already in the HSL color space skip the conversion to RGB.)
279 *
280 * @param color A permissible color space instance.
281 */
282 // tslint:disable-next-line:unified-signatures
283 (color: ColorSpaceObject | ColorCommonInstance): HSLColor;
284 /**
285 * Prototype of the factory, which can be used for instanceof testing
286 */
287 readonly prototype: HSLColor;
288}
289
290/**
291 * A Lab (CIELAB) color object.
292 */
293export interface LabColor extends Color {
294 /**
295 * Lightness typically in the range [0, 100].
296 */
297 l: number;
298 /**
299 * Position between red/magenta and green typically in [-160, +160].
300 */
301 a: number;
302 /**
303 * Position between yellow and blue typically in [-160, +160].
304 */
305 b: number;
306 /**
307 * Opacity value
308 */
309 opacity: number;
310 /**
311 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
312 * If k is not specified, it defaults to 1.
313 *
314 * @param k A color space dependent number to determine, how much brighter the returned color should be.
315 */
316 brighter(k?: number): this;
317 /**
318 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
319 * If k is not specified, it defaults to 1.
320 *
321 * @param k A color space dependent number to determine, how much darker the returned color should be.
322 */
323 darker(k?: number): this;
324 /**
325 * Returns the RGB color equivalent of this color.
326 */
327 rgb(): RGBColor;
328 /**
329 * Returns a copy of this color.
330 *
331 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
332 */
333 copy(values?: { l?: number | undefined; a?: number | undefined; b?: number | undefined; opacity?: number | undefined }): this;
334}
335
336/**
337 * A Lab (CIELAB) color factory object, which may also be used with instanceof to test if an object
338 * is a Lab color instance.
339 */
340export interface LabColorFactory extends Function {
341 /**
342 * Constructs a new CIELAB color based on the specified channel values and opacity.
343 *
344 * @param l Lightness typically in the range [0, 100].
345 * @param a Position between red/magenta and green typically in [-160, +160].
346 * @param b Position between yellow and blue typically in [-160, +160].
347 * @param opacity Optional opacity value, defaults to 1.
348 */
349 (l: number, a: number, b: number, opacity?: number): LabColor;
350 /**
351 * Parses the specified CSS Color Module Level 3 specifier string, returning a Lab color.
352 * If the specifier was not valid, null is returned.
353 *
354 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
355 */
356 (cssColorSpecifier: string): LabColor;
357 /**
358 * Converts the provided color instance and returns a Lab color.
359 * The color instance is converted to the RGB color space using color.rgb and then converted to CIELAB.
360 * (Colors already in the Lab color space skip the conversion to RGB,
361 * and colors in the HCL color space are converted directly to CIELAB.)
362 *
363 * @param color A permissible color space instance.
364 */
365 // tslint:disable-next-line:unified-signatures
366 (color: ColorSpaceObject | ColorCommonInstance): LabColor;
367 /**
368 * Prototype of the factory, which can be used for instanceof testing
369 */
370 readonly prototype: LabColor;
371}
372
373/**
374 * A gray color factory for Lab (CIELAB) colors.
375 */
376export type GrayColorFactory =
377 /**
378 * Constructs a new CIELAB color with the specified l value and a = b = 0.
379 *
380 * @param l Lightness typically in the range [0, 100].
381 * @param opacity Optional opacity value, defaults to 1.
382 */
383 (l: number, opacity?: number) => LabColor;
384
385/**
386 * An HCL (CIELCH) color object.
387 */
388export interface HCLColor extends Color {
389 /**
390 * Hue channel value typically in [0, 360).
391 */
392 h: number;
393 /**
394 * Chroma channel value typically in [0, 230].
395 */
396 c: number;
397 /**
398 * Luminance channel value typically in the range [0, 100].
399 */
400 l: number;
401 /**
402 * Opacity value
403 */
404 opacity: number;
405 /**
406 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
407 * If k is not specified, it defaults to 1.
408 *
409 * @param k A color space dependent number to determine, how much brighter the returned color should be.
410 */
411 brighter(k?: number): this;
412 /**
413 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
414 * If k is not specified, it defaults to 1.
415 *
416 * @param k A color space dependent number to determine, how much darker the returned color should be.
417 */
418 darker(k?: number): this;
419 /**
420 * Returns the RGB color equivalent of this color.
421 */
422 rgb(): RGBColor;
423 /**
424 * Returns a copy of this color.
425 *
426 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
427 */
428 copy(values?: { h?: number | undefined; c?: number | undefined; l?: number | undefined; opacity?: number | undefined }): this;
429}
430
431/**
432 * An HCL (CIELCH) color factory object, which may also be used with instanceof to test if an object
433 * is an HCL color instance.
434 */
435export interface HCLColorFactory extends Function {
436 /**
437 * Constructs a new HCL color based on the specified channel values and opacity.
438 *
439 * @param h Hue channel value typically in [0, 360).
440 * @param c Chroma channel value typically in [0, 230].
441 * @param l Luminance channel value typically in the range [0, 100].
442 * @param opacity Optional opacity value, defaults to 1.
443 */
444 (h: number, c: number, l: number, opacity?: number): HCLColor;
445 /**
446 * Parses the specified CSS Color Module Level 3 specifier string, returning an HCL color.
447 * If the specifier was not valid, null is returned.
448 *
449 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
450 */
451 (cssColorSpecifier: string): HCLColor;
452 /**
453 * Converts the provided color instance and returns an HCL color.
454 * The color instance is converted to the RGB color space using color.rgb and then converted to HCL.
455 * (Colors already in the HCL color space skip the conversion to RGB,
456 * and colors in the Lab color space are converted directly to HCL.)
457 *
458 * @param color A permissible color space instance.
459 */
460 // tslint:disable-next-line:unified-signatures
461 (color: ColorSpaceObject | ColorCommonInstance): HCLColor;
462 /**
463 * Prototype of the factory, which can be used for instanceof testing
464 */
465 readonly prototype: HCLColor;
466}
467
468/**
469 * An LCH (CIELCH) color factory function to create an HCL color object.
470 */
471export interface LCHColorFactory {
472 /**
473 * Constructs a new HCL color based on the specified channel values and opacity.
474 *
475 * @param l Luminance channel value typically in the range [0, 100].
476 * @param c Chroma channel value typically in [0, 230].
477 * @param h Hue channel value typically in [0, 360).
478 * @param opacity Optional opacity value, defaults to 1.
479 */
480 (l: number, c: number, h: number, opacity?: number): HCLColor;
481 /**
482 * Parses the specified CSS Color Module Level 3 specifier string, returning an HCL color.
483 * If the specifier was not valid, null is returned.
484 *
485 * @param cssColorSpecifier A CSS color Module Level 3 specifier string.
486 */
487 (cssColorSpecifier: string): HCLColor;
488 /**
489 * Converts the provided color instance and returns an HCL color.
490 * The color instance is converted to the RGB color space using color.rgb and then converted to HCL.
491 * (Colors already in the HCL color space skip the conversion to RGB,
492 * and colors in the Lab color space are converted directly to HCL.)
493 *
494 * @param color A permissible color space instance.
495 */
496 // tslint:disable-next-line:unified-signatures
497 (color: ColorSpaceObject | ColorCommonInstance): HCLColor;
498}
499
500/**
501 * Dave Green’s Cubehelix color object.
502 */
503export interface CubehelixColor extends Color {
504 /**
505 * Hue channel value.
506 */
507 h: number;
508 /**
509 * Saturation channel value.
510 */
511 s: number;
512 /**
513 * Lightness channel value.
514 */
515 l: number;
516 /**
517 * Opacity value.
518 */
519 opacity: number;
520 /**
521 * Returns a brighter copy of this color. If k is specified, it controls how much brighter the returned color should be.
522 * If k is not specified, it defaults to 1.
523 *
524 * @param k A color space dependent number to determine, how much brighter the returned color should be.
525 */
526 brighter(k?: number): this;
527 /**
528 * Returns a darker copy of this color. If k is specified, it controls how much darker the returned color should be.
529 * If k is not specified, it defaults to 1.
530 *
531 * @param k A color space dependent number to determine, how much darker the returned color should be.
532 */
533 darker(k?: number): this;
534 /**
535 * Returns the RGB color equivalent of this color.
536 */
537 rgb(): RGBColor;
538 /**
539 * Returns a copy of this color.
540 *
541 * @param values If values is specified, any enumerable own properties of values are assigned to the new returned color.
542 */
543 copy(values?: { h?: number | undefined; s?: number | undefined; l?: number | undefined; opacity?: number | undefined }): this;
544}
545
546/**
547 * A color factory object for Dave Green's Cubehelix colors, which may also be used with instanceof to test if an object
548 * is a Cubehelix color instance.
549 */
550export interface CubehelixColorFactory extends Function {
551 /**
552 * Constructs a new Cubehelix color based on the specified channel values and opacity.
553 *
554 * @param h Hue channel value.
555 * @param s Saturation channel value.
556 * @param l Lightness channel value.
557 * @param opacity Optional opacity value, defaults to 1.
558 */
559 (h: number, s: number, l: number, opacity?: number): CubehelixColor;
560 /**
561 * Parses the specified CSS Color Module Level 3 specifier string, returning an Cubehelix color.
562 * If the specifier was not valid, null is returned.
563 *
564 * @param cssColorSpecifier A CSS Color Module Level 3 specifier string.
565 */
566 (cssColorSpecifier: string): CubehelixColor;
567 /**
568 * Converts the provided color instance and returns a Cubehelix color.
569 * The color instance is specified, it is converted to the RGB color space using color.rgb and then converted to Cubehelix.
570 * (Colors already in the Cubehelix color space skip the conversion to RGB.)
571 *
572 * @param color A permissible color space instance.
573 */
574 // tslint:disable-next-line:unified-signatures
575 (color: ColorSpaceObject | ColorCommonInstance): CubehelixColor;
576 /**
577 * Prototype of the factory, which can be used for instanceof testing
578 */
579 readonly prototype: CubehelixColor;
580}
581
582// --------------------------------------------------------------------------
583// Color object factories
584// --------------------------------------------------------------------------
585
586/**
587 * A Color factory object, which may also be used with instanceof to test if an object is a color instance.
588 */
589export const color: ColorFactory;
590
591/**
592 * An RGB color factory object, which may also be used with instanceof to test if an object
593 * is an RGB color instance.
594 */
595export const rgb: RGBColorFactory;
596
597/**
598 * An HSL color factory object, which may also be used with instanceof to test if an object
599 * is an HSL color instance.
600 */
601export const hsl: HSLColorFactory;
602
603/**
604 * A Lab (CIELAB) color factory object, which may also be used with instanceof to test if an object
605 * is a Lab color instance.
606 */
607export const lab: LabColorFactory;
608
609/**
610 * A gray color factory for Lab (CIELAB) colors.
611 */
612export const gray: GrayColorFactory;
613
614/**
615 * An HCL (CIELCH) color factory object, which may also be used with instanceof to test if an object
616 * is an HCL color instance.
617 */
618export const hcl: HCLColorFactory;
619
620/**
621 * An LCH (CIELCH) color factory function to create an HCL color object.
622 */
623export const lch: LCHColorFactory;
624
625/**
626 * A color factory object for Dave Green's Cubehelix colors, which may also be used with instanceof to test if an object
627 * is a Cubehelix color instance.
628 */
629export const cubehelix: CubehelixColorFactory;