UNPKG

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