Simplify color generation code

This commit is contained in:
Daniel Imms
2017-08-31 21:18:55 -07:00
parent 1998675dc0
commit c88824155f
3 changed files with 46 additions and 28 deletions
+40 -24
View File
@@ -1,6 +1,25 @@
// TODO: Ideally colors would be exposed through some theme manager since colors
// are moving to JS.
export enum COLOR_CODES {
BLACK = 0,
RED = 1,
GREEN = 2,
YELLOW = 3,
BLUE = 4,
MAGENTA = 5,
CYAN = 6,
WHITE = 7,
BRIGHT_BLACK = 8,
BRIGHT_RED = 9,
BRIGHT_GREEN = 10,
BRIGHT_YELLOW = 11,
BRIGHT_BLUE = 12,
BRIGHT_MAGENTA = 13,
BRIGHT_CYAN = 14,
BRIGHT_WHITE = 15
}
const TANGO_COLORS = [
// dark:
'#2e3436',
@@ -22,33 +41,30 @@ const TANGO_COLORS = [
'#eeeeec'
];
export const COLORS: string[] = (function(): string[] {
let colors = TANGO_COLORS.slice();
let r = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff];
let i;
export const COLORS: string[] = generate256Colors(TANGO_COLORS);
// 16-231
i = 0;
for (; i < 216; i++) {
out(r[(i / 36) % 6 | 0], r[(i / 6) % 6 | 0], r[i % 6]);
function generate256Colors(first16Colors: string[]): string[] {
let colors = first16Colors.slice();
// Generate colors (16-231)
let v = [0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff];
for (let i = 0; i < 216; i++) {
const r = toPaddedHex(v[(i / 36) % 6 | 0]);
const g = toPaddedHex(v[(i / 6) % 6 | 0]);
const b = toPaddedHex(v[i % 6]);
colors.push(`#${r}${g}${b}`);
}
// 232-255 (grey)
i = 0;
let c: number;
for (; i < 24; i++) {
c = 8 + i * 10;
out(c, c, c);
}
function out(r: number, g: number, b: number): void {
colors.push('#' + hex(r) + hex(g) + hex(b));
}
function hex(c: number): string {
let s = c.toString(16);
return s.length < 2 ? '0' + s : s;
// Generate greys (232-255)
for (let i = 0; i < 24; i++) {
const c = toPaddedHex(8 + i * 10);
colors.push(`#${c}${c}${c}`);
}
return colors;
})();
}
function toPaddedHex(c: number): string {
let s = c.toString(16);
return s.length < 2 ? '0' + s : s;
}