Browse Source

* Fixed colour conversion routines so that they work with ARGB too.

tags/v0.99.beta14
Sam Hocevar sam 19 years ago
parent
commit
b355e0581d
1 changed files with 57 additions and 9 deletions
  1. +57
    -9
      cucul/colour.c

+ 57
- 9
cucul/colour.c View File

@@ -29,24 +29,64 @@ static const uint16_t ansitab[16] =
0xf444, 0xf44f, 0xf4f4, 0xf4ff, 0xff44, 0xff4f, 0xfff4, 0xffff,
};

/* FIXME: could this be inlined? */
static uint8_t nearest_ansi(uint16_t argb16, uint8_t def)
{
unsigned int i, best, dist;

if(argb16 < CUCUL_COLOR_DEFAULT)
return argb16;

if(argb16 == CUCUL_COLOR_DEFAULT || argb16 == CUCUL_COLOR_TRANSPARENT)
return def;

if(argb16 < 0x5fff) /* too transparent, return default colour */
return def;

dist = 0xffff;
for(i = 0; i < 16; i++)
{
unsigned int d = 0;
int a, b;

a = (ansitab[i] >> 8) & 0xf;
b = (argb16 >> 8) & 0xf;
d += (a - b) * (a - b);

a = (ansitab[i] >> 4) & 0xf;
b = (argb16 >> 4) & 0xf;
d += (a - b) * (a - b);

a = ansitab[i] & 0xf;
b = argb16 & 0xf;
d += (a - b) * (a - b);

if(d < dist)
{
dist = d;
best = i;
}
}

return best;
}

uint8_t _cucul_argb32_to_ansi8(uint32_t c)
{
/* FIXME: we need nearest colour handling for non-ANSI */
return (c & 0x0000000f) | ((c & 0x000f0000) >> 12);
uint16_t fg = c & 0xffff;
uint16_t bg = c >> 16;

return nearest_ansi(fg, CUCUL_COLOR_LIGHTGRAY)
| (nearest_ansi(bg, CUCUL_COLOR_BLACK) << 4);
}

uint8_t _cucul_argb32_to_ansi4fg(uint32_t c)
{
/* FIXME: we need nearest colour handling for non-ANSI */
return c & 0x0000000f;
return nearest_ansi(c & 0xffff, CUCUL_COLOR_LIGHTGRAY);
}

uint8_t _cucul_argb32_to_ansi4bg(uint32_t c)
{
/* FIXME: we need nearest colour handling for non-ANSI */
return (c & 0x000f0000) >> 16;
return nearest_ansi(c >> 16, CUCUL_COLOR_BLACK);
}

void _cucul_argb32_to_argb4(uint32_t c, uint8_t argb[8])
@@ -54,11 +94,19 @@ void _cucul_argb32_to_argb4(uint32_t c, uint8_t argb[8])
uint16_t fg = c & 0xffff;
uint16_t bg = c >> 16;

if(fg < 0x0016)
if(fg < CUCUL_COLOR_DEFAULT)
fg = ansitab[fg];
else if(fg == CUCUL_COLOR_DEFAULT)
fg = ansitab[CUCUL_COLOR_LIGHTGRAY];
else if(fg == CUCUL_COLOR_TRANSPARENT)
fg = 0x0fff;

if(bg < 0x0016)
if(bg < CUCUL_COLOR_DEFAULT)
bg = ansitab[bg];
else if(bg == CUCUL_COLOR_DEFAULT)
bg = ansitab[CUCUL_COLOR_BLACK];
else if(bg == CUCUL_COLOR_TRANSPARENT)
bg = 0x0fff;

argb[0] = bg >> 12;
argb[1] = (bg >> 8) & 0xf;


Loading…
Cancel
Save