@d3plus/color 3.0.0-alpha.0

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/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # @d3plus/color
2
+
3
+ Color functions that extent the ability of d3-color.
4
+
5
+ ## Installing
6
+
7
+ If using npm, `npm install @d3plus/color`. Otherwise, you can download the [latest release from GitHub](https://github.com/d3plus/d3plus/releases/latest) or load from a [CDN](https://cdn.jsdelivr.net/npm/@d3plus/color@3.0.0-alpha.0/+esm).
8
+
9
+ ```js
10
+ import modules from "@d3plus/color";
11
+ ```
12
+
13
+ In vanilla JavaScript, a `d3plus` global is exported from the pre-bundled version:
14
+
15
+ ```html
16
+ <script src="https://cdn.jsdelivr.net/npm/@d3plus/color@3.0.0-alpha.0"></script>
17
+ <script>
18
+ console.log(d3plus);
19
+ </script>
20
+ ```
21
+
22
+ ## Examples
23
+
24
+ Live examples can be found on [d3plus.org](https://d3plus.org/), which includes a collection of example visualizations using @d3plus/react.
25
+
26
+ ## API Reference
27
+
28
+ #####
29
+ * [colorAdd](#colorAdd) - Adds two colors together.
30
+ * [colorAssign](#colorAssign) - Assigns a color to a value using a predefined set of defaults.
31
+ * [colorContrast](#colorContrast) - A set of default color values used when assigning colors based on data.
32
+ * [colorLegible](#colorLegible) - Darkens a color so that it will appear legible on a white background.
33
+ * [colorLighter](#colorLighter) - Similar to d3.color.brighter, except that this also reduces saturation so that colors don't appear neon.
34
+ * [colorSubtract](#colorSubtract) - Subtracts one color from another.
35
+
36
+ #####
37
+ * [colorDefaults](#colorDefaults) - A set of default color values used when assigning colors based on data.
38
+
39
+ | Name | Default | Description |
40
+ |---|---|---|
41
+ | dark | "#555555" | Used in the [contrast](#contrast) function when the color given is very light. |
42
+ | light | "#f7f7f7" | Used in the [contrast](#contrast) function when the color given is very dark. |
43
+ | missing | "#cccccc" | Used in the [assign](#assign) function when the value passed is `null` or `undefined`. |
44
+ | off | "#C44536" | Used in the [assign](#assign) function when the value passed is `false`. |
45
+ | on | "#6A994E" | Used in the [assign](#assign) function when the value passed is `true`. |
46
+ | scale | "#4281A4", "#F6AE2D", "#C44536", "#2A9D8F", "#6A994E", "#CEB54A", "#5E548E", "#C08497", "#99582A", "#8C8C99", "#1D3557", "#D08C60", "#6D2E46", "#8BB19C", "#52796F", "#5E60CE", "#985277", "#5C374C" | An ordinal scale used in the [assign](#assign) function for non-valid color strings and numbers. |
47
+
48
+ ---
49
+
50
+ <a name="colorAdd"></a>
51
+ #### d3plus.**colorAdd**(c1, c2, [o1], [o2]) [<>](https://github.com/d3plus/d3plus/blob/main/packages/color/src/add.js#L3)
52
+
53
+ Adds two colors together.
54
+
55
+
56
+ This is a global function
57
+
58
+ ---
59
+
60
+ <a name="colorAssign"></a>
61
+ #### d3plus.**colorAssign**(c, [u]) [<>](https://github.com/d3plus/d3plus/blob/main/packages/color/src/assign.js#L4)
62
+
63
+ Assigns a color to a value using a predefined set of defaults.
64
+
65
+
66
+ This is a global function
67
+
68
+ ---
69
+
70
+ <a name="colorContrast"></a>
71
+ #### d3plus.**colorContrast**(c, [u]) [<>](https://github.com/d3plus/d3plus/blob/main/packages/color/src/contrast.js#L4)
72
+
73
+ A set of default color values used when assigning colors based on data.
74
+
75
+
76
+ This is a global function
77
+
78
+ ---
79
+
80
+ <a name="colorLegible"></a>
81
+ #### d3plus.**colorLegible**(c) [<>](https://github.com/d3plus/d3plus/blob/main/packages/color/src/legible.js#L3)
82
+
83
+ Darkens a color so that it will appear legible on a white background.
84
+
85
+
86
+ This is a global function
87
+
88
+ ---
89
+
90
+ <a name="colorLighter"></a>
91
+ #### d3plus.**colorLighter**(c, [i]) [<>](https://github.com/d3plus/d3plus/blob/main/packages/color/src/lighter.js#L3)
92
+
93
+ Similar to d3.color.brighter, except that this also reduces saturation so that colors don't appear neon.
94
+
95
+
96
+ This is a global function
97
+
98
+ ---
99
+
100
+ <a name="colorSubtract"></a>
101
+ #### d3plus.**colorSubtract**(c1, c2, [o1], [o2]) [<>](https://github.com/d3plus/d3plus/blob/main/packages/color/src/subtract.js#L3)
102
+
103
+ Subtracts one color from another.
104
+
105
+
106
+ This is a global function
107
+
108
+ ---
109
+
110
+ <a name="colorDefaults"></a>
111
+ #### **colorDefaults** [<>](https://github.com/d3plus/d3plus/blob/main/packages/color/src/defaults.js#L3)
112
+
113
+ A set of default color values used when assigning colors based on data.
114
+
115
+ | Name | Default | Description |
116
+ |---|---|---|
117
+ | dark | "#555555" | Used in the [contrast](#contrast) function when the color given is very light. |
118
+ | light | "#f7f7f7" | Used in the [contrast](#contrast) function when the color given is very dark. |
119
+ | missing | "#cccccc" | Used in the [assign](#assign) function when the value passed is `null` or `undefined`. |
120
+ | off | "#C44536" | Used in the [assign](#assign) function when the value passed is `false`. |
121
+ | on | "#6A994E" | Used in the [assign](#assign) function when the value passed is `true`. |
122
+ | scale | "#4281A4", "#F6AE2D", "#C44536", "#2A9D8F", "#6A994E", "#CEB54A", "#5E548E", "#C08497", "#99582A", "#8C8C99", "#1D3557", "#D08C60", "#6D2E46", "#8BB19C", "#52796F", "#5E60CE", "#985277", "#5C374C" | An ordinal scale used in the [assign](#assign) function for non-valid color strings and numbers. |
123
+
124
+
125
+ This is a global namespace
126
+
127
+ ---
128
+
129
+
130
+ ###### <sub>Documentation generated on Thu, 13 Mar 2025 19:58:28 GMT</sub>
package/es/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export { default as colorAdd } from "./src/add.js";
2
+ export { default as colorAssign } from "./src/assign.js";
3
+ export { default as colorContrast } from "./src/contrast.js";
4
+ export { default as colorDefaults } from "./src/defaults.js";
5
+ export { default as colorLegible } from "./src/legible.js";
6
+ export { default as colorLighter } from "./src/lighter.js";
7
+ export { default as colorSubtract } from "./src/subtract.js";
package/es/src/add.js ADDED
@@ -0,0 +1,22 @@
1
+ import { hsl } from "d3-color";
2
+ /**
3
+ @function colorAdd
4
+ @desc Adds two colors together.
5
+ @param {String} c1 The first color, a valid CSS color string.
6
+ @param {String} c2 The second color, also a valid CSS color string.
7
+ @param {String} [o1 = 1] Value from 0 to 1 of the first color's opacity.
8
+ @param {String} [o2 = 1] Value from 0 to 1 of the first color's opacity.
9
+ @returns {String}
10
+ */ export default function(c1, c2) {
11
+ var o1 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1, o2 = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 1;
12
+ c1 = hsl(c1);
13
+ c2 = hsl(c2);
14
+ var d = Math.abs(c2.h * o2 - c1.h * o1);
15
+ if (d > 180) d -= 360;
16
+ var h = (Math.min(c1.h, c2.h) + d / 2) % 360;
17
+ var l = c1.l + (c2.l * o2 - c1.l * o1) / 2, s = c1.s + (c2.s * o2 - c1.s * o1) / 2;
18
+ // a = o1 + (o2 - o1) / 2;
19
+ if (h < 0) h += 360;
20
+ return hsl("hsl(".concat(h, ",").concat(s * 100, "%,").concat(l * 100, "%)")).toString();
21
+ // return hsl(`hsl(${h},${s * 100}%,${l * 100}%,${a})`).toString();
22
+ }
@@ -0,0 +1,22 @@
1
+ import { color } from "d3-color";
2
+ import { getColor } from "./defaults.js";
3
+ /**
4
+ @function colorAssign
5
+ @desc Assigns a color to a value using a predefined set of defaults.
6
+ @param {String} c A valid CSS color string.
7
+ @param {Object} [u = defaults] An object containing overrides of the default colors.
8
+ @returns {String}
9
+ */ export default function(c) {
10
+ var u = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
11
+ // If the value is null or undefined, set to grey.
12
+ if ([
13
+ null,
14
+ void 0
15
+ ].indexOf(c) >= 0) return getColor("missing", u);
16
+ else if (c === true) return getColor("on", u);
17
+ else if (c === false) return getColor("off", u);
18
+ var p = color(c);
19
+ // If the value is not a valid color string, use the color scale.
20
+ if (!p) return getColor("scale", u)(c);
21
+ return c.toString();
22
+ }
@@ -0,0 +1,14 @@
1
+ import { getColor } from "./defaults.js";
2
+ import { rgb } from "d3-color";
3
+ /**
4
+ @function colorContrast
5
+ @desc A set of default color values used when assigning colors based on data.
6
+ @param {String} c A valid CSS color string.
7
+ @param {Object} [u = defaults] An object containing overrides of the default colors.
8
+ @returns {String}
9
+ */ export default function(c) {
10
+ var u = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
11
+ c = rgb(c);
12
+ var yiq = (c.r * 299 + c.g * 587 + c.b * 114) / 1000;
13
+ return yiq >= 128 ? getColor("dark", u) : getColor("light", u);
14
+ }
@@ -0,0 +1,49 @@
1
+ import { scaleOrdinal } from "d3-scale";
2
+ /**
3
+ @namespace {Object} colorDefaults
4
+ @desc A set of default color values used when assigning colors based on data.
5
+ *
6
+ * | Name | Default | Description |
7
+ * |---|---|---|
8
+ * | dark | "#555555" | Used in the [contrast](#contrast) function when the color given is very light. |
9
+ * | light | "#f7f7f7" | Used in the [contrast](#contrast) function when the color given is very dark. |
10
+ * | missing | "#cccccc" | Used in the [assign](#assign) function when the value passed is `null` or `undefined`. |
11
+ * | off | "#C44536" | Used in the [assign](#assign) function when the value passed is `false`. |
12
+ * | on | "#6A994E" | Used in the [assign](#assign) function when the value passed is `true`. |
13
+ * | scale | "#4281A4", "#F6AE2D", "#C44536", "#2A9D8F", "#6A994E", "#CEB54A", "#5E548E", "#C08497", "#99582A", "#8C8C99", "#1D3557", "#D08C60", "#6D2E46", "#8BB19C", "#52796F", "#5E60CE", "#985277", "#5C374C" | An ordinal scale used in the [assign](#assign) function for non-valid color strings and numbers. |
14
+ */ var defaults = {
15
+ dark: "#555555",
16
+ light: "#f7f7f7",
17
+ missing: "#cccccc",
18
+ off: "#C44536",
19
+ on: "#6A994E",
20
+ scale: scaleOrdinal().range([
21
+ "#4281A4",
22
+ "#F6AE2D",
23
+ "#C44536",
24
+ "#2A9D8F",
25
+ "#6A994E",
26
+ "#CEB54A",
27
+ "#5E548E",
28
+ "#C08497",
29
+ "#99582A",
30
+ "#8C8C99",
31
+ "#1D3557",
32
+ "#D08C60",
33
+ "#6D2E46",
34
+ "#8BB19C",
35
+ "#52796F",
36
+ "#5E60CE",
37
+ "#985277",
38
+ "#5C374C"
39
+ ])
40
+ };
41
+ /**
42
+ Returns a color based on a key, whether it is present in a user supplied object or in the default object.
43
+ @returns {String}
44
+ @private
45
+ */ export function getColor(k) {
46
+ var u = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
47
+ return k in u ? u[k] : k in defaults ? defaults[k] : defaults.missing;
48
+ }
49
+ export default defaults;
@@ -0,0 +1,14 @@
1
+ import { hsl } from "d3-color";
2
+ /**
3
+ @function colorLegible
4
+ @desc Darkens a color so that it will appear legible on a white background.
5
+ @param {String} c A valid CSS color string.
6
+ @returns {String}
7
+ */ export default function(c) {
8
+ c = hsl(c);
9
+ if (c.l > 0.45) {
10
+ if (c.s > 0.8) c.s = 0.8;
11
+ c.l = 0.45;
12
+ }
13
+ return c.toString();
14
+ }
@@ -0,0 +1,15 @@
1
+ import { hsl } from "d3-color";
2
+ /**
3
+ @function colorLighter
4
+ @desc Similar to d3.color.brighter, except that this also reduces saturation so that colors don't appear neon.
5
+ @param {String} c A valid CSS color string.
6
+ @param {String} [i = 0.5] A value from 0 to 1 dictating the strength of the function.
7
+ @returns {String}
8
+ */ export default function(c) {
9
+ var i = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0.5;
10
+ c = hsl(c);
11
+ i *= 1 - c.l;
12
+ c.l += i;
13
+ c.s -= i;
14
+ return c.toString();
15
+ }
@@ -0,0 +1,22 @@
1
+ import { hsl } from "d3-color";
2
+ /**
3
+ @function colorSubtract
4
+ @desc Subtracts one color from another.
5
+ @param {String} c1 The base color, a valid CSS color string.
6
+ @param {String} c2 The color to remove from the base color, also a valid CSS color string.
7
+ @param {String} [o1 = 1] Value from 0 to 1 of the first color's opacity.
8
+ @param {String} [o2 = 1] Value from 0 to 1 of the first color's opacity.
9
+ @returns {String}
10
+ */ export default function(c1, c2) {
11
+ var o1 = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : 1, o2 = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : 1;
12
+ c1 = hsl(c1);
13
+ c2 = hsl(c2);
14
+ var d = c2.h * o2 - c1.h * o1;
15
+ if (Math.abs(d) > 180) d -= 360;
16
+ var h = (c1.h - d) % 360;
17
+ var l = c1.l - (c2.l * o2 - c1.l * o1) / 2, s = c1.s - (c2.s * o2 - c1.s * o1) / 2;
18
+ // a = o1 - (o2 - o1) / 2;
19
+ if (h < 0) h += 360;
20
+ return hsl("hsl(".concat(h, ",").concat(s * 100, "%,").concat(l * 100, "%)")).toString();
21
+ // return hsl(`hsl(${h},${s * 100}%,${l * 100}%,${a})`).toString();
22
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@d3plus/color",
3
+ "version": "3.0.0-alpha.0",
4
+ "description": "Color functions that extent the ability of d3-color.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "exports": "./es/index.js",
8
+ "browser": "./umd/d3plus-color.full.js",
9
+ "engines": {
10
+ "node": ">=18"
11
+ },
12
+ "sideEffects": false,
13
+ "files": [
14
+ "umd",
15
+ "es"
16
+ ],
17
+ "homepage": "https://d3plus.org",
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/d3plus/d3plus.git",
21
+ "directory": "packages/color"
22
+ },
23
+ "keywords": [
24
+ "export",
25
+ "d3",
26
+ "d3plus",
27
+ "data",
28
+ "visualization"
29
+ ],
30
+ "scripts": {
31
+ "build:esm": "node ../../scripts/build-esm.js",
32
+ "build:umd": "node ../../scripts/build-umd.js",
33
+ "dev": "node ../../scripts/dev.js",
34
+ "test": "eslint index.js src/**/*.js && eslint --global=it test && mocha 'test/**/*-test.js'"
35
+ },
36
+ "dependencies": {
37
+ "d3-color": "^3.1.0",
38
+ "d3-scale": "^4.0.2"
39
+ }
40
+ }