summaryrefslogtreecommitdiffstats
path: root/clut.c
blob: ae1d7f4690a5b3e48592ee333a18e9ded77fb4ce (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
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
/*
 *  CLUT manipulation
 *
 *  (C) Copyright 2001-2002 Geert Uytterhoeven
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License. See the file COPYING in the main directory of this archive for
 *  more details.
 */

#include <string.h>

#include "types.h"
#include "visual.h"
#include "util.h"


    /*
     *  Monochrome
     */

const rgba_t clut_mono[2] = {
    { 0x0000, 0x0000, 0x0000, 0xffff },
    { 0xffff, 0xffff, 0xffff, 0xffff }
};


    /*
     *  Linux console palette
     */

const rgba_t clut_console[16] = {
    { 0x0000, 0x0000, 0x0000, 0xffff },
    { 0x0000, 0x0000, 0xaaaa, 0xffff },
    { 0x0000, 0xaaaa, 0x0000, 0xffff },
    { 0x0000, 0xaaaa, 0xaaaa, 0xffff },
    { 0xaaaa, 0x0000, 0x0000, 0xffff },
    { 0xaaaa, 0x0000, 0xaaaa, 0xffff },
    { 0xaaaa, 0x5555, 0x0000, 0xffff },
    { 0xaaaa, 0xaaaa, 0xaaaa, 0xffff },
    { 0x5555, 0x5555, 0x5555, 0xffff },
    { 0x5555, 0x5555, 0xffff, 0xffff },
    { 0x5555, 0xffff, 0x5555, 0xffff },
    { 0x5555, 0xffff, 0xffff, 0xffff },
    { 0xffff, 0x5555, 0x5555, 0xffff },
    { 0xffff, 0x5555, 0xffff, 0xffff },
    { 0xffff, 0xffff, 0x5555, 0xffff },
    { 0xffff, 0xffff, 0xffff, 0xffff }
};


    /*
     *  VGA palette
     */

const rgba_t clut_vga[16] = {
    { 0x0000, 0x0000, 0x0000, 0xffff },
    { 0x8080, 0x0000, 0x0000, 0xffff },
    { 0x0000, 0x8080, 0x0000, 0xffff },
    { 0x8080, 0x8080, 0x0000, 0xffff },
    { 0x0000, 0x0000, 0x8080, 0xffff },
    { 0x8080, 0x0000, 0x8080, 0xffff },
    { 0x0000, 0x8080, 0x8080, 0xffff },
    { 0xc0c0, 0xc0c0, 0xc0c0, 0xffff },
    { 0x8080, 0x8080, 0x8080, 0xffff },
    { 0xffff, 0x0000, 0x0000, 0xffff },
    { 0x0000, 0xffff, 0x0000, 0xffff },
    { 0xffff, 0xffff, 0x0000, 0xffff },
    { 0x0000, 0x0000, 0xffff, 0xffff },
    { 0xffff, 0x0000, 0xffff, 0xffff },
    { 0x0000, 0xffff, 0xffff, 0xffff },
    { 0xffff, 0xffff, 0xffff, 0xffff },
};


    /*
     *  4 Extra Windows colors
     */

const rgba_t clut_windows[4] = {
    { 0xc0c0, 0xdcdc, 0xc0c0, 0xffff },
    { 0xa6a6, 0xcaca, 0xf0f0, 0xffff },
    { 0xffff, 0xfbfb, 0xf0f0, 0xffff },
    { 0xa0a0, 0xa0a0, 0xa4a4, 0xffff },
};


    /*
     *  Create a color cube
     */

void clut_create_rgbcube(rgba_t *clut, u32 rlen, u32 glen, u32 blen)
{
    u32 r, g, b;
    u16 red, green, blue;

    Debug("clut_create_rgbcube(): RGB %dx%dx%d\n", rlen, glen, blen);
    for (r = 0; r < rlen; r++) {
	red = EXPAND_TO_16BIT(r, rlen-1);
	for (g = 0; g < glen; g++) {
	    green = EXPAND_TO_16BIT(g, glen-1);
	    for (b = 0; b < blen; b++) {
		blue = EXPAND_TO_16BIT(b, blen-1);
		clut->r = red;
		clut->g = green;
		clut->b = blue;
		clut->a = 0xffff;
		clut++;
	    }
	}
    }
}


    /*
     *  Create a linear ramp
     */

void clut_create_linear(rgba_t *clut, u32 len)
{
    u32 i;
    u16 gray;

    Debug("clut_create_linear(): GRAY %d\n", len);
    for (i = 0; i < len; i++) {
	gray = EXPAND_TO_16BIT(i, len-1);
	clut->r = gray;
	clut->g = gray;
	clut->b = gray;
	clut->a = 0xffff;
	clut++;
    }
}


    /*
     *  Create a `useful' palette
     */

void clut_init_nice(void)
{
    if (idx_len >= 512) {
	clut_create_rgbcube(clut, 8, 8, 8);
    } else if (idx_len >= 256) {
	memcpy(clut, clut_console, sizeof(clut_console));
	/* FIXME: skip colors that are already present */
	clut_create_rgbcube(clut+16, 6, 6, 6);
	/* FIXME: skip colors that are already present */
	clut_create_linear(clut+232, 24);
    } else if (idx_len >= 128) {
	memcpy(clut, clut_console, sizeof(clut_console));
	/* FIXME: skip colors that are already present */
	clut_create_rgbcube(clut+16, 4, 4, 4);
	/* FIXME: skip colors that are already present */
	clut_create_linear(clut+80, 48);
    } else if (idx_len >= 64) {
	clut_create_rgbcube(clut, 4, 4, 4);
    } else if (idx_len >= 32) {
	clut_create_rgbcube(clut, 3, 3, 3);
	/* FIXME: still 3 entries left */
    } else if (idx_len >= 16) {
	memcpy(clut, clut_console, sizeof(clut_console));
    } else if (idx_len >= 8) {
	memcpy(clut, clut_console, 8*sizeof(rgba_t));
    } else if (idx_len >= 4) {
	clut_create_linear(clut, 4);
    } else if (idx_len >= 2) {
	memcpy(clut, clut_mono, sizeof(clut_mono));
    }

    clut_update();
}