UNPKG

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