/
find-color.js
74 lines (66 loc) · 1.54 KB
/
find-color.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* find-color
* Copyright(c) 2016 Mihai Potra <mike@mpotra.com>
* MIT Licensed
*/
/**
* Find a RGB tuple inside a list of colors.
*
* Computes the distance between the given RGB color
* expressed as a 3D point, and each of the colors in the list.
* Returns the color in the list that is closest.
*
*/
const findNearestRGB = (find, colors) => {
var nearest
if (!find) {
return find
}
// For each color in the list, compute the distance to the color
// that needs matching.
// Formula: sqrt((x1 - x2)^2 + (y1 - y2)^2 + (z1 - z2)^2)
nearest = colors
.map(color =>
Math.sqrt(
Math.pow(find[0] - color[0], 2) +
Math.pow(find[1] - color[1], 2) +
Math.pow(find[2] - color[2], 2)
)
)
// Find the smallest distance.
.reduce(
(prev, curr, index) => {
if ((prev && prev.distance > curr) || !prev) {
return {distance: curr, index: index}
} else {
return prev
}
},
{distance: Infinity, index: -1}
)
return nearest.index >= 0 ? colors[nearest.index] : undefined
}
/**
* Find a color in a list of RGB tuple colors.
*/
const find = (color, colors) => {
return findNearestRGB(color, colors)
}
/**
* Create an object that allows finding a color,
* from an array of RGB tuples.
*/
const fromRGB = arrRGB => {
return {find: color => find(color, arrRGB)}
}
/**
* Create an object that allows finding a color,
* from an array of colors given in HEX.
*/
const fromHex = arrHex => {
return fromRGB(arrHex.map(color => decodeHex(color)))
}
/**
* Export
*/
module.exports = {fromHex, fromRGB, find}