-
Notifications
You must be signed in to change notification settings - Fork 1
/
color.js
156 lines (126 loc) · 4.29 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const InvalidArgumentException = require('@ostro/support/exceptions/invalidArgumentException')
class Color {
static COLORS = {
'black': 0,
'red': 1,
'green': 2,
'yellow': 3,
'blue': 4,
'magenta': 5,
'cyan': 6,
'white': 7,
'default': 9,
};
static BRIGHT_COLORS = {
'gray': 0,
'bright-red': 1,
'bright-green': 2,
'bright-yellow': 3,
'bright-blue': 4,
'bright-magenta': 5,
'bright-cyan': 6,
'bright-white': 7,
};
static AVAILABLE_OPTIONS = {
'bold': { 'set': 1, 'unset': 22 },
'underscore': { 'set': 4, 'unset': 24 },
'blink': { 'set': 5, 'unset': 25 },
'reverse': { 'set': 7, 'unset': 27 },
'conceal': { 'set': 8, 'unset': 28 },
};
$options = {};
constructor($foreground = '', $background = '', $options = []) {
this.$foreground = this.parseColor($foreground);
this.$background = this.parseColor($background, true);
for (let $option of $options) {
if (!isset(this.constructor.AVAILABLE_OPTIONS[$option])) {
throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s).', $option, implode(', ', Object.keys(this.constructor.AVAILABLE_OPTIONS))));
}
this.$options[$option] = this.constructor.AVAILABLE_OPTIONS[$option];
}
}
apply($text) {
return this.set() + $text + this.unset();
}
set() {
let $setCodes = [];
if ('' !== this.$foreground) {
$setCodes.push(this.$foreground);
}
if ('' !== this.$background) {
$setCodes.push(this.$background);
}
for (let $option in this.$options) {
$setCodes.push($option['set']);
}
if (0 === count($setCodes)) {
return '';
}
return sprintf("\x1b[%sm", $setCodes.join(';'));
}
unset() {
let $unsetCodes = [];
if ('' !== this.$foreground) {
$unsetCodes.push(39);
}
if ('' !== this.$background) {
$unsetCodes.push(49);
}
for (let $option in this.$options) {
$unsetCodes.push($option['unset']);
}
if (0 === count($unsetCodes)) {
return '';
}
return sprintf("\x1b[%sm", $unsetCodes.join(';'));
}
parseColor($color, $background = false) {
if ('' === $color) {
return '';
}
if ('#' === $color[0]) {
$color = substr($color, 1);
if (3 === strlen($color)) {
$color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2];
}
if (6 !== strlen($color)) {
throw new InvalidArgumentException(sprintf('Invalid "%s" color.', $color));
}
return ($background ? '4' : '3') + this.convertHexColorToAnsi(hexdec($color));
}
if (isset(this.constructor.COLORS[$color])) {
return ($background ? '4' : '3') + this.constructor.COLORS[$color];
}
if (isset(this.constructor.BRIGHT_COLORS[$color])) {
return ($background ? '10' : '9') + this.constructor.BRIGHT_COLORS[$color];
}
throw new InvalidArgumentException(sprintf('Invalid "%s" color; expected one of (%s).', $color, Object.keys(this.constructor.COLORS).concat(Object.keys(this.constructor.BRIGHT_COLORS)).join(',')));
}
convertHexColorToAnsi($color) {
let $r = ($color >> 16) & 255;
let $g = ($color >> 8) & 255;
let $b = $color & 255;
if ('truecolor' !== env('COLORTERM')) {
return this.degradeHexColorToAnsi($r, $g, $b);
}
return sprintf('8;2;%d;%d;%d', $r, $g, $b);
}
degradeHexColorToAnsi($r, $g, $b) {
if (0 === round(this.getSaturation($r, $g, $b) / 50)) {
return 0;
}
return (round($b / 255) << 2) | (round($g / 255) << 1) | round($r / 255);
}
getSaturation($r, $g, $b) {
$r = $r / 255;
$g = $g / 255;
$b = $b / 255;
$v = max($r, $g, $b);
let $diff = $v - min($r, $g, $b)
if (0 === $diff) {
return 0;
}
return $diff * 100 / $v;
}
}
module.exports = Color