-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.js
229 lines (206 loc) · 7.1 KB
/
colors.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
colors.js
Copyright (c) 2010 Alexis Sellier (cloudhead) , Marak Squires
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
exports.mode = "console";
// prototypes the string object to have additional method calls that add terminal colors
var addProperty = function (color, func) {
exports[color] = function(str) {
return func.apply(str);
};
// __defineGetter__ at the least works in more browsers
// http://robertnyman.com/javascript/javascript-getters-setters.html
// Object.defineProperty only works in Chrome
String.prototype.__defineGetter__(color, func);
};
var isHeadless = (typeof module !== 'undefined');
['bold', 'underline', 'italic', 'inverse', 'grey', 'black', 'yellow', 'red', 'green', 'blue', 'white', 'cyan', 'magenta'].forEach(function (style) {
addProperty(style, function () {
return isHeadless ?
stylize(this, style) : // for those running in node (headless environments)
this.replace(/( )/, '$1'); // and for those running in browsers:
// re: ^ you'd think 'return this' works (but doesn't) so replace coerces the string to be a real string
});
});
// prototypes string with method "rainbow"
// rainbow will apply a the color spectrum to a string, changing colors every letter
addProperty('rainbow', function () {
if (!isHeadless) {
return this.replace(/( )/, '$1');
}
var rainbowcolors = ['red','yellow','green','blue','magenta']; //RoY G BiV
var exploded = this.split("");
var i=0;
exploded = exploded.map(function(letter) {
if (letter==" ") {
return letter;
}
else {
return stylize(letter,rainbowcolors[i++ % rainbowcolors.length]);
}
});
return exploded.join("");
});
function stylize(str, style) {
if (exports.mode == 'console') {
var styles = {
//styles
'bold' : ['\033[1m', '\033[22m'],
'italic' : ['\033[3m', '\033[23m'],
'underline' : ['\033[4m', '\033[24m'],
'inverse' : ['\033[7m', '\033[27m'],
//grayscale
'white' : ['\033[37m', '\033[39m'],
'grey' : ['\033[90m', '\033[39m'],
'black' : ['\033[30m', '\033[39m'],
//colors
'blue' : ['\033[34m', '\033[39m'],
'cyan' : ['\033[36m', '\033[39m'],
'green' : ['\033[32m', '\033[39m'],
'magenta' : ['\033[35m', '\033[39m'],
'red' : ['\033[31m', '\033[39m'],
'yellow' : ['\033[33m', '\033[39m']
};
} else if (exports.mode == 'browser') {
var styles = {
//styles
'bold' : ['<b>', '</b>'],
'italic' : ['<i>', '</i>'],
'underline' : ['<u>', '</u>'],
'inverse' : ['<span style="background-color:black;color:white;">', '</span>'],
//grayscale
'white' : ['<span style="color:white;">', '</span>'],
'grey' : ['<span style="color:grey;">', '</span>'],
'black' : ['<span style="color:black;">', '</span>'],
//colors
'blue' : ['<span style="color:blue;">', '</span>'],
'cyan' : ['<span style="color:cyan;">', '</span>'],
'green' : ['<span style="color:green;">', '</span>'],
'magenta' : ['<span style="color:magenta;">', '</span>'],
'red' : ['<span style="color:red;">', '</span>'],
'yellow' : ['<span style="color:yellow;">', '</span>']
};
} else {
console.log('unsupported mode, try "browser" or "console"');
}
return styles[style][0] + str + styles[style][1];
};
// don't summon zalgo
addProperty('zalgo', function () {
return zalgo(this);
});
// please no
function zalgo(text, options) {
var soul = {
"up" : [
'̍','̎','̄','̅',
'̿','̑','̆','̐',
'͒','͗','͑','̇',
'̈','̊','͂','̓',
'̈','͊','͋','͌',
'̃','̂','̌','͐',
'̀','́','̋','̏',
'̒','̓','̔','̽',
'̉','ͣ','ͤ','ͥ',
'ͦ','ͧ','ͨ','ͩ',
'ͪ','ͫ','ͬ','ͭ',
'ͮ','ͯ','̾','͛',
'͆','̚'
],
"down" : [
'̖','̗','̘','̙',
'̜','̝','̞','̟',
'̠','̤','̥','̦',
'̩','̪','̫','̬',
'̭','̮','̯','̰',
'̱','̲','̳','̹',
'̺','̻','̼','ͅ',
'͇','͈','͉','͍',
'͎','͓','͔','͕',
'͖','͙','͚','̣'
],
"mid" : [
'̕','̛','̀','́',
'͘','̡','̢','̧',
'̨','̴','̵','̶',
'͜','͝','͞',
'͟','͠','͢','̸',
'̷','͡',' ҉'
]
},
all = [].concat(soul.up, soul.down, soul.mid),
zalgo = {};
function randomNumber(range) {
r = Math.floor(Math.random()*range);
return r;
};
function is_char(character) {
var bool = false;
all.filter(function(i){
bool = (i == character);
});
return bool;
}
function heComes(text, options){
result = '';
options = options || {};
options["up"] = options["up"] || true;
options["mid"] = options["mid"] || true;
options["down"] = options["down"] || true;
options["size"] = options["size"] || "maxi";
var counts;
text = text.split('');
for(var l in text){
if(is_char(l)) { continue; }
result = result + text[l];
counts = {"up" : 0, "down" : 0, "mid" : 0};
switch(options.size) {
case 'mini':
counts.up = randomNumber(8);
counts.min= randomNumber(2);
counts.down = randomNumber(8);
break;
case 'maxi':
counts.up = randomNumber(16) + 3;
counts.min = randomNumber(4) + 1;
counts.down = randomNumber(64) + 3;
break;
default:
counts.up = randomNumber(8) + 1;
counts.mid = randomNumber(6) / 2;
counts.down= randomNumber(8) + 1;
break;
}
var arr = ["up", "mid", "down"];
for(var d in arr){
var index = arr[d];
for (var i = 0 ; i <= counts[index]; i++)
{
if(options[index]) {
result = result + soul[index][randomNumber(soul[index].length)];
}
}
}
}
return result;
};
return heComes(text);
}
addProperty('stripColors', function() {
return ("" + this).replace(/\u001b\[\d+m/g,'');
});