-
Notifications
You must be signed in to change notification settings - Fork 2
/
ansi256css.js
49 lines (44 loc) · 1.51 KB
/
ansi256css.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
// Generated by CoffeeScript 1.7.1
(function() {
var cssToAnsi, fromHex, __doc__;
__doc__ = "A module that returns an ANSI color code given either a CSS color name, a hex string,\nor an ANSI color code as a number\n";
cssToAnsi = require('./css-to-ansi');
fromHex = function(hex) {
"Maps a 6 digit hex string describing a CSS color onto the ANSI 256 color palette";
var blue, green, red, x, _ref;
_ref = (function() {
var _i, _len, _ref, _results;
_ref = [hex.slice(0, 2), hex.slice(2, 4), hex.slice(4, 6)];
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
x = _ref[_i];
_results.push(Math.round(parseInt(x, 16) / 255 * 5));
}
return _results;
})(), red = _ref[0], green = _ref[1], blue = _ref[2];
return 16 + red * 36 + green * 6 + blue;
};
module.exports = function(code) {
"Returns an ANSI color number given an ANSI color number, CSS color name, or CSS hex color";
switch (typeof code) {
case 'number':
return code;
case 'string':
if (/^#?(?:[0-9a-f]{3}){1,2}$/i.test(code)) {
if (code[0] === '#') {
code = code.slice(1);
}
if (code.length < 6) {
return fromHex(code[0] + code[0] + code[1] + code[1] + code[2] + code[2]);
} else {
return fromHex(code);
}
} else {
return cssToAnsi[code.toLowerCase()];
}
break;
default:
return void 0;
}
};
}).call(this);