color-name-list 9.11.0 → 9.14.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "color-name-list",
3
- "version": "9.11.0",
3
+ "version": "9.14.0",
4
4
  "description": "long list of color names",
5
5
  "main": "dist/colornames.json",
6
6
  "browser": "dist/colornames.umd.js",
@@ -0,0 +1,65 @@
1
+ const fs = require('fs');
2
+ const RGB_HEX = /^#?(?:([\da-f]{3})[\da-f]?|([\da-f]{6})(?:[\da-f]{2})?)$/i;
3
+
4
+ const namedColors = JSON.parse(
5
+ fs.readFileSync(__dirname + '/../../dist/colornames.json', 'utf8')
6
+ );
7
+
8
+ const hexToRgb = (hexSrt) => {
9
+ const [, short, long] = String(hexSrt).match(RGB_HEX) || [];
10
+
11
+ if (long) {
12
+ const value = Number.parseInt(long, 16);
13
+ return {
14
+ r: value >> 16,
15
+ g: value >> 8 & 0xFF,
16
+ b: value & 0xFF,
17
+ };
18
+ } else if (short) {
19
+ const rgbArray = Array.from(short,
20
+ (s) => Number.parseInt(s, 16)
21
+ ).map((n) => (n << 4) | n);
22
+ return {
23
+ r: rgbArray[0],
24
+ g: rgbArray[1],
25
+ b: rgbArray[2],
26
+ };
27
+ }
28
+ };
29
+
30
+ const rgbColors = namedColors.map((color) => {
31
+ const {r,g,b} = hexToRgb(color.hex);
32
+ return [r, g, b];
33
+ });
34
+
35
+
36
+ function calculateDistance(r, g, b, rgbColors) {
37
+ let distance = 0;
38
+ for (let i = 0; i < rgbColors.length; i++) {
39
+ const rgb = rgbColors[i];
40
+ distance += Math.abs(r - rgb[0]) + Math.abs(g - rgb[1]) + Math.abs(b - rgb[2]);
41
+ }
42
+ return distance;
43
+ }
44
+
45
+ // create new RGB color with the largest possible distance from all other rgbColors
46
+ function findLargestSpaceInRGB(rgbColors) {
47
+ const rgb = [0, 0, 0];
48
+ let largestDistance = 0;
49
+ for (let i = 0; i < 256; i+=4) {
50
+ for (let j = 0; j < 256; j+=4) {
51
+ for (let k = 0; k < 256; k+=4) {
52
+ const distance = calculateDistance(i, j, k, rgbColors);
53
+ if (distance > largestDistance) {
54
+ largestDistance = distance;
55
+ rgb[0] = i;
56
+ rgb[1] = j;
57
+ rgb[2] = k;
58
+ }
59
+ }
60
+ }
61
+ }
62
+ return rgb;
63
+ }
64
+
65
+ console.log(findLargestSpaceInRGB(rgbColors));