@d3plus/color 3.0.16 → 3.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ /**
2
+ Darkens a color so that it will appear legible on a white background.
3
+ @param c A valid CSS color string.
4
+ */
5
+ export default function (c: string): string;
@@ -0,0 +1,6 @@
1
+ /**
2
+ Similar to d3.color.brighter, except that this also reduces saturation so that colors don't appear neon.
3
+ @param c A valid CSS color string.
4
+ @param i Strength of the lightening effect, from 0 to 1.
5
+ */
6
+ export default function (c: string, i?: number): string;
@@ -0,0 +1,8 @@
1
+ /**
2
+ Subtracts one color from another.
3
+ @param c1 The base color, a valid CSS color string.
4
+ @param c2 The color to remove from the base color, also a valid CSS color string.
5
+ @param o1 Value from 0 to 1 of the first color's opacity.
6
+ @param o2 Value from 0 to 1 of the first color's opacity.
7
+ */
8
+ export default function (c1: string, c2: string, o1?: number, o2?: number): string;
@@ -1,113 +1,9 @@
1
1
  /*
2
- @d3plus/color v3.0.16
2
+ @d3plus/color v3.1.0
3
3
  Color functions that extent the ability of d3-color.
4
4
  Copyright (c) 2026 D3plus - https://d3plus.org
5
5
  @license MIT
6
6
  */
