-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.js
65 lines (49 loc) · 1.43 KB
/
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
/*
* Generate a color code.
* inputs:
* text string
* fgc foreground color int
* bgc background color int
* properties string [normal | bold | faint | underline | blink]
*
* The color int is a number 0-7 and maps to the array below.
* [Black, Red, Green, Yellow, Blue, Magenta, Cyan, White]
*
* Returns ansi escaped color coded text
*/
function colorcode(text, fgc, bgc, properties) {
// property map: name -> ansi property id
var prop = {
"normal": 0,
"bold": 1,
"faint":2,
"underline":4,
"blink": 5
};
//TODO: add properties by linking them to the checkboxes
// Take the property parameter and turn it into ansi codes joined by ;
var specialprops = properties.map(function(e) {
return prop[e];
}).join(";");
// If no properties, add a ; delimiter
if (specialprops !== "") specialprops = ";" + specialprops;
// set the foreground color
var fg = '3'+fgc;
// set the bg color if it exists
var bg = (bgc === "") ? "" : ";" + '4' + bgc;
// write the ansi color esacape header
var esc = "\\033";
var colorings = esc + "[" + fg + bg + specialprops + 'm';
// join the header, text and reset escape into the color text string
var reset = esc + "[0m";
var color_text = colorings + text + reset;
return color_text;
}
/*
* A helper for the colorcode function.
*
*/
function color(text, fg, bg, opts) {
if (!opts) opts = [];
return colorcode(text, fg, bg, opts);
}