about summary refs log tree commit diff stats
path: root/baremetal/vga_palette.c
blob: 9f4161ce267c0907f13e3fb8f0fa33cacae5f1c6 (plain) (blame)
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
/* Visualize the standard VGA palette.
 * Based on https://github.com/canidlogic/vgapal (MIT License) */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

char* bar(int n) {
  char* result = calloc(1024, 32);
  int i;
  strcat(result, "<div style='float:left; width:40em'>");
  for (i = 0; i < n; ++i)
//?     result[i] = '.';
    strcat(result, "&#9632;");  /* black square */
//?   for (; i < 64; ++i)
//?     result[i] = ' ';
//?   result[64] = '\0';
  strcat(result, "&nbsp;</div>");  /* make sure the div occupies space */
  return result;
}

/* convert 6-bit color to 8-bit color */
int levelUp(int n) {
  assert(n < 64);
  /* duplicate two most significant bits in two least significant bits */
  return (n<<2) | (n>>4);
}

void addColor(int r, int g, int b) {
  static int i = 0;
//?   printf("%02x %02x %02x\n", r, g, b);
//?   printf("%3d: %2d %2d %2d %s %s %s\n", i, r, g, b, bar(r), bar(g), bar(b));
  printf("<div style='clear:both; white-space:pre; color:#%02x%02x%02x'><div style='float:left; margin-right:1em'>%03d: %02d %02d %02d</div> %s %s %s</div>\n", levelUp(r), levelUp(g), levelUp(b), i, r, g, b, bar(r), bar(g), bar(b));
  ++i;
}

void addGrayColor(int v) {
  addColor(v, v, v);
}

void add16Color(int lo, int melo, int mehi, int hi) {
  int r, g, b, i, h, l;

  for (i = 0; i < 8; i++) {
    r = g = b = lo;
    if (i & 4) r = mehi;
    if (i & 2) g = (i==6)?melo:mehi;  /* exception: color 6 is brown rather than dark yellow */
    if (i & 1) b = mehi;
    addColor(r, g, b);
  }

  for (i = 0; i < 8; i++) {
    r = g = b = melo;
    if (i & 4) r = hi;
    if (i & 2) g = hi;
    if (i & 1) b = hi;
    addColor(r, g, b);
  }
}

/* Add four colors to the palette corresponding to a "run" within an RGB cycle.
 *
 * start - high and low starting states for each channel at the start of the run
 * ch - the channel to change from high to low or low to high */
void addRun(int start, int ch, int lo, int melo, int me, int mehi, int hi) {
  int r, g, b, i, up;

  /* Check parameters */
  if (start < 0 || start > 7)
    abort();
  if (ch != 1 && ch != 2 && ch != 4)
    abort();

  /* Get the starting RGB color and add it */
  r = lo;
  g = lo;
  b = lo;
  if ((start & 4) == 4)
    r = hi;
  if ((start & 2) == 2)
    g = hi;
  if ((start & 1) == 1)
    b = hi;
  addColor(r, g, b);

  /* If selected channel starts high, we're going down; otherwise we're going up */
  up = (start & ch) != ch;

  /* Add remaining three colors of the run */
  switch (ch) {
  case 4:  r = up?melo:mehi; break;
  case 2:  g = up?melo:mehi; break;
  case 1:  b = up?melo:mehi; break;
  }
  addColor(r, g, b);

  switch (ch) {
  case 4:  r = me; break;
  case 2:  g = me; break;
  case 1:  b = me; break;
  }
  addColor(r, g, b);

  switch (ch) {
  case 4:  r = up?mehi:melo; break;
  case 2:  g = up?mehi:melo; break;
  case 1:  b = up?mehi:melo; break;
  }
  addColor(r, g, b);
}

/* A cycle consists of six 4-color runs, each of which transitions from
 * one hue to another until arriving back at starting position. */
void addCycle(int lo, int melo, int me, int mehi, int hi) {
  int hue = 1;  /* blue */
  addRun(hue, 4, lo, melo, me, mehi, hi);
  hue ^= 4;
  assert(hue == 5);
  addRun(hue, 1, lo, melo, me, mehi, hi);
  hue ^= 1;
  assert(hue == 4);
  addRun(hue, 2, lo, melo, me, mehi, hi);
  hue ^= 2;
  assert(hue == 6);
  addRun(hue, 4, lo, melo, me, mehi, hi);
  hue ^= 4;
  assert(hue == 2);
  addRun(hue, 1, lo, melo, me, mehi, hi);
  hue ^= 1;
  assert(hue == 3);
  addRun(hue, 2, lo, melo, me, mehi, hi);
}

int main(void) {
  int i;

  /* 16-color palette */
  add16Color(0, 21, 42, 63);

  /* 16 shades of gray */
  addGrayColor( 0);
  addGrayColor( 5);
  addGrayColor( 8);
  addGrayColor(11);
  addGrayColor(14);
  addGrayColor(17);
  addGrayColor(20);
  addGrayColor(24);
  addGrayColor(28);
  addGrayColor(32);
  addGrayColor(36);
  addGrayColor(40);
  addGrayColor(45);
  addGrayColor(50);
  addGrayColor(56);
  addGrayColor(63);

  /* Nine RGB cycles organized in three groups of three cycles,
   * The groups represent high/medium/low value,
   * and the cycles within the groups represent high/medium/low saturation. */
  addCycle( 0, 16, 31, 47, 63);
  addCycle(31, 39, 47, 55, 63);
  addCycle(45, 49, 54, 58, 63);

  addCycle( 0,  7, 14, 21, 28);
  addCycle(14, 17, 21, 24, 28);
  addCycle(20, 22, 24, 26, 28);

  addCycle( 0,  4,  8, 12, 16);
  addCycle( 8, 10, 12, 14, 16);
  addCycle(11, 12, 13, 15, 16);

  /* final eight palette entries are full black */
  for (i = 0; i < 8; ++i)
    addGrayColor(0);

  return 0;
}