UNPKG

828 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 return (diff += abs(match[channel] - data[channel]));
31 }, 0);
32 if (ndiff < diff) {
33 index = i;
34 diff = ndiff;
35 }
36 return ndiff;
37 });
38 return result[index];
39}));