summaryrefslogtreecommitdiff
path: root/src/ui/color.c
blob: 46d8a71f6336c7b2f568ee2fb416b84687d2c119 (plain)
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
#include "color.h"

#include <the_Foundation/string.h>

static const iColor transparent_;

iColor get_Color(int color) {
    static const iColor palette[] = {
        { 0,   0,   0,   255 },
        { 24,  24,  24,  255 },
        { 40,  40,  40,  255 },
        { 80,  80,  80,  255 },
        { 160, 160, 160, 255 },
        { 208, 208, 208, 255 },
        { 255, 255, 255, 255 },
        { 106, 80,  0,   255 },
        { 255, 192, 0,   255 },
        { 0,   96,  128, 255 },
        { 0,   192, 255, 255 },
        { 255, 255, 32,  255 },
        { 255, 64,  64,  255 },
        { 255, 0,   255, 255 },
        { 132, 132, 255, 255 },
        { 0,   200, 0,   255 },
    };
    const iColor *clr = &transparent_;
    if (color >= 0 && color < (int) iElemCount(palette)) {
        clr = &palette[color];
    }
    return *clr;
}

const char *escape_Color(int color) {
    static const char *esc[] = {
        black_ColorEscape,
        gray15_ColorEscape,
        gray25_ColorEscape,
        gray50_ColorEscape,
        gray75_ColorEscape,
        gray88_ColorEscape,
        white_ColorEscape,
        brown_ColorEscape,
        orange_ColorEscape,
        teal_ColorEscape,
        cyan_ColorEscape,
        yellow_ColorEscape,
        red_ColorEscape,
        magenta_ColorEscape,
        blue_ColorEscape,
        green_ColorEscape,
    };
    if (color >= 0 && color < max_ColorId) {
        return esc[color];
    }
    return white_ColorEscape;
}

iColor ansi_Color(iRangecc escapeSequence, int fallback) {
    iColor clr = get_Color(fallback);
    for (const char *ch = escapeSequence.start; ch < escapeSequence.end; ch++) {
        char *endPtr;
        unsigned long arg = strtoul(ch, &endPtr, 10);
        ch = endPtr;
        switch (arg) {
            default:
                break;
            case 30:
                clr = (iColor){ 0, 0, 0, 255 };
                break;
            case 31:
                clr = (iColor){ 170, 0, 0, 255 };
                break;
            case 32:
                clr = (iColor){ 0, 170, 0, 255 };
                break;
            case 33:
                clr = (iColor){ 170, 85, 0, 255 };
                break;
            case 34:
                clr = (iColor){ 0, 0, 170, 255 };
                break;
            case 35:
                clr = (iColor){ 170, 0, 170, 255 };
                break;
            case 36:
                clr = (iColor){ 0, 170, 170, 255 };
                break;
            case 37:
                clr = (iColor){ 170, 170, 170, 255 };
                break;
            case 90:
                clr = (iColor){ 85, 85, 85, 255 };
                break;
            case 91:
                clr = (iColor){ 255, 85, 85, 255 };
                break;
            case 92:
                clr = (iColor){ 85, 255, 85, 255 };
                break;
            case 93:
                clr = (iColor){ 255, 255, 85, 255 };
                break;
            case 94:
                clr = (iColor){ 85, 85, 255, 255 };
                break;
            case 95:
                clr = (iColor){ 255, 85, 255, 255 };
                break;
            case 96:
                clr = (iColor){ 85, 255, 255, 255 };
                break;
            case 97:
                clr = (iColor){ 255, 255, 255, 255 };
                break;
        }
    }
    return clr;
}