color-name-list 10.0.0 → 10.0.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/package.json +1 -1
- package/scripts/findColors.js +0 -120
- package/scripts/generatePaletteName.js +0 -48
package/package.json
CHANGED
package/scripts/findColors.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
const lib = require('./lib.js');
|
|
2
|
-
const ClosestVector = require('closestvector');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* enriches color object and fills RGB color arrays
|
|
6
|
-
* Warning: Not a pure function at all :D
|
|
7
|
-
* @param {object} colorObj hex representation of color
|
|
8
|
-
* @param {array} rgbColorArrRef reference to RGB color array
|
|
9
|
-
* @return {object} enriched color object
|
|
10
|
-
*/
|
|
11
|
-
const enrichColorObj = (colorObj, rgbColorArrRef) => {
|
|
12
|
-
const rgb = lib.hexToRgb(colorObj.hex);
|
|
13
|
-
// populates array needed for ClosestVector()
|
|
14
|
-
rgbColorArrRef.push([rgb.r, rgb.g, rgb.b]);
|
|
15
|
-
// transform hex to RGB
|
|
16
|
-
colorObj.rgb = rgb;
|
|
17
|
-
// get hsl color value
|
|
18
|
-
colorObj.hsl = lib.rgbToHsl(...Object.values(rgb));
|
|
19
|
-
|
|
20
|
-
// calculate luminancy for each color
|
|
21
|
-
colorObj.luminance = lib.luminance(rgb);
|
|
22
|
-
|
|
23
|
-
return colorObj;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
module.exports = class FindColors {
|
|
27
|
-
constructor(colorsListsObj) {
|
|
28
|
-
this.colorLists = colorsListsObj;
|
|
29
|
-
|
|
30
|
-
// object containing the name:hex pairs for nearestColor()
|
|
31
|
-
this.colorListsRGBArrays = {};
|
|
32
|
-
this.closestInstances = {};
|
|
33
|
-
|
|
34
|
-
// prepare color array
|
|
35
|
-
Object.keys(this.colorLists).forEach((listName) => {
|
|
36
|
-
this.colorListsRGBArrays[listName] = [];
|
|
37
|
-
|
|
38
|
-
this.colorLists[listName].forEach(c => {
|
|
39
|
-
enrichColorObj(c, this.colorListsRGBArrays[listName]);
|
|
40
|
-
});
|
|
41
|
-
|
|
42
|
-
Object.freeze(this.colorLists[listName]);
|
|
43
|
-
this.closestInstances[listName] = new ClosestVector(
|
|
44
|
-
this.colorListsRGBArrays[listName]
|
|
45
|
-
);
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
_validateListKey (listKey) {
|
|
50
|
-
if (!this.colorLists[listKey]) {
|
|
51
|
-
throw new Error(`List key "${listKey}" is not valid.`);
|
|
52
|
-
} else {
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* returns all colors that match a name
|
|
59
|
-
* @param {string} searchStr search term
|
|
60
|
-
* @param {boolen} bestOf if set only returns good names
|
|
61
|
-
*/
|
|
62
|
-
searchNames (searchStr, listKey = 'default') {
|
|
63
|
-
this._validateListKey(listKey);
|
|
64
|
-
return this.colorLists[listKey].filter(
|
|
65
|
-
color => color.name.toLowerCase().includes(searchStr.toLowerCase())
|
|
66
|
-
);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* names an array of colors
|
|
71
|
-
* @param {array} colorArr array containing hex values without the hash
|
|
72
|
-
* @param {boolean} unique if set to true every returned name will be unque
|
|
73
|
-
* @param {boolean} bestOf if set only returns good names
|
|
74
|
-
* @return {object} object containing all nearest colors
|
|
75
|
-
*/
|
|
76
|
-
getNamesForValues(colorArr, unique = false, listKey = 'default') {
|
|
77
|
-
let localClosest = this.closestInstances[listKey];
|
|
78
|
-
|
|
79
|
-
if (unique) {
|
|
80
|
-
localClosest = new ClosestVector(
|
|
81
|
-
this.colorListsRGBArrays[listKey],
|
|
82
|
-
true
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
let lastResult = null;
|
|
87
|
-
|
|
88
|
-
const colorResp = colorArr.map((hex) => {
|
|
89
|
-
// calculate RGB values for passed color
|
|
90
|
-
const rgb = lib.hexToRgb(hex);
|
|
91
|
-
|
|
92
|
-
// get the closest named colors
|
|
93
|
-
let closestColor = localClosest.get([rgb.r, rgb.g, rgb.b]);
|
|
94
|
-
if (closestColor && unique) {
|
|
95
|
-
lastResult = closestColor;
|
|
96
|
-
} else if (unique) {
|
|
97
|
-
closestColor = lastResult;
|
|
98
|
-
}
|
|
99
|
-
const color = this.colorLists[listKey][closestColor.index];
|
|
100
|
-
|
|
101
|
-
return {
|
|
102
|
-
...color,
|
|
103
|
-
requestedHex: `#${hex}`,
|
|
104
|
-
distance: Math.sqrt(
|
|
105
|
-
Math.pow(color.rgb.r - rgb.r, 2) +
|
|
106
|
-
Math.pow(color.rgb.g - rgb.g, 2) +
|
|
107
|
-
Math.pow(color.rgb.b - rgb.b, 2)
|
|
108
|
-
),
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
if (unique) {
|
|
113
|
-
localClosest.clearCache();
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
return colorResp;
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
const seedrandom = require('seedrandom');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* getPaletteTitle
|
|
5
|
-
* @param {string[]} namesArr
|
|
6
|
-
* @param {int(0-1)} rnd1
|
|
7
|
-
* @param {int(0-1)} rnd2
|
|
8
|
-
* @param {int(0-1)} longPartFirst
|
|
9
|
-
* @param {RegExp} separatorRegex
|
|
10
|
-
* @returns {string}
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
module.exports = function getPaletteTitle(
|
|
14
|
-
namesArr, // array of names
|
|
15
|
-
separatorRegex = /(\s|-)+/g
|
|
16
|
-
) {
|
|
17
|
-
let localnames = [...namesArr];
|
|
18
|
-
|
|
19
|
-
const rng = seedrandom(namesArr.join('-'));
|
|
20
|
-
const rnd1 = rng();
|
|
21
|
-
const rnd2 = rng();
|
|
22
|
-
const longPartFirst = rng() < .5;
|
|
23
|
-
|
|
24
|
-
// select a random name from the list for the first word in the palette title
|
|
25
|
-
const indexFirst = Math.round(rnd1 * (localnames.length - 1));
|
|
26
|
-
|
|
27
|
-
// remove the selected name from the list
|
|
28
|
-
const firstName = localnames.splice(indexFirst, 1)[0];
|
|
29
|
-
|
|
30
|
-
// select a random name from the list as a last word in the palette title
|
|
31
|
-
const lastIndex = Math.round(rnd2 * (localnames.length - 1));
|
|
32
|
-
const lastName = localnames[lastIndex];
|
|
33
|
-
|
|
34
|
-
const partsFirst = firstName.split(separatorRegex);
|
|
35
|
-
const partsLast = lastName.split(separatorRegex);
|
|
36
|
-
|
|
37
|
-
if (longPartFirst) {
|
|
38
|
-
partsFirst.length > 1 ?
|
|
39
|
-
partsFirst.pop() :
|
|
40
|
-
partsFirst[0] = `${partsFirst[0]} `;
|
|
41
|
-
return partsFirst.join('') + partsLast.pop();
|
|
42
|
-
} else {
|
|
43
|
-
partsLast.length > 1 ?
|
|
44
|
-
partsLast.shift() :
|
|
45
|
-
partsLast[0] = ` ${partsLast[0]}`;
|
|
46
|
-
return partsFirst.shift() + partsLast.join('');
|
|
47
|
-
}
|
|
48
|
-
}
|