@sardine/colour 2.2.1 → 2.4.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 +0 -3
- package/dist/convertHextoCSSRGB.d.ts +12 -0
- package/dist/convertHextoRGB.d.ts +1 -1
- package/dist/index.cjs +59 -56
- package/dist/index.d.ts +3 -0
- package/dist/index.min.js +4 -4
- package/dist/index.mjs +59 -56
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
[](https://codeclimate.com/github/sardinedev/colour/test_coverage)
|
|
2
|
-
[](https://badge.fury.io/js/%40sardine%2Fcolour)
|
|
3
|
-
|
|
4
1
|
<img alt="Sardine logo" src="https://colour.sardine.dev/assets/icons/logo.svg" width="300">
|
|
5
2
|
|
|
6
3
|
# @sardine/colour
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts an hexadecimal colour into CSS RGB/A colour.
|
|
3
|
+
* @param {string} hex - An hexadecimal colour in the format:
|
|
4
|
+
*
|
|
5
|
+
* - `#000`
|
|
6
|
+
* - `#102030`
|
|
7
|
+
* - `#ffff`
|
|
8
|
+
* - `#102030ff`
|
|
9
|
+
*
|
|
10
|
+
* @returns {string} either a CSS RGB or CSS RGBA string.
|
|
11
|
+
*/
|
|
12
|
+
export declare function convertHextoCSSRGB(hex: string): string;
|
package/dist/index.cjs
CHANGED
|
@@ -14,31 +14,27 @@ function deltaHue_d({ C1, C2, h1_d, h2_d }) {
|
|
|
14
14
|
if (C1 * C2 === 0) {
|
|
15
15
|
return 0;
|
|
16
16
|
}
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
const delta = h2_d - h1_d;
|
|
18
|
+
if (Math.abs(delta) <= 180) {
|
|
19
|
+
return delta;
|
|
19
20
|
}
|
|
20
|
-
if (
|
|
21
|
-
return
|
|
21
|
+
if (delta > 180) {
|
|
22
|
+
return delta - 360;
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
-
return h2_d - h1_d + 360;
|
|
25
|
-
}
|
|
26
|
-
return 0;
|
|
24
|
+
return delta + 360;
|
|
27
25
|
}
|
|
28
26
|
function meanHue_d({ C1, C2, h1_d, h2_d }) {
|
|
29
27
|
if (C1 * C2 === 0) {
|
|
30
28
|
return h2_d + h1_d;
|
|
31
29
|
}
|
|
32
|
-
|
|
30
|
+
const delta = Math.abs(h1_d - h2_d);
|
|
31
|
+
if (delta <= 180) {
|
|
33
32
|
return (h2_d + h1_d) / 2;
|
|
34
33
|
}
|
|
35
|
-
if (
|
|
34
|
+
if (delta > 180 && h1_d + h2_d < 360) {
|
|
36
35
|
return (h2_d + h1_d + 360) / 2;
|
|
37
36
|
}
|
|
38
|
-
|
|
39
|
-
return (h2_d + h1_d - 360) / 2;
|
|
40
|
-
}
|
|
41
|
-
return 0;
|
|
37
|
+
return (h2_d + h1_d - 360) / 2;
|
|
42
38
|
}
|
|
43
39
|
const toRadians = (n) => n * (Math.PI / 180);
|
|
44
40
|
const bigSquare = (n) => Math.sqrt(n ** 7 / (n ** 7 + 25 ** 7));
|
|
@@ -178,6 +174,51 @@ function convertCSSRGBtoHex(colour) {
|
|
|
178
174
|
const rgb = convertCSSRGBtoRGB(colour);
|
|
179
175
|
return convertRGBtoHex(rgb);
|
|
180
176
|
}
|
|
177
|
+
function convertHextoRGB(hex) {
|
|
178
|
+
if (typeof hex !== "string") {
|
|
179
|
+
throw new Error(`convertHextoRGB expects a string but got a ${typeof hex}`);
|
|
180
|
+
}
|
|
181
|
+
if (hex.match(hexRegex)) {
|
|
182
|
+
return {
|
|
183
|
+
R: Number.parseInt(`${hex[1]}${hex[2]}`, 16),
|
|
184
|
+
G: Number.parseInt(`${hex[3]}${hex[4]}`, 16),
|
|
185
|
+
B: Number.parseInt(`${hex[5]}${hex[6]}`, 16)
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
if (hex.match(shortHexRegex)) {
|
|
189
|
+
return {
|
|
190
|
+
R: Number.parseInt(`${hex[1]}${hex[1]}`, 16),
|
|
191
|
+
G: Number.parseInt(`${hex[2]}${hex[2]}`, 16),
|
|
192
|
+
B: Number.parseInt(`${hex[3]}${hex[3]}`, 16)
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
if (hex.match(hexAlphaRegex)) {
|
|
196
|
+
return {
|
|
197
|
+
R: Number.parseInt(`${hex[1]}${hex[2]}`, 16),
|
|
198
|
+
G: Number.parseInt(`${hex[3]}${hex[4]}`, 16),
|
|
199
|
+
B: Number.parseInt(`${hex[5]}${hex[6]}`, 16),
|
|
200
|
+
A: Math.round(Number.parseInt(`${hex[7]}${hex[8]}`, 16) / 255 * 100) / 100
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
if (hex.match(shortAlphaHexRegex)) {
|
|
204
|
+
return {
|
|
205
|
+
R: Number.parseInt(`${hex[1]}${hex[1]}`, 16),
|
|
206
|
+
G: Number.parseInt(`${hex[2]}${hex[2]}`, 16),
|
|
207
|
+
B: Number.parseInt(`${hex[3]}${hex[3]}`, 16),
|
|
208
|
+
A: Math.round(Number.parseInt(`${hex[4]}${hex[4]}`, 16) / 255 * 100) / 100
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
throw new Error(
|
|
212
|
+
`convertHextoRGB expects an valid hexadecimal colour value but got ${hex}`
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
function convertHextoCSSRGB(hex) {
|
|
216
|
+
const rgb = convertHextoRGB(hex);
|
|
217
|
+
if (rgb.A) {
|
|
218
|
+
return `rgba(${rgb.R},${rgb.G},${rgb.B},${rgb.A})`;
|
|
219
|
+
}
|
|
220
|
+
return `rgb(${rgb.R},${rgb.G},${rgb.B})`;
|
|
221
|
+
}
|
|
181
222
|
const namedCSSColours = /* @__PURE__ */ new Map([
|
|
182
223
|
["aliceblue", "#f0f8ff"],
|
|
183
224
|
["antiquewhite", "#faebd7"],
|
|
@@ -335,44 +376,6 @@ function convertHextoNamedCSSColour(colour) {
|
|
|
335
376
|
}
|
|
336
377
|
return void 0;
|
|
337
378
|
}
|
|
338
|
-
function convertHextoRGB(hex) {
|
|
339
|
-
if (typeof hex !== "string") {
|
|
340
|
-
throw new Error(`convertHextoRGB expects a string but got a ${typeof hex}`);
|
|
341
|
-
}
|
|
342
|
-
if (hex.match(hexRegex)) {
|
|
343
|
-
return {
|
|
344
|
-
R: Number.parseInt(`${hex[1]}${hex[2]}`, 16),
|
|
345
|
-
G: Number.parseInt(`${hex[3]}${hex[4]}`, 16),
|
|
346
|
-
B: Number.parseInt(`${hex[5]}${hex[6]}`, 16)
|
|
347
|
-
};
|
|
348
|
-
}
|
|
349
|
-
if (hex.match(shortHexRegex)) {
|
|
350
|
-
return {
|
|
351
|
-
R: Number.parseInt(`${hex[1]}${hex[1]}`, 16),
|
|
352
|
-
G: Number.parseInt(`${hex[2]}${hex[2]}`, 16),
|
|
353
|
-
B: Number.parseInt(`${hex[3]}${hex[3]}`, 16)
|
|
354
|
-
};
|
|
355
|
-
}
|
|
356
|
-
if (hex.match(hexAlphaRegex)) {
|
|
357
|
-
return {
|
|
358
|
-
R: Number.parseInt(`${hex[1]}${hex[2]}`, 16),
|
|
359
|
-
G: Number.parseInt(`${hex[3]}${hex[4]}`, 16),
|
|
360
|
-
B: Number.parseInt(`${hex[5]}${hex[6]}`, 16),
|
|
361
|
-
A: Number.parseInt(`${hex[7]}${hex[8]}`, 16) / 255
|
|
362
|
-
};
|
|
363
|
-
}
|
|
364
|
-
if (hex.match(shortAlphaHexRegex)) {
|
|
365
|
-
return {
|
|
366
|
-
R: Number.parseInt(`${hex[1]}${hex[1]}`, 16),
|
|
367
|
-
G: Number.parseInt(`${hex[2]}${hex[2]}`, 16),
|
|
368
|
-
B: Number.parseInt(`${hex[3]}${hex[3]}`, 16),
|
|
369
|
-
A: Number.parseInt(`${hex[4]}${hex[4]}`, 16) / 255
|
|
370
|
-
};
|
|
371
|
-
}
|
|
372
|
-
throw new Error(
|
|
373
|
-
`convertHextoRGB expects an valid hexadecimal colour value but got ${hex}`
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
379
|
function convertNamedCSSColourtoHex(name) {
|
|
377
380
|
return namedCSSColours.get(name);
|
|
378
381
|
}
|
|
@@ -468,10 +471,7 @@ function findNearestColour(colour, palette) {
|
|
|
468
471
|
if (colourType === "cssRGB") {
|
|
469
472
|
return convertRGBtoCSSRGB(nearest);
|
|
470
473
|
}
|
|
471
|
-
|
|
472
|
-
return convertRGBtoNamedCSSColour(nearest);
|
|
473
|
-
}
|
|
474
|
-
return;
|
|
474
|
+
return convertRGBtoNamedCSSColour(nearest);
|
|
475
475
|
}
|
|
476
476
|
function findNearestHexColour(colour, palette) {
|
|
477
477
|
if (!palette || palette.length < 2) {
|
|
@@ -645,6 +645,7 @@ exports.RGBdistance = RGBdistance;
|
|
|
645
645
|
exports.ciede2000 = ciede2000;
|
|
646
646
|
exports.convertCSSRGBtoHex = convertCSSRGBtoHex;
|
|
647
647
|
exports.convertCSSRGBtoRGB = convertCSSRGBtoRGB;
|
|
648
|
+
exports.convertHextoCSSRGB = convertHextoCSSRGB;
|
|
648
649
|
exports.convertHextoNamedCSSColour = convertHextoNamedCSSColour;
|
|
649
650
|
exports.convertHextoRGB = convertHextoRGB;
|
|
650
651
|
exports.convertNamedCSSColourtoHex = convertNamedCSSColourtoHex;
|
|
@@ -670,5 +671,7 @@ exports.isCSSNamedDarkColour = isCSSNamedDarkColour;
|
|
|
670
671
|
exports.isCSSRGBColour = isCSSRGBColour;
|
|
671
672
|
exports.isCSSRGBDarkColour = isCSSRGBDarkColour;
|
|
672
673
|
exports.isDarkColour = isDarkColour;
|
|
674
|
+
exports.isHexColour = isHexColour;
|
|
673
675
|
exports.isHexDarkColour = isHexDarkColour;
|
|
676
|
+
exports.isNamedCSSColour = isNamedCSSColour;
|
|
674
677
|
exports.pickHexColourContrast = pickHexColourContrast;
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ export { RGBdistance } from "./RGBdistance";
|
|
|
2
2
|
export { ciede2000 } from "./CIEDE2000";
|
|
3
3
|
export { convertCSSRGBtoHex } from "./convertCSSRGBtoHex";
|
|
4
4
|
export { convertCSSRGBtoRGB } from "./convertCSSRGBtoRGB";
|
|
5
|
+
export { convertHextoCSSRGB } from "./convertHextoCSSRGB";
|
|
5
6
|
export { convertHextoNamedCSSColour } from "./convertHextoNamedCSSColour";
|
|
6
7
|
export { convertHextoRGB } from "./convertHextoRGB";
|
|
7
8
|
export { convertNamedCSSColourtoHex } from "./convertNamedCSSColourtoHex";
|
|
@@ -27,5 +28,7 @@ export { isCSSNamedDarkColour } from "./isCSSNameDarkColour";
|
|
|
27
28
|
export { isCSSRGBColour } from "./assertions";
|
|
28
29
|
export { isCSSRGBDarkColour } from "./isCSSRGBDarkColour";
|
|
29
30
|
export { isDarkColour } from "./isDarkColour";
|
|
31
|
+
export { isHexColour } from "./assertions";
|
|
30
32
|
export { isHexDarkColour } from "./isHexDarkColour";
|
|
33
|
+
export { isNamedCSSColour } from "./assertions";
|
|
31
34
|
export { pickHexColourContrast } from "./pickHexColourContrast";
|
package/dist/index.min.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
"use strict";function
|
|
1
|
+
"use strict";function j(e,t){if(e===0&&t===0)return 0;const n=Math.atan2(e,t)*(180/Math.PI);return n>=0?n:n+360}function Ce({C1:e,C2:t,h1_d:n,h2_d:o}){if(e*t===0)return 0;const r=o-n;return Math.abs(r)<=180?r:r>180?r-360:r+360}function Se({C1:e,C2:t,h1_d:n,h2_d:o}){if(e*t===0)return o+n;const r=Math.abs(n-o);return r<=180?(o+n)/2:r>180&&n+o<360?(o+n+360)/2:(o+n-360)/2}const b=e=>e*(Math.PI/180),O=e=>Math.sqrt(e**7/(e**7+25**7));function g(e,t){const n=e/255,o=t?.03928:.04045;let r;return n>o?r=((n+.055)/1.055)**2.4:r=n/12.92,r}function y(e){const t=.20689655172413793,n=t**3;let o;return e>n?o=Math.cbrt(e):o=e/(3*t**2)+4/29,o}function pe(e,t,n){return Math.min(Math.max(e,t),n)}function V(e,t){const n=e.L,o=e.a,r=e.b,a=t.L,s=t.a,c=t.b,f=1,m=1,G=1,h=Math.sqrt(o**2+r**2),C=Math.sqrt(s**2+c**2),ue=a-n,le=(h+C)/2,F=.5*(1-O(le)),E=o*(1+F),Y=s*(1+F),B=Math.sqrt(E**2+r**2),k=Math.sqrt(Y**2+c**2),w=(B+k)/2,X=k-B,Z=j(r,E),D=j(c,Y),de=Ce({C1:h,C2:C,h1_d:Z,h2_d:D}),z=2*Math.sqrt(B*k)*Math.sin(b(de)/2),S=Se({C1:h,C2:C,h1_d:Z,h2_d:D}),_=(n+a)/2,me=1-.17*Math.cos(b(S-30))+.24*Math.cos(b(2*S))+.32*Math.cos(b(3*S+6))-.2*Math.cos(b(4*S-63)),be=1+.015*(_-50)**2/Math.sqrt(20+(_-50)**2),T=.045*w+1,U=1+.015*w*me,ge=30*Math.exp(-(((S-275)/25)**2)),he=-2*O(w)*Math.sin(b(ge*2));return Math.sqrt((ue/(f*be))**2+(X/(m*T))**2+(z/(G*U))**2+he*(X/(m*T))*(z/(G*U)))}function J(e){const{R:t,G:n,B:o}=e,r=g(t)*100,a=g(n)*100,s=g(o)*100,c=r*.4124+a*.3576+s*.1805,f=r*.2126+a*.7152+s*.0722,m=r*.0193+a*.1192+s*.9505;return{X:c,Y:f,Z:m}}function K(e){const{X:t,Y:n,Z:o}=e,r=t/95.047,a=n/100,s=o/108.883,c=y(r),f=y(a),m=y(s),G=116*f-16,h=500*(c-f),C=200*(f-m);return{L:G,a:h,b:C}}function N(e){const t=J(e);return K(t)}const Q=(e,t)=>{const n=N(e),o=N(t);return V(n,o)},ee=/^#[a-fA-F0-9]{6}$/,te=/^#[a-fA-F0-9]{8}$/,ne=/^#[a-fA-F0-9]{3}$/,re=/^#[a-fA-F0-9]{4}$/,oe=/^rgba*\(\s*([-+]?\d+)\s*(?:,)?\s*([-+]?\d+)\s*(?:,)?\s*([-+]?\d+)\s*(?:,*|\/*)\s*([-+]?\d*[.]?\d+[%]?)*\)$/i;function i(e){const t=e.match(oe);if(!t)throw new Error(`convertCSSRGBtoHex expects a valid CSS RGB string but got ${e}`);const n=o=>o?Number.parseFloat(o):void 0;return{R:n(t[1]),G:n(t[2]),B:n(t[3]),A:n(t[4])}}function p({R:e,G:t,B:n,A:o}){const r=a=>pe(a,0,255).toString(16).padStart(2,"0");return`#${r(e)}${r(t)}${r(n)}${o?r(Math.round(o*255)):""}`}function M(e){const t=i(e);return p(t)}function u(e){if(typeof e!="string")throw new Error(`convertHextoRGB expects a string but got a ${typeof e}`);if(e.match(ee))return{R:Number.parseInt(`${e[1]}${e[2]}`,16),G:Number.parseInt(`${e[3]}${e[4]}`,16),B:Number.parseInt(`${e[5]}${e[6]}`,16)};if(e.match(ne))return{R:Number.parseInt(`${e[1]}${e[1]}`,16),G:Number.parseInt(`${e[2]}${e[2]}`,16),B:Number.parseInt(`${e[3]}${e[3]}`,16)};if(e.match(te))return{R:Number.parseInt(`${e[1]}${e[2]}`,16),G:Number.parseInt(`${e[3]}${e[4]}`,16),B:Number.parseInt(`${e[5]}${e[6]}`,16),A:Math.round(Number.parseInt(`${e[7]}${e[8]}`,16)/255*100)/100};if(e.match(re))return{R:Number.parseInt(`${e[1]}${e[1]}`,16),G:Number.parseInt(`${e[2]}${e[2]}`,16),B:Number.parseInt(`${e[3]}${e[3]}`,16),A:Math.round(Number.parseInt(`${e[4]}${e[4]}`,16)/255*100)/100};throw new Error(`convertHextoRGB expects an valid hexadecimal colour value but got ${e}`)}function Re(e){const t=u(e);return t.A?`rgba(${t.R},${t.G},${t.B},${t.A})`:`rgb(${t.R},${t.G},${t.B})`}const H=new Map([["aliceblue","#f0f8ff"],["antiquewhite","#faebd7"],["aqua","#00ffff"],["aquamarine","#7fffd4"],["azure","#f0ffff"],["beige","#f5f5dc"],["bisque","#ffe4c4"],["black","#000000"],["blanchedalmond","#ffebcd"],["blue","#0000ff"],["blueviolet","#8a2be2"],["brown","#a52a2a"],["burlywood","#deb887"],["cadetblue","#5f9ea0"],["chartreuse","#7fff00"],["chocolate","#d2691e"],["coral","#ff7f50"],["cornflowerblue","#6495ed"],["cornsilk","#fff8dc"],["crimson","#dc143c"],["cyan","#00ffff"],["darkblue","#00008b"],["darkcyan","#008b8b"],["darkgoldenrod","#b8860b"],["darkgray","#a9a9a9"],["darkgreen","#006400"],["darkgrey","#a9a9a9"],["darkkhaki","#bdb76b"],["darkmagenta","#8b008b"],["darkolivegreen","#556b2f"],["darkorange","#ff8c00"],["darkorchid","#9932cc"],["darkred","#8b0000"],["darksalmon","#e9967a"],["darkseagreen","#8fbc8f"],["darkslateblue","#483d8b"],["darkslategray","#2f4f4f"],["darkslategrey","#2f4f4f"],["darkturquoise","#00ced1"],["darkviolet","#9400d3"],["deeppink","#ff1493"],["deepskyblue","#00bfff"],["dimgray","#696969"],["dimgrey","#696969"],["dodgerblue","#1e90ff"],["firebrick","#b22222"],["floralwhite","#fffaf0"],["forestgreen","#228b22"],["fuchsia","#ff00ff"],["gainsboro","#dcdcdc"],["ghostwhite","#f8f8ff"],["gold","#ffd700"],["goldenrod","#daa520"],["gray","#808080"],["green","#008000"],["greenyellow","#adff2f"],["grey","#808080"],["honeydew","#f0fff0"],["hotpink","#ff69b4"],["indianred","#cd5c5c"],["indigo","#4b0082"],["ivory","#fffff0"],["khaki","#f0e68c"],["lavender","#e6e6fa"],["lavenderblush","#fff0f5"],["lawngreen","#7cfc00"],["lemonchiffon","#fffacd"],["lightblue","#add8e6"],["lightcoral","#f08080"],["lightcyan","#e0ffff"],["lightgoldenrodyellow","#fafad2"],["lightgray","#d3d3d3"],["lightgreen","#90ee90"],["lightgrey","#d3d3d3"],["lightpink","#ffb6c1"],["lightsalmon","#ffa07a"],["lightseagreen","#20b2aa"],["lightskyblue","#87cefa"],["lightslategray","#778899"],["lightslategrey","#778899"],["lightsteelblue","#b0c4de"],["lightyellow","#ffffe0"],["lime","#00ff00"],["limegreen","#32cd32"],["linen","#faf0e6"],["magenta","#ff00ff"],["maroon","#800000"],["mediumaquamarine","#66cdaa"],["mediumblue","#0000cd"],["mediumorchid","#ba55d3"],["mediumpurple","#9370db"],["mediumseagreen","#3cb371"],["mediumslateblue","#7b68ee"],["mediumspringgreen","#00fa9a"],["mediumturquoise","#48d1cc"],["mediumvioletred","#c71585"],["midnightblue","#191970"],["mintcream","#f5fffa"],["mistyrose","#ffe4e1"],["moccasin","#ffe4b5"],["navajowhite","#ffdead"],["navy","#000080"],["oldlace","#fdf5e6"],["olive","#808000"],["olivedrab","#6b8e23"],["orangered","#ff4500"],["orchid","#da70d6"],["palegoldenrod","#eee8aa"],["palegreen","#98fb98"],["paleturquoise","#afeeee"],["palevioletred","#db7093"],["papayawhip","#ffefd5"],["peachpuff","#ffdab9"],["peru","#cd853f"],["pink","#ffc0cb"],["plum","#dda0dd"],["powderblue","#b0e0e6"],["purple","#800080"],["red","#ff0000"],["rosybrown","#bc8f8f"],["royalblue","#4169e1"],["saddlebrown","#8b4513"],["salmon","#fa8072"],["sandybrown","#f4a460"],["seagreen","#2e8b57"],["seashell","#fff5ee"],["sienna","#a0522d"],["silver","#c0c0c0"],["skyblue","#87ceeb"],["slateblue","#6a5acd"],["slategray","#708090"],["slategrey","#708090"],["snow","#fffafa"],["springgreen","#00ff7f"],["steelblue","#4682b4"],["tan","#d2b48c"],["teal","#008080"],["thistle","#d8bfd8"],["tomato","#ff6347"],["transparent","#00000000"],["turquoise","#40e0d0"],["violet","#ee82ee"],["wheat","#f5deb3"],["white","#ffffff"],["whitesmoke","#f5f5f5"],["yellow","#ffff00"],["yellowgreen","#9acd32"]]);function ae(e){for(const[t,n]of H.entries())if(n===e)return t}function l(e){return H.get(e)}function R(e){const t=l(e);if(t)return u(t)}function q({R:e,G:t,B:n,A:o}){return`rgb(${e} ${t} ${n}${o?` / ${o}`:""})`}function I(e){const t=p(e);return ae(t)}function $(e,t){if(!t||t.length<2)return e;const n=[];for(const r of t){const a=Q(e,r);n.push([r,a])}return n.sort((r,a)=>r[1]-a[1])[0][0]}function $e(e,t){if(!t||t.length<2)return e;const n=i(e),o=t.map(a=>i(a)),r=$(n,o);return q(r)}function L(e){return!!e.match(oe)}function W(e){return!!e.match(ee)||!!e.match(te)||!!e.match(re)||!!e.match(ne)}function x(e){return H.has(e)}function ve(e,t){if(!t||t.length<2)return e;let n,o;const r=[];if(W(e)&&(n=u(e),o="hex"),L(e)&&(n=i(e),o="cssRGB"),x(e)&&(n=R(e),o="namedCSS"),!n)return;for(const s of t)W(s)&&r.push(u(s)),L(s)&&r.push(i(s)),x(s)&&r.push(R(s));if(r.length<2)return e;const a=$(n,r);return o==="hex"?p(a):o==="cssRGB"?q(a):I(a)}function Ge(e,t){if(!t||t.length<2)return e;const n=u(e),o=t.map(a=>u(a)),r=$(n,o);return p(r)}function Be(e,t){if(!t||t.length<2)return e;const n=R(e),r=t.map(s=>R(s)).filter(s=>s!==void 0);if(!n||r.length<2)return e;const a=$(n,r);return I(a)}function v({R:e,G:t,B:n},o){const r=o==="WCAG2.1",a=g(e,r),s=g(t,r),c=g(n,r);return .2126*a+.7152*s+.0722*c}function d(e,t){const n=u(e);return v(n,t)}function se(e,t){const n=Math.max(e,t),o=Math.min(e,t),r=(n+.05)/(o+.05);return Math.floor(r*1e3)/1e3}function P(e,t,n){const o=d(e,n),r=d(t,n);return se(o,r)}function ke(e,t,n){let o,r;if(e.startsWith("#"))o=e;else if(e.startsWith("rgb"))o=M(e);else{const a=l(e);if(a===void 0)throw new Error(`getContrastRatio expects valid CSS named colours.
|
|
2
2
|
${e} is not a valid CSS named colour.
|
|
3
|
-
See https://developer.mozilla.org/en-US/docs/Web/CSS/named-color`);
|
|
3
|
+
See https://developer.mozilla.org/en-US/docs/Web/CSS/named-color`);o=a}if(t.startsWith("#"))r=t;else if(t.startsWith("rgb"))r=M(t);else{const a=l(t);if(a===void 0)throw new Error(`getContrastRatio expects valid CSS named colours.
|
|
4
4
|
${t} is not a valid CSS named colour.
|
|
5
|
-
See https://developer.mozilla.org/en-US/docs/Web/CSS/named-color`);
|
|
5
|
+
See https://developer.mozilla.org/en-US/docs/Web/CSS/named-color`);r=a}return P(o,r,n)}function we(e,t,n){const o=i(e),r=i(t),a=v(o,n),s=v(r,n);return se(a,s)}function ye(e,t,n){const o=l(e),r=l(t);if(o===void 0||r===void 0)throw new Error(`getContrastRatioFromNamedCSSColour expects valid CSS named colours.
|
|
6
6
|
${e} or ${t} are not valid CSS named colours.
|
|
7
|
-
See https://developer.mozilla.org/en-US/docs/Web/CSS/named-color`);return
|
|
7
|
+
See https://developer.mozilla.org/en-US/docs/Web/CSS/named-color`);return P(o,r,n)}function A(e,t){const n=d(e,t)+.05,o=1.05/n,r=n/.05;return o>r}function ce(e,t){const n=l(e);if(n)return A(n,t);throw new Error(`${e} is not a valid colour format. isCSSNamedDarkColour only accepts CSS named colours. Check more details here https://developer.mozilla.org/en-US/docs/Web/CSS/named-color`)}function fe(e,t){const n=i(e),o=v(n,t),r=1.05/o,a=o/.05;return r>a}function Ne(e,t){try{return e.startsWith("#")?A(e,t):e.startsWith("rgb")?fe(e,t):ce(e,t)}catch{throw new Error(`${e} is not a valid colour format. isDarkColour accepts CSS RGB formats, ie rgb(0,0,0) and rgba(255, 255, 255, 0.4), hexadecimal and CSS named colours.`)}}const ie=(e,t)=>e>t?e/t:t/e,Me=({backgroundColour:e,optionOneColour:t,optionTwoColour:n},o)=>{const r=d(e,o)+.05,a=d(t,o)+.05,s=d(n,o)+.05,c=ie(a,r),f=ie(s,r);return c>f?t:n};export{Q as RGBdistance,V as ciede2000,M as convertCSSRGBtoHex,i as convertCSSRGBtoRGB,Re as convertHextoCSSRGB,ae as convertHextoNamedCSSColour,u as convertHextoRGB,l as convertNamedCSSColourtoHex,R as convertNamedCSSColourtoRGB,q as convertRGBtoCSSRGB,p as convertRGBtoHex,N as convertRGBtoLab,I as convertRGBtoNamedCSSColour,J as convertRGBtoXYZ,K as convertXYZtoLab,$e as findNearestCSSRGBColour,ve as findNearestColour,Ge as findNearestHexColour,Be as findNearestNamedCSSColour,$ as findNearestRGBColour,ke as getContrastRatio,we as getContrastRatioFromCSSRGB,P as getContrastRatioFromHex,ye as getContrastRatioFromNamedCSSColour,d as getSRGBLuminanceFromHex,v as getSRGBLuminanceFromRGB,ce as isCSSNamedDarkColour,L as isCSSRGBColour,fe as isCSSRGBDarkColour,Ne as isDarkColour,W as isHexColour,A as isHexDarkColour,x as isNamedCSSColour,Me as pickHexColourContrast};
|
package/dist/index.mjs
CHANGED
|
@@ -12,31 +12,27 @@ function deltaHue_d({ C1, C2, h1_d, h2_d }) {
|
|
|
12
12
|
if (C1 * C2 === 0) {
|
|
13
13
|
return 0;
|
|
14
14
|
}
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
const delta = h2_d - h1_d;
|
|
16
|
+
if (Math.abs(delta) <= 180) {
|
|
17
|
+
return delta;
|
|
17
18
|
}
|
|
18
|
-
if (
|
|
19
|
-
return
|
|
19
|
+
if (delta > 180) {
|
|
20
|
+
return delta - 360;
|
|
20
21
|
}
|
|
21
|
-
|
|
22
|
-
return h2_d - h1_d + 360;
|
|
23
|
-
}
|
|
24
|
-
return 0;
|
|
22
|
+
return delta + 360;
|
|
25
23
|
}
|
|
26
24
|
function meanHue_d({ C1, C2, h1_d, h2_d }) {
|
|
27
25
|
if (C1 * C2 === 0) {
|
|
28
26
|
return h2_d + h1_d;
|
|
29
27
|
}
|
|
30
|
-
|
|
28
|
+
const delta = Math.abs(h1_d - h2_d);
|
|
29
|
+
if (delta <= 180) {
|
|
31
30
|
return (h2_d + h1_d) / 2;
|
|
32
31
|
}
|
|
33
|
-
if (
|
|
32
|
+
if (delta > 180 && h1_d + h2_d < 360) {
|
|
34
33
|
return (h2_d + h1_d + 360) / 2;
|
|
35
34
|
}
|
|
36
|
-
|
|
37
|
-
return (h2_d + h1_d - 360) / 2;
|
|
38
|
-
}
|
|
39
|
-
return 0;
|
|
35
|
+
return (h2_d + h1_d - 360) / 2;
|
|
40
36
|
}
|
|
41
37
|
const toRadians = (n) => n * (Math.PI / 180);
|
|
42
38
|
const bigSquare = (n) => Math.sqrt(n ** 7 / (n ** 7 + 25 ** 7));
|
|
@@ -176,6 +172,51 @@ function convertCSSRGBtoHex(colour) {
|
|
|
176
172
|
const rgb = convertCSSRGBtoRGB(colour);
|
|
177
173
|
return convertRGBtoHex(rgb);
|
|
178
174
|
}
|
|
175
|
+
function convertHextoRGB(hex) {
|
|
176
|
+
if (typeof hex !== "string") {
|
|
177
|
+
throw new Error(`convertHextoRGB expects a string but got a ${typeof hex}`);
|
|
178
|
+
}
|
|
179
|
+
if (hex.match(hexRegex)) {
|
|
180
|
+
return {
|
|
181
|
+
R: Number.parseInt(`${hex[1]}${hex[2]}`, 16),
|
|
182
|
+
G: Number.parseInt(`${hex[3]}${hex[4]}`, 16),
|
|
183
|
+
B: Number.parseInt(`${hex[5]}${hex[6]}`, 16)
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (hex.match(shortHexRegex)) {
|
|
187
|
+
return {
|
|
188
|
+
R: Number.parseInt(`${hex[1]}${hex[1]}`, 16),
|
|
189
|
+
G: Number.parseInt(`${hex[2]}${hex[2]}`, 16),
|
|
190
|
+
B: Number.parseInt(`${hex[3]}${hex[3]}`, 16)
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
if (hex.match(hexAlphaRegex)) {
|
|
194
|
+
return {
|
|
195
|
+
R: Number.parseInt(`${hex[1]}${hex[2]}`, 16),
|
|
196
|
+
G: Number.parseInt(`${hex[3]}${hex[4]}`, 16),
|
|
197
|
+
B: Number.parseInt(`${hex[5]}${hex[6]}`, 16),
|
|
198
|
+
A: Math.round(Number.parseInt(`${hex[7]}${hex[8]}`, 16) / 255 * 100) / 100
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
if (hex.match(shortAlphaHexRegex)) {
|
|
202
|
+
return {
|
|
203
|
+
R: Number.parseInt(`${hex[1]}${hex[1]}`, 16),
|
|
204
|
+
G: Number.parseInt(`${hex[2]}${hex[2]}`, 16),
|
|
205
|
+
B: Number.parseInt(`${hex[3]}${hex[3]}`, 16),
|
|
206
|
+
A: Math.round(Number.parseInt(`${hex[4]}${hex[4]}`, 16) / 255 * 100) / 100
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
throw new Error(
|
|
210
|
+
`convertHextoRGB expects an valid hexadecimal colour value but got ${hex}`
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
function convertHextoCSSRGB(hex) {
|
|
214
|
+
const rgb = convertHextoRGB(hex);
|
|
215
|
+
if (rgb.A) {
|
|
216
|
+
return `rgba(${rgb.R},${rgb.G},${rgb.B},${rgb.A})`;
|
|
217
|
+
}
|
|
218
|
+
return `rgb(${rgb.R},${rgb.G},${rgb.B})`;
|
|
219
|
+
}
|
|
179
220
|
const namedCSSColours = /* @__PURE__ */ new Map([
|
|
180
221
|
["aliceblue", "#f0f8ff"],
|
|
181
222
|
["antiquewhite", "#faebd7"],
|
|
@@ -333,44 +374,6 @@ function convertHextoNamedCSSColour(colour) {
|
|
|
333
374
|
}
|
|
334
375
|
return void 0;
|
|
335
376
|
}
|
|
336
|
-
function convertHextoRGB(hex) {
|
|
337
|
-
if (typeof hex !== "string") {
|
|
338
|
-
throw new Error(`convertHextoRGB expects a string but got a ${typeof hex}`);
|
|
339
|
-
}
|
|
340
|
-
if (hex.match(hexRegex)) {
|
|
341
|
-
return {
|
|
342
|
-
R: Number.parseInt(`${hex[1]}${hex[2]}`, 16),
|
|
343
|
-
G: Number.parseInt(`${hex[3]}${hex[4]}`, 16),
|
|
344
|
-
B: Number.parseInt(`${hex[5]}${hex[6]}`, 16)
|
|
345
|
-
};
|
|
346
|
-
}
|
|
347
|
-
if (hex.match(shortHexRegex)) {
|
|
348
|
-
return {
|
|
349
|
-
R: Number.parseInt(`${hex[1]}${hex[1]}`, 16),
|
|
350
|
-
G: Number.parseInt(`${hex[2]}${hex[2]}`, 16),
|
|
351
|
-
B: Number.parseInt(`${hex[3]}${hex[3]}`, 16)
|
|
352
|
-
};
|
|
353
|
-
}
|
|
354
|
-
if (hex.match(hexAlphaRegex)) {
|
|
355
|
-
return {
|
|
356
|
-
R: Number.parseInt(`${hex[1]}${hex[2]}`, 16),
|
|
357
|
-
G: Number.parseInt(`${hex[3]}${hex[4]}`, 16),
|
|
358
|
-
B: Number.parseInt(`${hex[5]}${hex[6]}`, 16),
|
|
359
|
-
A: Number.parseInt(`${hex[7]}${hex[8]}`, 16) / 255
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
if (hex.match(shortAlphaHexRegex)) {
|
|
363
|
-
return {
|
|
364
|
-
R: Number.parseInt(`${hex[1]}${hex[1]}`, 16),
|
|
365
|
-
G: Number.parseInt(`${hex[2]}${hex[2]}`, 16),
|
|
366
|
-
B: Number.parseInt(`${hex[3]}${hex[3]}`, 16),
|
|
367
|
-
A: Number.parseInt(`${hex[4]}${hex[4]}`, 16) / 255
|
|
368
|
-
};
|
|
369
|
-
}
|
|
370
|
-
throw new Error(
|
|
371
|
-
`convertHextoRGB expects an valid hexadecimal colour value but got ${hex}`
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
377
|
function convertNamedCSSColourtoHex(name) {
|
|
375
378
|
return namedCSSColours.get(name);
|
|
376
379
|
}
|
|
@@ -466,10 +469,7 @@ function findNearestColour(colour, palette) {
|
|
|
466
469
|
if (colourType === "cssRGB") {
|
|
467
470
|
return convertRGBtoCSSRGB(nearest);
|
|
468
471
|
}
|
|
469
|
-
|
|
470
|
-
return convertRGBtoNamedCSSColour(nearest);
|
|
471
|
-
}
|
|
472
|
-
return;
|
|
472
|
+
return convertRGBtoNamedCSSColour(nearest);
|
|
473
473
|
}
|
|
474
474
|
function findNearestHexColour(colour, palette) {
|
|
475
475
|
if (!palette || palette.length < 2) {
|
|
@@ -644,6 +644,7 @@ export {
|
|
|
644
644
|
ciede2000,
|
|
645
645
|
convertCSSRGBtoHex,
|
|
646
646
|
convertCSSRGBtoRGB,
|
|
647
|
+
convertHextoCSSRGB,
|
|
647
648
|
convertHextoNamedCSSColour,
|
|
648
649
|
convertHextoRGB,
|
|
649
650
|
convertNamedCSSColourtoHex,
|
|
@@ -669,6 +670,8 @@ export {
|
|
|
669
670
|
isCSSRGBColour,
|
|
670
671
|
isCSSRGBDarkColour,
|
|
671
672
|
isDarkColour,
|
|
673
|
+
isHexColour,
|
|
672
674
|
isHexDarkColour,
|
|
675
|
+
isNamedCSSColour,
|
|
673
676
|
pickHexColourContrast
|
|
674
677
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sardine/colour",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "It does things to colours",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -53,7 +53,7 @@
|
|
|
53
53
|
"@vitest/coverage-v8": "^2.0.0",
|
|
54
54
|
"lefthook": "^1.8.1",
|
|
55
55
|
"typescript": "^5.4.5",
|
|
56
|
-
"vite": "^
|
|
56
|
+
"vite": "^6.0.0",
|
|
57
57
|
"vitest": "^2.0.0"
|
|
58
58
|
}
|
|
59
59
|
}
|