@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.
- package/LICENSE +21 -0
- package/README.md +170 -42
- package/es/src/add.js +10 -14
- package/es/src/assign.js +14 -14
- package/es/src/contrast.js +7 -9
- package/es/src/defaults.js +22 -11
- package/es/src/legible.js +7 -9
- package/es/src/lighter.js +8 -10
- package/es/src/subtract.js +10 -12
- package/package.json +19 -10
- package/types/index.d.ts +8 -0
- package/types/src/add.d.ts +8 -0
- package/types/src/assign.d.ts +7 -0
- package/types/src/contrast.d.ts +7 -0
- package/types/src/defaults.d.ts +33 -0
- package/types/src/legible.d.ts +5 -0
- package/types/src/lighter.d.ts +6 -0
- package/types/src/subtract.d.ts +8 -0
- package/umd/d3plus-color.full.js +76 -181
- package/umd/d3plus-color.full.js.map +1 -1
- package/umd/d3plus-color.full.min.js +56 -58
- package/umd/d3plus-color.js +76 -181
- package/umd/d3plus-color.js.map +1 -1
- package/umd/d3plus-color.min.js +54 -56
|
@@ -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;
|
package/umd/d3plus-color.full.js
CHANGED
|
@@ -1,113 +1,9 @@
|
|
|
1
1
|
/*
|
|
2
|
-
@d3plus/color v3.0
|
|
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, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
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
|
-
|
|
459
|
-
@
|
|
460
|
-
@param
|
|
461
|
-
@param
|
|
462
|
-
@param
|
|
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
|
-
|
|
467
|
-
|
|
468
|
-
let d = Math.abs(
|
|
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(
|
|
471
|
-
const l =
|
|
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
|
-
|
|
764
|
-
|
|
765
|
-
|
|
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
|
-
|
|
802
|
-
@
|
|
803
|
-
@param
|
|
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
|
-
|
|
811
|
-
].indexOf(c) >= 0) return
|
|
812
|
-
else if (c === true) return
|
|
813
|
-
else if (c === false) return
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
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
|
-
|
|
822
|
-
@
|
|
823
|
-
@param
|
|
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
|
-
|
|
828
|
-
const yiq = (
|
|
829
|
-
return yiq >= 128 ?
|
|
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
|
-
|
|
834
|
-
@
|
|
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
|
-
|
|
839
|
-
if (
|
|
840
|
-
if (
|
|
841
|
-
|
|
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
|
|
742
|
+
return hslColor.toString();
|
|
844
743
|
}
|
|
845
744
|
|
|
846
745
|
/**
|
|
847
|
-
|
|
848
|
-
@
|
|
849
|
-
@param
|
|
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
|
-
|
|
854
|
-
i *= 1 -
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
return
|
|
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
|
-
|
|
862
|
-
@
|
|
863
|
-
@param
|
|
864
|
-
@param
|
|
865
|
-
@param
|
|
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
|
-
|
|
870
|
-
|
|
871
|
-
let d =
|
|
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 = (
|
|
874
|
-
const l =
|
|
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();
|