7
-
8
- (function (factory) {
9
- typeof define === 'function' && define.amd ? define(factory) :
10
- factory();
11
- })((function () { 'use strict';
12
-
13
- if (typeof window !== "undefined") {
14
- (function () {
15
- try {
16
- if (typeof SVGElement === 'undefined' || Boolean(SVGElement.prototype.innerHTML)) {
17
- return;
18
- }
19
- } catch (e) {
20
- return;
21
- }
22
-
23
- function serializeNode (node) {
24
- switch (node.nodeType) {
25
- case 1:
26
- return serializeElementNode(node);
27
- case 3:
28
- return serializeTextNode(node);
29
- case 8:
30
- return serializeCommentNode(node);
31
- }
32
- }
33
-
34
- function serializeTextNode (node) {
35
- return node.textContent.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
36
- }
37
-
38
- function serializeCommentNode (node) {
39
- return '<!--' + node.nodeValue + '-->'
40
- }
41
-
42
- function serializeElementNode (node) {
43
- var output = '';
44
-
45
- output += '<' + node.tagName;
46
-
47
- if (node.hasAttributes()) {
48
- [].forEach.call(node.attributes, function(attrNode) {
49
- output += ' ' + attrNode.name + '="' + attrNode.value + '"';
50
- });
51
- }
52
-
53
- output += '>';
54
-
55
- if (node.hasChildNodes()) {
56
- [].forEach.call(node.childNodes, function(childNode) {
57
- output += serializeNode(childNode);
58
- });
59
- }
60
-
61
- output += '</' + node.tagName + '>';
62
-
63
- return output;
64
- }
65
-
66
- Object.defineProperty(SVGElement.prototype, 'innerHTML', {
67
- get: function () {
68
- var output = '';
69
-
70
- [].forEach.call(this.childNodes, function(childNode) {
71
- output += serializeNode(childNode);
72
- });
73
-
74
- return output;
75
- },
76
- set: function (markup) {
77
- while (this.firstChild) {
78
- this.removeChild(this.firstChild);
79
- }
80
-
81
- try {
82
- var dXML = new DOMParser();
83
- dXML.async = false;
84
-
85
- var sXML = '<svg xmlns=\'http://www.w3.org/2000/svg\' xmlns:xlink=\'http://www.w3.org/1999/xlink\'>' + markup + '</svg>';
86
- var svgDocElement = dXML.parseFromString(sXML, 'text/xml').documentElement;
87
-
88
- [].forEach.call(svgDocElement.childNodes, function(childNode) {
89
- this.appendChild(this.ownerDocument.importNode(childNode, true));
90
- }.bind(this));
91
- } catch (e) {
92
- throw new Error('Error parsing markup string');
93
- }
94
- }
95
- });
96
-
97
- Object.defineProperty(SVGElement.prototype, 'innerSVG', {
98
- get: function () {
99
- return this.innerHTML;
100
- },
101
- set: function (markup) {
102
- this.innerHTML = markup;
103
- }
104
- });
105
-
106
- })();
107
- }
108
-
109
- }));
110
-
111
7
  (function (global, factory) {
112
8
  typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
113
9
  typeof define === 'function' && define.amd ? define('@d3plus/color', ['exports'], factory) :
@@ -455,24 +351,20 @@
455
351
  }
456
352
 
457
353
  /**
458
- @function colorAdd
459
- @desc Adds two colors together.
460
- @param {String} c1 The first color, a valid CSS color string.
461
- @param {String} c2 The second color, also a valid CSS color string.
462
- @param {String} [o1 = 1] Value from 0 to 1 of the first color's opacity.
463
- @param {String} [o2 = 1] Value from 0 to 1 of the first color's opacity.
464
- @returns {String}
354
+ Adds two colors together.
355
+ @param c1 The first color, a valid CSS color string.
356
+ @param c2 The second color, also a valid CSS color string.
357
+ @param o1 Value from 0 to 1 of the first color's opacity.
358
+ @param o2 Value from 0 to 1 of the first color's opacity.
465
359
  */ function add(c1, c2, o1 = 1, o2 = 1) {
466
- c1 = hsl(c1);
467
- c2 = hsl(c2);
468
- let d = Math.abs(c2.h * o2 - c1.h * o1);
360
+ const hsl1 = hsl(c1);
361
+ const hsl2 = hsl(c2);
362
+ let d = Math.abs(hsl2.h * o2 - hsl1.h * o1);
469
363
  if (d > 180) d -= 360;
470
- let h = (Math.min(c1.h, c2.h) + d / 2) % 360;
471
- const l = c1.l + (c2.l * o2 - c1.l * o1) / 2, s = c1.s + (c2.s * o2 - c1.s * o1) / 2;
472
- // a = o1 + (o2 - o1) / 2;
364
+ let h = (Math.min(hsl1.h, hsl2.h) + d / 2) % 360;
365
+ const l = hsl1.l + (hsl2.l * o2 - hsl1.l * o1) / 2, s = hsl1.s + (hsl2.s * o2 - hsl1.s * o1) / 2;
473
366
  if (h < 0) h += 360;
474
367
  return hsl(`hsl(${h},${s * 100}%,${l * 100}%)`).toString();
475
- // return hsl(`hsl(${h},${s * 100}%,${l * 100}%,${a})`).toString();
476
368
  }
477
369
 
478
370
  class InternMap extends Map {
@@ -760,9 +652,27 @@
760
652
 
761
653
  const { theme: openColor } = pkg;
762
654
  /**
763
- @namespace {Object} colorDefaults
764
- @desc A set of default color values used when assigning colors based on data.
765
- */ const defaults = {
655
+ * A set of default color values used when assigning colors based on data.
656
+ *
657
+ * @defaultValue
658
+ * ```
659
+ * {
660
+ * dark: "#495057",
661
+ * light: "#f8f9fa",
662
+ * missing: "#ced4da",
663
+ * off: "#c92a2a",
664
+ * on: "#2b8a3e",
665
+ * scale: d3.scaleOrdinal().range([
666
+ * "#364fc7", "#fab005", "#c92a2a",
667
+ * "#2b8a3e", "#fd7e14", "#862e9c",
668
+ * "#15aabf", "#e64980", "#82c91e",
669
+ * "#74c0fc", "#faa2c1", "#c0eb75",
670
+ * "#b197fc", "#c5f6fa", "#ffe8cc",
671
+ * "#d3f9d8", "#f3d9fa", "#ffe3e3"
672
+ * ])
673
+ * }
674
+ * ```
675
+ */ const defaults = {
766
676
  dark: openColor.colors.gray[700],
767
677
  light: openColor.colors.gray[50],
768
678
  missing: openColor.colors.gray[400],
@@ -789,89 +699,74 @@
789
699
  openColor.colors.red[100]
790
700
  ])
791
701
  };
792
- /**
793
- Returns a color based on a key, whether it is present in a user supplied object or in the default object.
794
- @returns {String}
795
- @private
796
- */ function getColor(k, u = {}) {
797
- return k in u ? u[k] : k in defaults ? defaults[k] : defaults.missing;
798
- }
799
702
 
800
703
  /**
801
- @function colorAssign
802
- @desc Assigns a color to a value using a predefined set of defaults.
803
- @param {String} c A valid CSS color string.
804
- @param {Object} [u = defaults] An object containing overrides of the default colors.
805
- @returns {String}
704
+ Assigns a color to a value using a predefined set of defaults.
705
+ @param c A valid CSS color string.
706
+ @param u An object containing overrides of the default colors.
806
707
  */ function assign(c, u = {}) {
807
708
  // If the value is null or undefined, set to grey.
808
709
  if ([
809
710
  null,
810
- void 0
811
- ].indexOf(c) >= 0) return getColor("missing", u);
812
- else if (c === true) return getColor("on", u);
813
- else if (c === false) return getColor("off", u);
814
- const p = color(c);
815
- // If the value is not a valid color string, use the color scale.
816
- if (!p) return getColor("scale", u)(c);
817
- return c.toString();
711
+ undefined
712
+ ].indexOf(c) >= 0) return u["missing"] || defaults["missing"];
713
+ else if (c === true) return u["on"] || defaults["on"];
714
+ else if (c === false) return u["off"] || defaults["off"];
715
+ else {
716
+ const p = color(c);
717
+ // If the value is not a valid color string, use the color scale.
718
+ if (!p) return (u["scale"] || defaults["scale"])(c);
719
+ return c;
720
+ }
818
721
  }
