| 1 |
"use strict"; |
| 2 |
|
| 3 |
var push = Array.prototype.push |
| 4 |
, reduce = Array.prototype.reduce |
| 5 |
, abs = Math.abs |
| 6 |
, colors |
| 7 |
, match |
| 8 |
, result |
| 9 |
, i; |
| 10 |
|
| 11 |
colors = require("./xterm-colors").map(function (color) { |
| 12 |
return { |
| 13 |
r: parseInt(color.slice(0, 2), 16), |
| 14 |
g: parseInt(color.slice(2, 4), 16), |
| 15 |
b: parseInt(color.slice(4), 16) |
| 16 |
}; |
| 17 |
}); |
| 18 |
|
| 19 |
match = colors.slice(0, 16); |
| 20 |
|
| 21 |
module.exports = result = []; |
| 22 |
|
| 23 |
i = 0; |
| 24 |
while (i < 8) { |
| 25 |
result.push(30 + i++); |
| 26 |
} |
| 27 |
i = 0; |
| 28 |
while (i < 8) { |
| 29 |
result.push(90 + i++); |
| 30 |
} |
| 31 |
push.apply( |
| 32 |
result, |
| 33 |
colors.slice(16).map(function (data) { |
| 34 |
var index, diff = Infinity; |
| 35 |
match.every(function (innerMatch, currentIndex) { |
| 36 |
var ndiff = reduce.call( |
| 37 |
"rgb", |
| 38 |
function (currentDiff, channel) { |
| 39 |
currentDiff += abs(innerMatch[channel] - data[channel]); |
| 40 |
return currentDiff; |
| 41 |
}, |
| 42 |
0 |
| 43 |
); |
| 44 |
if (ndiff < diff) { |
| 45 |
index = currentIndex; |
| 46 |
diff = ndiff; |
| 47 |
} |
| 48 |
return ndiff; |
| 49 |
}); |
| 50 |
return result[index]; |
| 51 |
}) |
| 52 |
); |