| 1 | // https://d3js.org/d3-color/ v3.1.0 Copyright 2010-2022 Mike Bostock
|
|---|
| 2 | (function (global, factory) {
|
|---|
| 3 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
|---|
| 4 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
|---|
| 5 | (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
|
|---|
| 6 | })(this, (function (exports) { 'use strict';
|
|---|
| 7 |
|
|---|
| 8 | function define(constructor, factory, prototype) {
|
|---|
| 9 | constructor.prototype = factory.prototype = prototype;
|
|---|
| 10 | prototype.constructor = constructor;
|
|---|
| 11 | }
|
|---|
| 12 |
|
|---|
| 13 | function extend(parent, definition) {
|
|---|
| 14 | var prototype = Object.create(parent.prototype);
|
|---|
| 15 | for (var key in definition) prototype[key] = definition[key];
|
|---|
| 16 | return prototype;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | function Color() {}
|
|---|
| 20 |
|
|---|
| 21 | var darker = 0.7;
|
|---|
| 22 | var brighter = 1 / darker;
|
|---|
| 23 |
|
|---|
| 24 | var reI = "\\s*([+-]?\\d+)\\s*",
|
|---|
| 25 | reN = "\\s*([+-]?(?:\\d*\\.)?\\d+(?:[eE][+-]?\\d+)?)\\s*",
|
|---|
| 26 | reP = "\\s*([+-]?(?:\\d*\\.)?\\d+(?:[eE][+-]?\\d+)?)%\\s*",
|
|---|
| 27 | reHex = /^#([0-9a-f]{3,8})$/,
|
|---|
| 28 | reRgbInteger = new RegExp(`^rgb\\(${reI},${reI},${reI}\\)$`),
|
|---|
| 29 | reRgbPercent = new RegExp(`^rgb\\(${reP},${reP},${reP}\\)$`),
|
|---|
| 30 | reRgbaInteger = new RegExp(`^rgba\\(${reI},${reI},${reI},${reN}\\)$`),
|
|---|
| 31 | reRgbaPercent = new RegExp(`^rgba\\(${reP},${reP},${reP},${reN}\\)$`),
|
|---|
| 32 | reHslPercent = new RegExp(`^hsl\\(${reN},${reP},${reP}\\)$`),
|
|---|
| 33 | reHslaPercent = new RegExp(`^hsla\\(${reN},${reP},${reP},${reN}\\)$`);
|
|---|
| 34 |
|
|---|
| 35 | var named = {
|
|---|
| 36 | aliceblue: 0xf0f8ff,
|
|---|
| 37 | antiquewhite: 0xfaebd7,
|
|---|
| 38 | aqua: 0x00ffff,
|
|---|
| 39 | aquamarine: 0x7fffd4,
|
|---|
| 40 | azure: 0xf0ffff,
|
|---|
| 41 | beige: 0xf5f5dc,
|
|---|
| 42 | bisque: 0xffe4c4,
|
|---|
| 43 | black: 0x000000,
|
|---|
| 44 | blanchedalmond: 0xffebcd,
|
|---|
| 45 | blue: 0x0000ff,
|
|---|
| 46 | blueviolet: 0x8a2be2,
|
|---|
| 47 | brown: 0xa52a2a,
|
|---|
| 48 | burlywood: 0xdeb887,
|
|---|
| 49 | cadetblue: 0x5f9ea0,
|
|---|
| 50 | chartreuse: 0x7fff00,
|
|---|
| 51 | chocolate: 0xd2691e,
|
|---|
| 52 | coral: 0xff7f50,
|
|---|
| 53 | cornflowerblue: 0x6495ed,
|
|---|
| 54 | cornsilk: 0xfff8dc,
|
|---|
| 55 | crimson: 0xdc143c,
|
|---|
| 56 | cyan: 0x00ffff,
|
|---|
| 57 | darkblue: 0x00008b,
|
|---|
| 58 | darkcyan: 0x008b8b,
|
|---|
| 59 | darkgoldenrod: 0xb8860b,
|
|---|
| 60 | darkgray: 0xa9a9a9,
|
|---|
| 61 | darkgreen: 0x006400,
|
|---|
| 62 | darkgrey: 0xa9a9a9,
|
|---|
| 63 | darkkhaki: 0xbdb76b,
|
|---|
| 64 | darkmagenta: 0x8b008b,
|
|---|
| 65 | darkolivegreen: 0x556b2f,
|
|---|
| 66 | darkorange: 0xff8c00,
|
|---|
| 67 | darkorchid: 0x9932cc,
|
|---|
| 68 | darkred: 0x8b0000,
|
|---|
| 69 | darksalmon: 0xe9967a,
|
|---|
| 70 | darkseagreen: 0x8fbc8f,
|
|---|
| 71 | darkslateblue: 0x483d8b,
|
|---|
| 72 | darkslategray: 0x2f4f4f,
|
|---|
| 73 | darkslategrey: 0x2f4f4f,
|
|---|
| 74 | darkturquoise: 0x00ced1,
|
|---|
| 75 | darkviolet: 0x9400d3,
|
|---|
| 76 | deeppink: 0xff1493,
|
|---|
| 77 | deepskyblue: 0x00bfff,
|
|---|
| 78 | dimgray: 0x696969,
|
|---|
| 79 | dimgrey: 0x696969,
|
|---|
| 80 | dodgerblue: 0x1e90ff,
|
|---|
| 81 | firebrick: 0xb22222,
|
|---|
| 82 | floralwhite: 0xfffaf0,
|
|---|
| 83 | forestgreen: 0x228b22,
|
|---|
| 84 | fuchsia: 0xff00ff,
|
|---|
| 85 | gainsboro: 0xdcdcdc,
|
|---|
| 86 | ghostwhite: 0xf8f8ff,
|
|---|
| 87 | gold: 0xffd700,
|
|---|
| 88 | goldenrod: 0xdaa520,
|
|---|
| 89 | gray: 0x808080,
|
|---|
| 90 | green: 0x008000,
|
|---|
| 91 | greenyellow: 0xadff2f,
|
|---|
| 92 | grey: 0x808080,
|
|---|
| 93 | honeydew: 0xf0fff0,
|
|---|
| 94 | hotpink: 0xff69b4,
|
|---|
| 95 | indianred: 0xcd5c5c,
|
|---|
| 96 | indigo: 0x4b0082,
|
|---|
| 97 | ivory: 0xfffff0,
|
|---|
| 98 | khaki: 0xf0e68c,
|
|---|
| 99 | lavender: 0xe6e6fa,
|
|---|
| 100 | lavenderblush: 0xfff0f5,
|
|---|
| 101 | lawngreen: 0x7cfc00,
|
|---|
| 102 | lemonchiffon: 0xfffacd,
|
|---|
| 103 | lightblue: 0xadd8e6,
|
|---|
| 104 | lightcoral: 0xf08080,
|
|---|
| 105 | lightcyan: 0xe0ffff,
|
|---|
| 106 | lightgoldenrodyellow: 0xfafad2,
|
|---|
| 107 | lightgray: 0xd3d3d3,
|
|---|
| 108 | lightgreen: 0x90ee90,
|
|---|
| 109 | lightgrey: 0xd3d3d3,
|
|---|
| 110 | lightpink: 0xffb6c1,
|
|---|
| 111 | lightsalmon: 0xffa07a,
|
|---|
| 112 | lightseagreen: 0x20b2aa,
|
|---|
| 113 | lightskyblue: 0x87cefa,
|
|---|
| 114 | lightslategray: 0x778899,
|
|---|
| 115 | lightslategrey: 0x778899,
|
|---|
| 116 | lightsteelblue: 0xb0c4de,
|
|---|
| 117 | lightyellow: 0xffffe0,
|
|---|
| 118 | lime: 0x00ff00,
|
|---|
| 119 | limegreen: 0x32cd32,
|
|---|
| 120 | linen: 0xfaf0e6,
|
|---|
| 121 | magenta: 0xff00ff,
|
|---|
| 122 | maroon: 0x800000,
|
|---|
| 123 | mediumaquamarine: 0x66cdaa,
|
|---|
| 124 | mediumblue: 0x0000cd,
|
|---|
| 125 | mediumorchid: 0xba55d3,
|
|---|
| 126 | mediumpurple: 0x9370db,
|
|---|
| 127 | mediumseagreen: 0x3cb371,
|
|---|
| 128 | mediumslateblue: 0x7b68ee,
|
|---|
| 129 | mediumspringgreen: 0x00fa9a,
|
|---|
| 130 | mediumturquoise: 0x48d1cc,
|
|---|
| 131 | mediumvioletred: 0xc71585,
|
|---|
| 132 | midnightblue: 0x191970,
|
|---|
| 133 | mintcream: 0xf5fffa,
|
|---|
| 134 | mistyrose: 0xffe4e1,
|
|---|
| 135 | moccasin: 0xffe4b5,
|
|---|
| 136 | navajowhite: 0xffdead,
|
|---|
| 137 | navy: 0x000080,
|
|---|
| 138 | oldlace: 0xfdf5e6,
|
|---|
| 139 | olive: 0x808000,
|
|---|
| 140 | olivedrab: 0x6b8e23,
|
|---|
| 141 | orange: 0xffa500,
|
|---|
| 142 | orangered: 0xff4500,
|
|---|
| 143 | orchid: 0xda70d6,
|
|---|
| 144 | palegoldenrod: 0xeee8aa,
|
|---|
| 145 | palegreen: 0x98fb98,
|
|---|
| 146 | paleturquoise: 0xafeeee,
|
|---|
| 147 | palevioletred: 0xdb7093,
|
|---|
| 148 | papayawhip: 0xffefd5,
|
|---|
| 149 | peachpuff: 0xffdab9,
|
|---|
| 150 | peru: 0xcd853f,
|
|---|
| 151 | pink: 0xffc0cb,
|
|---|
| 152 | plum: 0xdda0dd,
|
|---|
| 153 | powderblue: 0xb0e0e6,
|
|---|
| 154 | purple: 0x800080,
|
|---|
| 155 | rebeccapurple: 0x663399,
|
|---|
| 156 | red: 0xff0000,
|
|---|
| 157 | rosybrown: 0xbc8f8f,
|
|---|
| 158 | royalblue: 0x4169e1,
|
|---|
| 159 | saddlebrown: 0x8b4513,
|
|---|
| 160 | salmon: 0xfa8072,
|
|---|
| 161 | sandybrown: 0xf4a460,
|
|---|
| 162 | seagreen: 0x2e8b57,
|
|---|
| 163 | seashell: 0xfff5ee,
|
|---|
| 164 | sienna: 0xa0522d,
|
|---|
| 165 | silver: 0xc0c0c0,
|
|---|
| 166 | skyblue: 0x87ceeb,
|
|---|
| 167 | slateblue: 0x6a5acd,
|
|---|
| 168 | slategray: 0x708090,
|
|---|
| 169 | slategrey: 0x708090,
|
|---|
| 170 | snow: 0xfffafa,
|
|---|
| 171 | springgreen: 0x00ff7f,
|
|---|
| 172 | steelblue: 0x4682b4,
|
|---|
| 173 | tan: 0xd2b48c,
|
|---|
| 174 | teal: 0x008080,
|
|---|
| 175 | thistle: 0xd8bfd8,
|
|---|
| 176 | tomato: 0xff6347,
|
|---|
| 177 | turquoise: 0x40e0d0,
|
|---|
| 178 | violet: 0xee82ee,
|
|---|
| 179 | wheat: 0xf5deb3,
|
|---|
| 180 | white: 0xffffff,
|
|---|
| 181 | whitesmoke: 0xf5f5f5,
|
|---|
| 182 | yellow: 0xffff00,
|
|---|
| 183 | yellowgreen: 0x9acd32
|
|---|
| 184 | };
|
|---|
| 185 |
|
|---|
| 186 | define(Color, color, {
|
|---|
| 187 | copy(channels) {
|
|---|
| 188 | return Object.assign(new this.constructor, this, channels);
|
|---|
| 189 | },
|
|---|
| 190 | displayable() {
|
|---|
| 191 | return this.rgb().displayable();
|
|---|
| 192 | },
|
|---|
| 193 | hex: color_formatHex, // Deprecated! Use color.formatHex.
|
|---|
| 194 | formatHex: color_formatHex,
|
|---|
| 195 | formatHex8: color_formatHex8,
|
|---|
| 196 | formatHsl: color_formatHsl,
|
|---|
| 197 | formatRgb: color_formatRgb,
|
|---|
| 198 | toString: color_formatRgb
|
|---|
| 199 | });
|
|---|
| 200 |
|
|---|
| 201 | function color_formatHex() {
|
|---|
| 202 | return this.rgb().formatHex();
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 | function color_formatHex8() {
|
|---|
| 206 | return this.rgb().formatHex8();
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | function color_formatHsl() {
|
|---|
| 210 | return hslConvert(this).formatHsl();
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | function color_formatRgb() {
|
|---|
| 214 | return this.rgb().formatRgb();
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | function color(format) {
|
|---|
| 218 | var m, l;
|
|---|
| 219 | format = (format + "").trim().toLowerCase();
|
|---|
| 220 | return (m = reHex.exec(format)) ? (l = m[1].length, m = parseInt(m[1], 16), l === 6 ? rgbn(m) // #ff0000
|
|---|
| 221 | : l === 3 ? new Rgb((m >> 8 & 0xf) | (m >> 4 & 0xf0), (m >> 4 & 0xf) | (m & 0xf0), ((m & 0xf) << 4) | (m & 0xf), 1) // #f00
|
|---|
| 222 | : l === 8 ? rgba(m >> 24 & 0xff, m >> 16 & 0xff, m >> 8 & 0xff, (m & 0xff) / 0xff) // #ff000000
|
|---|
| 223 | : l === 4 ? rgba((m >> 12 & 0xf) | (m >> 8 & 0xf0), (m >> 8 & 0xf) | (m >> 4 & 0xf0), (m >> 4 & 0xf) | (m & 0xf0), (((m & 0xf) << 4) | (m & 0xf)) / 0xff) // #f000
|
|---|
| 224 | : null) // invalid hex
|
|---|
| 225 | : (m = reRgbInteger.exec(format)) ? new Rgb(m[1], m[2], m[3], 1) // rgb(255, 0, 0)
|
|---|
| 226 | : (m = reRgbPercent.exec(format)) ? new Rgb(m[1] * 255 / 100, m[2] * 255 / 100, m[3] * 255 / 100, 1) // rgb(100%, 0%, 0%)
|
|---|
| 227 | : (m = reRgbaInteger.exec(format)) ? rgba(m[1], m[2], m[3], m[4]) // rgba(255, 0, 0, 1)
|
|---|
| 228 | : (m = reRgbaPercent.exec(format)) ? rgba(m[1] * 255 / 100, m[2] * 255 / 100, m[3] * 255 / 100, m[4]) // rgb(100%, 0%, 0%, 1)
|
|---|
| 229 | : (m = reHslPercent.exec(format)) ? hsla(m[1], m[2] / 100, m[3] / 100, 1) // hsl(120, 50%, 50%)
|
|---|
| 230 | : (m = reHslaPercent.exec(format)) ? hsla(m[1], m[2] / 100, m[3] / 100, m[4]) // hsla(120, 50%, 50%, 1)
|
|---|
| 231 | : named.hasOwnProperty(format) ? rgbn(named[format]) // eslint-disable-line no-prototype-builtins
|
|---|
| 232 | : format === "transparent" ? new Rgb(NaN, NaN, NaN, 0)
|
|---|
| 233 | : null;
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | function rgbn(n) {
|
|---|
| 237 | return new Rgb(n >> 16 & 0xff, n >> 8 & 0xff, n & 0xff, 1);
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | function rgba(r, g, b, a) {
|
|---|
| 241 | if (a <= 0) r = g = b = NaN;
|
|---|
| 242 | return new Rgb(r, g, b, a);
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | function rgbConvert(o) {
|
|---|
| 246 | if (!(o instanceof Color)) o = color(o);
|
|---|
| 247 | if (!o) return new Rgb;
|
|---|
| 248 | o = o.rgb();
|
|---|
| 249 | return new Rgb(o.r, o.g, o.b, o.opacity);
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | function rgb(r, g, b, opacity) {
|
|---|
| 253 | return arguments.length === 1 ? rgbConvert(r) : new Rgb(r, g, b, opacity == null ? 1 : opacity);
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | function Rgb(r, g, b, opacity) {
|
|---|
| 257 | this.r = +r;
|
|---|
| 258 | this.g = +g;
|
|---|
| 259 | this.b = +b;
|
|---|
| 260 | this.opacity = +opacity;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | define(Rgb, rgb, extend(Color, {
|
|---|
| 264 | brighter(k) {
|
|---|
| 265 | k = k == null ? brighter : Math.pow(brighter, k);
|
|---|
| 266 | return new Rgb(this.r * k, this.g * k, this.b * k, this.opacity);
|
|---|
| 267 | },
|
|---|
| 268 | darker(k) {
|
|---|
| 269 | k = k == null ? darker : Math.pow(darker, k);
|
|---|
| 270 | return new Rgb(this.r * k, this.g * k, this.b * k, this.opacity);
|
|---|
| 271 | },
|
|---|
| 272 | rgb() {
|
|---|
| 273 | return this;
|
|---|
| 274 | },
|
|---|
| 275 | clamp() {
|
|---|
| 276 | return new Rgb(clampi(this.r), clampi(this.g), clampi(this.b), clampa(this.opacity));
|
|---|
| 277 | },
|
|---|
| 278 | displayable() {
|
|---|
| 279 | return (-0.5 <= this.r && this.r < 255.5)
|
|---|
| 280 | && (-0.5 <= this.g && this.g < 255.5)
|
|---|
| 281 | && (-0.5 <= this.b && this.b < 255.5)
|
|---|
| 282 | && (0 <= this.opacity && this.opacity <= 1);
|
|---|
| 283 | },
|
|---|
| 284 | hex: rgb_formatHex, // Deprecated! Use color.formatHex.
|
|---|
| 285 | formatHex: rgb_formatHex,
|
|---|
| 286 | formatHex8: rgb_formatHex8,
|
|---|
| 287 | formatRgb: rgb_formatRgb,
|
|---|
| 288 | toString: rgb_formatRgb
|
|---|
| 289 | }));
|
|---|
| 290 |
|
|---|
| 291 | function rgb_formatHex() {
|
|---|
| 292 | return `#${hex(this.r)}${hex(this.g)}${hex(this.b)}`;
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | function rgb_formatHex8() {
|
|---|
| 296 | return `#${hex(this.r)}${hex(this.g)}${hex(this.b)}${hex((isNaN(this.opacity) ? 1 : this.opacity) * 255)}`;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | function rgb_formatRgb() {
|
|---|
| 300 | const a = clampa(this.opacity);
|
|---|
| 301 | return `${a === 1 ? "rgb(" : "rgba("}${clampi(this.r)}, ${clampi(this.g)}, ${clampi(this.b)}${a === 1 ? ")" : `, ${a})`}`;
|
|---|
| 302 | }
|
|---|
| 303 |
|
|---|
| 304 | function clampa(opacity) {
|
|---|
| 305 | return isNaN(opacity) ? 1 : Math.max(0, Math.min(1, opacity));
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | function clampi(value) {
|
|---|
| 309 | return Math.max(0, Math.min(255, Math.round(value) || 0));
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | function hex(value) {
|
|---|
| 313 | value = clampi(value);
|
|---|
| 314 | return (value < 16 ? "0" : "") + value.toString(16);
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | function hsla(h, s, l, a) {
|
|---|
| 318 | if (a <= 0) h = s = l = NaN;
|
|---|
| 319 | else if (l <= 0 || l >= 1) h = s = NaN;
|
|---|
| 320 | else if (s <= 0) h = NaN;
|
|---|
| 321 | return new Hsl(h, s, l, a);
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | function hslConvert(o) {
|
|---|
| 325 | if (o instanceof Hsl) return new Hsl(o.h, o.s, o.l, o.opacity);
|
|---|
| 326 | if (!(o instanceof Color)) o = color(o);
|
|---|
| 327 | if (!o) return new Hsl;
|
|---|
| 328 | if (o instanceof Hsl) return o;
|
|---|
| 329 | o = o.rgb();
|
|---|
| 330 | var r = o.r / 255,
|
|---|
| 331 | g = o.g / 255,
|
|---|
| 332 | b = o.b / 255,
|
|---|
| 333 | min = Math.min(r, g, b),
|
|---|
| 334 | max = Math.max(r, g, b),
|
|---|
| 335 | h = NaN,
|
|---|
| 336 | s = max - min,
|
|---|
| 337 | l = (max + min) / 2;
|
|---|
| 338 | if (s) {
|
|---|
| 339 | if (r === max) h = (g - b) / s + (g < b) * 6;
|
|---|
| 340 | else if (g === max) h = (b - r) / s + 2;
|
|---|
| 341 | else h = (r - g) / s + 4;
|
|---|
| 342 | s /= l < 0.5 ? max + min : 2 - max - min;
|
|---|
| 343 | h *= 60;
|
|---|
| 344 | } else {
|
|---|
| 345 | s = l > 0 && l < 1 ? 0 : h;
|
|---|
| 346 | }
|
|---|
| 347 | return new Hsl(h, s, l, o.opacity);
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | function hsl(h, s, l, opacity) {
|
|---|
| 351 | return arguments.length === 1 ? hslConvert(h) : new Hsl(h, s, l, opacity == null ? 1 : opacity);
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | function Hsl(h, s, l, opacity) {
|
|---|
| 355 | this.h = +h;
|
|---|
| 356 | this.s = +s;
|
|---|
| 357 | this.l = +l;
|
|---|
| 358 | this.opacity = +opacity;
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | define(Hsl, hsl, extend(Color, {
|
|---|
| 362 | brighter(k) {
|
|---|
| 363 | k = k == null ? brighter : Math.pow(brighter, k);
|
|---|
| 364 | return new Hsl(this.h, this.s, this.l * k, this.opacity);
|
|---|
| 365 | },
|
|---|
| 366 | darker(k) {
|
|---|
| 367 | k = k == null ? darker : Math.pow(darker, k);
|
|---|
| 368 | return new Hsl(this.h, this.s, this.l * k, this.opacity);
|
|---|
| 369 | },
|
|---|
| 370 | rgb() {
|
|---|
| 371 | var h = this.h % 360 + (this.h < 0) * 360,
|
|---|
| 372 | s = isNaN(h) || isNaN(this.s) ? 0 : this.s,
|
|---|
| 373 | l = this.l,
|
|---|
| 374 | m2 = l + (l < 0.5 ? l : 1 - l) * s,
|
|---|
| 375 | m1 = 2 * l - m2;
|
|---|
| 376 | return new Rgb(
|
|---|
| 377 | hsl2rgb(h >= 240 ? h - 240 : h + 120, m1, m2),
|
|---|
| 378 | hsl2rgb(h, m1, m2),
|
|---|
| 379 | hsl2rgb(h < 120 ? h + 240 : h - 120, m1, m2),
|
|---|
| 380 | this.opacity
|
|---|
| 381 | );
|
|---|
| 382 | },
|
|---|
| 383 | clamp() {
|
|---|
| 384 | return new Hsl(clamph(this.h), clampt(this.s), clampt(this.l), clampa(this.opacity));
|
|---|
| 385 | },
|
|---|
| 386 | displayable() {
|
|---|
| 387 | return (0 <= this.s && this.s <= 1 || isNaN(this.s))
|
|---|
| 388 | && (0 <= this.l && this.l <= 1)
|
|---|
| 389 | && (0 <= this.opacity && this.opacity <= 1);
|
|---|
| 390 | },
|
|---|
| 391 | formatHsl() {
|
|---|
| 392 | const a = clampa(this.opacity);
|
|---|
| 393 | return `${a === 1 ? "hsl(" : "hsla("}${clamph(this.h)}, ${clampt(this.s) * 100}%, ${clampt(this.l) * 100}%${a === 1 ? ")" : `, ${a})`}`;
|
|---|
| 394 | }
|
|---|
| 395 | }));
|
|---|
| 396 |
|
|---|
| 397 | function clamph(value) {
|
|---|
| 398 | value = (value || 0) % 360;
|
|---|
| 399 | return value < 0 ? value + 360 : value;
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | function clampt(value) {
|
|---|
| 403 | return Math.max(0, Math.min(1, value || 0));
|
|---|
| 404 | }
|
|---|
| 405 |
|
|---|
| 406 | /* From FvD 13.37, CSS Color Module Level 3 */
|
|---|
| 407 | function hsl2rgb(h, m1, m2) {
|
|---|
| 408 | return (h < 60 ? m1 + (m2 - m1) * h / 60
|
|---|
| 409 | : h < 180 ? m2
|
|---|
| 410 | : h < 240 ? m1 + (m2 - m1) * (240 - h) / 60
|
|---|
| 411 | : m1) * 255;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | const radians = Math.PI / 180;
|
|---|
| 415 | const degrees = 180 / Math.PI;
|
|---|
| 416 |
|
|---|
| 417 | // https://observablehq.com/@mbostock/lab-and-rgb
|
|---|
| 418 | const K = 18,
|
|---|
| 419 | Xn = 0.96422,
|
|---|
| 420 | Yn = 1,
|
|---|
| 421 | Zn = 0.82521,
|
|---|
| 422 | t0 = 4 / 29,
|
|---|
| 423 | t1 = 6 / 29,
|
|---|
| 424 | t2 = 3 * t1 * t1,
|
|---|
| 425 | t3 = t1 * t1 * t1;
|
|---|
| 426 |
|
|---|
| 427 | function labConvert(o) {
|
|---|
| 428 | if (o instanceof Lab) return new Lab(o.l, o.a, o.b, o.opacity);
|
|---|
| 429 | if (o instanceof Hcl) return hcl2lab(o);
|
|---|
| 430 | if (!(o instanceof Rgb)) o = rgbConvert(o);
|
|---|
| 431 | var r = rgb2lrgb(o.r),
|
|---|
| 432 | g = rgb2lrgb(o.g),
|
|---|
| 433 | b = rgb2lrgb(o.b),
|
|---|
| 434 | y = xyz2lab((0.2225045 * r + 0.7168786 * g + 0.0606169 * b) / Yn), x, z;
|
|---|
| 435 | if (r === g && g === b) x = z = y; else {
|
|---|
| 436 | x = xyz2lab((0.4360747 * r + 0.3850649 * g + 0.1430804 * b) / Xn);
|
|---|
| 437 | z = xyz2lab((0.0139322 * r + 0.0971045 * g + 0.7141733 * b) / Zn);
|
|---|
| 438 | }
|
|---|
| 439 | return new Lab(116 * y - 16, 500 * (x - y), 200 * (y - z), o.opacity);
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | function gray(l, opacity) {
|
|---|
| 443 | return new Lab(l, 0, 0, opacity == null ? 1 : opacity);
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | function lab(l, a, b, opacity) {
|
|---|
| 447 | return arguments.length === 1 ? labConvert(l) : new Lab(l, a, b, opacity == null ? 1 : opacity);
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | function Lab(l, a, b, opacity) {
|
|---|
| 451 | this.l = +l;
|
|---|
| 452 | this.a = +a;
|
|---|
| 453 | this.b = +b;
|
|---|
| 454 | this.opacity = +opacity;
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | define(Lab, lab, extend(Color, {
|
|---|
| 458 | brighter(k) {
|
|---|
| 459 | return new Lab(this.l + K * (k == null ? 1 : k), this.a, this.b, this.opacity);
|
|---|
| 460 | },
|
|---|
| 461 | darker(k) {
|
|---|
| 462 | return new Lab(this.l - K * (k == null ? 1 : k), this.a, this.b, this.opacity);
|
|---|
| 463 | },
|
|---|
| 464 | rgb() {
|
|---|
| 465 | var y = (this.l + 16) / 116,
|
|---|
| 466 | x = isNaN(this.a) ? y : y + this.a / 500,
|
|---|
| 467 | z = isNaN(this.b) ? y : y - this.b / 200;
|
|---|
| 468 | x = Xn * lab2xyz(x);
|
|---|
| 469 | y = Yn * lab2xyz(y);
|
|---|
| 470 | z = Zn * lab2xyz(z);
|
|---|
| 471 | return new Rgb(
|
|---|
| 472 | lrgb2rgb( 3.1338561 * x - 1.6168667 * y - 0.4906146 * z),
|
|---|
| 473 | lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z),
|
|---|
| 474 | lrgb2rgb( 0.0719453 * x - 0.2289914 * y + 1.4052427 * z),
|
|---|
| 475 | this.opacity
|
|---|
| 476 | );
|
|---|
| 477 | }
|
|---|
| 478 | }));
|
|---|
| 479 |
|
|---|
| 480 | function xyz2lab(t) {
|
|---|
| 481 | return t > t3 ? Math.pow(t, 1 / 3) : t / t2 + t0;
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 | function lab2xyz(t) {
|
|---|
| 485 | return t > t1 ? t * t * t : t2 * (t - t0);
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | function lrgb2rgb(x) {
|
|---|
| 489 | return 255 * (x <= 0.0031308 ? 12.92 * x : 1.055 * Math.pow(x, 1 / 2.4) - 0.055);
|
|---|
| 490 | }
|
|---|
| 491 |
|
|---|
| 492 | function rgb2lrgb(x) {
|
|---|
| 493 | return (x /= 255) <= 0.04045 ? x / 12.92 : Math.pow((x + 0.055) / 1.055, 2.4);
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | function hclConvert(o) {
|
|---|
| 497 | if (o instanceof Hcl) return new Hcl(o.h, o.c, o.l, o.opacity);
|
|---|
| 498 | if (!(o instanceof Lab)) o = labConvert(o);
|
|---|
| 499 | if (o.a === 0 && o.b === 0) return new Hcl(NaN, 0 < o.l && o.l < 100 ? 0 : NaN, o.l, o.opacity);
|
|---|
| 500 | var h = Math.atan2(o.b, o.a) * degrees;
|
|---|
| 501 | return new Hcl(h < 0 ? h + 360 : h, Math.sqrt(o.a * o.a + o.b * o.b), o.l, o.opacity);
|
|---|
| 502 | }
|
|---|
| 503 |
|
|---|
| 504 | function lch(l, c, h, opacity) {
|
|---|
| 505 | return arguments.length === 1 ? hclConvert(l) : new Hcl(h, c, l, opacity == null ? 1 : opacity);
|
|---|
| 506 | }
|
|---|
| 507 |
|
|---|
| 508 | function hcl(h, c, l, opacity) {
|
|---|
| 509 | return arguments.length === 1 ? hclConvert(h) : new Hcl(h, c, l, opacity == null ? 1 : opacity);
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | function Hcl(h, c, l, opacity) {
|
|---|
| 513 | this.h = +h;
|
|---|
| 514 | this.c = +c;
|
|---|
| 515 | this.l = +l;
|
|---|
| 516 | this.opacity = +opacity;
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | function hcl2lab(o) {
|
|---|
| 520 | if (isNaN(o.h)) return new Lab(o.l, 0, 0, o.opacity);
|
|---|
| 521 | var h = o.h * radians;
|
|---|
| 522 | return new Lab(o.l, Math.cos(h) * o.c, Math.sin(h) * o.c, o.opacity);
|
|---|
| 523 | }
|
|---|
| 524 |
|
|---|
| 525 | define(Hcl, hcl, extend(Color, {
|
|---|
| 526 | brighter(k) {
|
|---|
| 527 | return new Hcl(this.h, this.c, this.l + K * (k == null ? 1 : k), this.opacity);
|
|---|
| 528 | },
|
|---|
| 529 | darker(k) {
|
|---|
| 530 | return new Hcl(this.h, this.c, this.l - K * (k == null ? 1 : k), this.opacity);
|
|---|
| 531 | },
|
|---|
| 532 | rgb() {
|
|---|
| 533 | return hcl2lab(this).rgb();
|
|---|
| 534 | }
|
|---|
| 535 | }));
|
|---|
| 536 |
|
|---|
| 537 | var A = -0.14861,
|
|---|
| 538 | B = +1.78277,
|
|---|
| 539 | C = -0.29227,
|
|---|
| 540 | D = -0.90649,
|
|---|
| 541 | E = +1.97294,
|
|---|
| 542 | ED = E * D,
|
|---|
| 543 | EB = E * B,
|
|---|
| 544 | BC_DA = B * C - D * A;
|
|---|
| 545 |
|
|---|
| 546 | function cubehelixConvert(o) {
|
|---|
| 547 | if (o instanceof Cubehelix) return new Cubehelix(o.h, o.s, o.l, o.opacity);
|
|---|
| 548 | if (!(o instanceof Rgb)) o = rgbConvert(o);
|
|---|
| 549 | var r = o.r / 255,
|
|---|
| 550 | g = o.g / 255,
|
|---|
| 551 | b = o.b / 255,
|
|---|
| 552 | l = (BC_DA * b + ED * r - EB * g) / (BC_DA + ED - EB),
|
|---|
| 553 | bl = b - l,
|
|---|
| 554 | k = (E * (g - l) - C * bl) / D,
|
|---|
| 555 | s = Math.sqrt(k * k + bl * bl) / (E * l * (1 - l)), // NaN if l=0 or l=1
|
|---|
| 556 | h = s ? Math.atan2(k, bl) * degrees - 120 : NaN;
|
|---|
| 557 | return new Cubehelix(h < 0 ? h + 360 : h, s, l, o.opacity);
|
|---|
| 558 | }
|
|---|
| 559 |
|
|---|
| 560 | function cubehelix(h, s, l, opacity) {
|
|---|
| 561 | return arguments.length === 1 ? cubehelixConvert(h) : new Cubehelix(h, s, l, opacity == null ? 1 : opacity);
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | function Cubehelix(h, s, l, opacity) {
|
|---|
| 565 | this.h = +h;
|
|---|
| 566 | this.s = +s;
|
|---|
| 567 | this.l = +l;
|
|---|
| 568 | this.opacity = +opacity;
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | define(Cubehelix, cubehelix, extend(Color, {
|
|---|
| 572 | brighter(k) {
|
|---|
| 573 | k = k == null ? brighter : Math.pow(brighter, k);
|
|---|
| 574 | return new Cubehelix(this.h, this.s, this.l * k, this.opacity);
|
|---|
| 575 | },
|
|---|
| 576 | darker(k) {
|
|---|
| 577 | k = k == null ? darker : Math.pow(darker, k);
|
|---|
| 578 | return new Cubehelix(this.h, this.s, this.l * k, this.opacity);
|
|---|
| 579 | },
|
|---|
| 580 | rgb() {
|
|---|
| 581 | var h = isNaN(this.h) ? 0 : (this.h + 120) * radians,
|
|---|
| 582 | l = +this.l,
|
|---|
| 583 | a = isNaN(this.s) ? 0 : this.s * l * (1 - l),
|
|---|
| 584 | cosh = Math.cos(h),
|
|---|
| 585 | sinh = Math.sin(h);
|
|---|
| 586 | return new Rgb(
|
|---|
| 587 | 255 * (l + a * (A * cosh + B * sinh)),
|
|---|
| 588 | 255 * (l + a * (C * cosh + D * sinh)),
|
|---|
| 589 | 255 * (l + a * (E * cosh)),
|
|---|
| 590 | this.opacity
|
|---|
| 591 | );
|
|---|
| 592 | }
|
|---|
| 593 | }));
|
|---|
| 594 |
|
|---|
| 595 | exports.color = color;
|
|---|
| 596 | exports.cubehelix = cubehelix;
|
|---|
| 597 | exports.gray = gray;
|
|---|
| 598 | exports.hcl = hcl;
|
|---|
| 599 | exports.hsl = hsl;
|
|---|
| 600 | exports.lab = lab;
|
|---|
| 601 | exports.lch = lch;
|
|---|
| 602 | exports.rgb = rgb;
|
|---|
| 603 |
|
|---|
| 604 | Object.defineProperty(exports, '__esModule', { value: true });
|
|---|
| 605 |
|
|---|
| 606 | }));
|
|---|