819
722
 
820
723
  /**
821
- @function colorContrast
822
- @desc A set of default color values used when assigning colors based on data.
823
- @param {String} c A valid CSS color string.
824
- @param {Object} [u = defaults] An object containing overrides of the default colors.
825
- @returns {String}
724
+ A set of default color values used when assigning colors based on data.
725
+ @param c A valid CSS color string.
726
+ @param u An object containing overrides of the default colors.
826
727
  */ function contrast(c, u = {}) {
827
- c = rgb(c);
828
- const yiq = (c.r * 299 + c.g * 587 + c.b * 114) / 1000;
829
- return yiq >= 128 ? getColor("dark", u) : getColor("light", u);
728
+ const rgbColor = rgb(c);
729
+ const yiq = (rgbColor.r * 299 + rgbColor.g * 587 + rgbColor.b * 114) / 1000;
730
+ return yiq >= 128 ? u["dark"] || defaults["dark"] : u["light"] || defaults["light"];
830
731
  }
831
732
 
832
733
  /**
833
- @function colorLegible
834
- @desc Darkens a color so that it will appear legible on a white background.
835
- @param {String} c A valid CSS color string.
836
- @returns {String}
734
+ Darkens a color so that it will appear legible on a white background.
735
+ @param c A valid CSS color string.
837
736
  */ function legible(c) {
838
- c = hsl(c);
839
- if (c.l > 0.45) {
840
- if (c.s > 0.8) c.s = 0.8;
841
- c.l = 0.45;
737
+ const hslColor = hsl(c);
738
+ if (hslColor.l > 0.45) {
739
+ if (hslColor.s > 0.8) hslColor.s = 0.8;
740
+ hslColor.l = 0.45;
842
741
  }
843
- return c.toString();
742
+ return hslColor.toString();
844
743
  }
845
744
 
846
745
  /**
847
- @function colorLighter
848
- @desc Similar to d3.color.brighter, except that this also reduces saturation so that colors don't appear neon.
849
- @param {String} c A valid CSS color string.
850
- @param {String} [i = 0.5] A value from 0 to 1 dictating the strength of the function.
851
- @returns {String}
746
+ Similar to d3.color.brighter, except that this also reduces saturation so that colors don't appear neon.
747
+ @param c A valid CSS color string.
748
+ @param i Strength of the lightening effect, from 0 to 1.
852
749
  */ function lighter(c, i = 0.5) {
853
- c = hsl(c);
854
- i *= 1 - c.l;
855
- c.l += i;
856
- c.s -= i;
857
- return c.toString();
750
+ const hslColor = hsl(c);
751
+ i *= 1 - hslColor.l;
752
+ hslColor.l += i;
753
+ hslColor.s -= i;
754
+ return hslColor.toString();
858
755
  }
859
756
 
860
757
  /**
861
- @function colorSubtract
862
- @desc Subtracts one color from another.
863
- @param {String} c1 The base color, a valid CSS color string.
864
- @param {String} c2 The color to remove from the base color, also a valid CSS color string.
865
- @param {String} [o1 = 1] Value from 0 to 1 of the first color's opacity.
866
- @param {String} [o2 = 1] Value from 0 to 1 of the first color's opacity.
867
- @returns {String}
758
+ Subtracts one color from another.
759
+ @param c1 The base color, a valid CSS color string.
760
+ @param c2 The color to remove from the base color, also a valid CSS color string.
761
+ @param o1 Value from 0 to 1 of the first color's opacity.
762
+ @param o2 Value from 0 to 1 of the first color's opacity.
868
763
  */ function subtract(c1, c2, o1 = 1, o2 = 1) {
869
- c1 = hsl(c1);
870
- c2 = hsl(c2);
871
- let d = c2.h * o2 - c1.h * o1;
764
+ const hsl1 = hsl(c1);
765
+ const hsl2 = hsl(c2);
766
+ let d = hsl2.h * o2 - hsl1.h * o1;
872
767
  if (Math.abs(d) > 180) d -= 360;
873
- let h = (c1.h - d) % 360;
874
- const l = c1.l - (c2.l * o2 - c1.l * o1) / 2, s = c1.s - (c2.s * o2 - c1.s * o1) / 2;
768
+ let h = (hsl1.h - d) % 360;
769
+ const l = hsl1.l - (hsl2.l * o2 - hsl1.l * o1) / 2, s = hsl1.s - (hsl2.s * o2 - hsl1.s * o1) / 2;
875
770
  // a = o1 - (o2 - o1) / 2;
876
771
  if (h < 0) h += 360;
877
772
  return hsl(`hsl(${h},${s * 100}%,${l * 100}%)`).toString();