summaryrefslogtreecommitdiff
path: root/src/ui/color.c
blob: 179006c1cde23c70ec1fde309ef81f8481464c91 (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
#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 },
        { 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;
}

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;
}