FreeBASIC  0.91.0
gfx_palette.c
Go to the documentation of this file.
1 /* palette management */
2 
3 #include "fb_gfx.h"
4 
5 static const unsigned char cga_association[5][4] = {
6  { 0, 11, 13, 15 }, { 0, 10, 12, 14 }, { 0, 3, 5, 7 }, { 0, 2, 4, 6 }, { 0, 15 }
7 };
8 static const unsigned char ega_association[2][16] = {
9  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
10  { 0, 1, 2, 3, 4, 5, 20, 7, 56, 57, 58, 59, 60, 61, 62, 63 }
11 };
12 
13 unsigned int fb_hMakeColor(int bpp, unsigned int index, int r, int g, int b)
14 {
15  unsigned int color;
16 
17  if (bpp == 2)
18  color = (b >> 3) | ((g << 3) & 0x07E0) | ((r << 8) & 0xF800);
19  else
20  color = index;
21 
22  return color;
23 }
24 
25 unsigned int fb_hFixColor(int bpp, unsigned int color)
26 {
27  return fb_hMakeColor(bpp, color, (color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF) & BPP_MASK(bpp);
28 }
29 
31 {
32  int i;
33 
35  return;
36 
37  for (i = 0; i < 256; i++) {
39  (__fb_gfx->device_palette[i] >> 8) & 0xFF,
40  (__fb_gfx->device_palette[i] >> 16) & 0xFF);
41  }
42 }
43 
44 void fb_hSetPaletteColorRgb(int index, int r, int g, int b)
45 {
46  index &= (__fb_gfx->default_palette->colors - 1);
47 
48  __fb_gfx->device_palette[index] = r | (g << 8) | (b << 16);
49 
51  __fb_gfx->driver->set_palette(index, r, g, b);
52 }
53 
54 void fb_hSetPaletteColor(int index, unsigned int color)
55 {
56  int r, g, b;
57 
58  index &= (__fb_gfx->default_palette->colors - 1);
59 
61  r = ((color & 0x3F) * 255.0) / 63.0;
62  g = (((color & 0x3F00) >> 8) * 255.0) / 63.0;
63  b = (((color & 0x3F0000) >> 16) * 255.0) / 63.0;
64  } else {
65  color &= (__fb_gfx->default_palette->colors - 1);
66  r = (__fb_gfx->palette[color] & 0xFF);
67  g = (__fb_gfx->palette[color] >> 8) & 0xFF;
68  b = (__fb_gfx->palette[color] >> 16) & 0xFF;
70  }
71 
72  fb_hSetPaletteColorRgb(index, r, g, b);
73 }
74 
75 FBCALL void fb_GfxPalette(int index, int red, int green, int blue)
76 {
77  int i, r, g, b;
78  const PALETTE *palette;
79  const unsigned char *mode_association;
80 
81  if ((!__fb_gfx) || (__fb_gfx->depth > 8))
82  return;
83 
84  DRIVER_LOCK();
85 
86  if (index < 0) {
87  palette = __fb_gfx->default_palette;
88  switch (__fb_gfx->mode_num) {
89  case 1:
90  index = MID(0, -(index + 1), 3);
91  mode_association = cga_association[index];
92  break;
93  case 2:
94  mode_association = cga_association[4];
95  break;
96  case 7:
97  case 8:
98  mode_association = ega_association[0];
99  break;
100  case 9:
101  mode_association = ega_association[1];
102  break;
103  default:
104  mode_association = NULL;
105  break;
106  }
107  for (i = 0; i < palette->colors; i++) {
108  r = palette->data[i * 3];
109  g = palette->data[(i * 3) + 1];
110  b = palette->data[(i * 3) + 2];
111  __fb_gfx->palette[i] = r | (g << 8) | (b << 16);
112  if (i < (1 << __fb_gfx->depth)) {
113  if (mode_association) {
114  __fb_gfx->color_association[i] = mode_association[i];
115  r = palette->data[__fb_gfx->color_association[i] * 3];
116  g = palette->data[(__fb_gfx->color_association[i] * 3) + 1];
117  b = palette->data[(__fb_gfx->color_association[i] * 3) + 2];
118  }
119  __fb_gfx->device_palette[i] = r | (g << 8) | (b << 16);
121  __fb_gfx->driver->set_palette(i, r, g, b);
122  }
123  }
124  } else {
125  if ((green < 0) || (blue < 0))
126  fb_hSetPaletteColor(index, (unsigned int)red);
127  else
128  fb_hSetPaletteColorRgb(index, red, green, blue);
129  }
130 
132 
133  DRIVER_UNLOCK();
134 }