Ignore:
Timestamp:
Apr 13, 2006, 6:54:25 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Fixed colour conversion routines so that they work with ARGB too.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/colour.c

    r753 r764  
    3030};
    3131
    32 /* FIXME: could this be inlined? */
     32static uint8_t nearest_ansi(uint16_t argb16, uint8_t def)
     33{
     34    unsigned int i, best, dist;
     35
     36    if(argb16 < CUCUL_COLOR_DEFAULT)
     37        return argb16;
     38
     39    if(argb16 == CUCUL_COLOR_DEFAULT || argb16 == CUCUL_COLOR_TRANSPARENT)
     40        return def;
     41
     42    if(argb16 < 0x5fff) /* too transparent, return default colour */
     43        return def;
     44
     45    dist = 0xffff;
     46    for(i = 0; i < 16; i++)
     47    {
     48        unsigned int d = 0;
     49        int a, b;
     50
     51        a = (ansitab[i] >> 8) & 0xf;
     52        b = (argb16 >> 8) & 0xf;
     53        d += (a - b) * (a - b);
     54
     55        a = (ansitab[i] >> 4) & 0xf;
     56        b = (argb16 >> 4) & 0xf;
     57        d += (a - b) * (a - b);
     58
     59        a = ansitab[i] & 0xf;
     60        b = argb16 & 0xf;
     61        d += (a - b) * (a - b);
     62
     63        if(d < dist)
     64        {
     65            dist = d;
     66            best = i;
     67        }
     68    }
     69
     70    return best;
     71}
    3372
    3473uint8_t _cucul_argb32_to_ansi8(uint32_t c)
    3574{
    36     /* FIXME: we need nearest colour handling for non-ANSI */
    37     return (c & 0x0000000f) | ((c & 0x000f0000) >> 12);
     75    uint16_t fg = c & 0xffff;
     76    uint16_t bg = c >> 16;
     77
     78    return nearest_ansi(fg, CUCUL_COLOR_LIGHTGRAY)
     79            | (nearest_ansi(bg, CUCUL_COLOR_BLACK) << 4);
    3880}
    3981
    4082uint8_t _cucul_argb32_to_ansi4fg(uint32_t c)
    4183{
    42     /* FIXME: we need nearest colour handling for non-ANSI */
    43     return c & 0x0000000f;
     84    return nearest_ansi(c & 0xffff, CUCUL_COLOR_LIGHTGRAY);
    4485}
    4586
    4687uint8_t _cucul_argb32_to_ansi4bg(uint32_t c)
    4788{
    48     /* FIXME: we need nearest colour handling for non-ANSI */
    49     return (c & 0x000f0000) >> 16;
     89    return nearest_ansi(c >> 16, CUCUL_COLOR_BLACK);
    5090}
    5191
     
    5595    uint16_t bg = c >> 16;
    5696
    57     if(fg < 0x0016)
     97    if(fg < CUCUL_COLOR_DEFAULT)
    5898        fg = ansitab[fg];
     99    else if(fg == CUCUL_COLOR_DEFAULT)
     100        fg = ansitab[CUCUL_COLOR_LIGHTGRAY];
     101    else if(fg == CUCUL_COLOR_TRANSPARENT)
     102        fg = 0x0fff;
    59103
    60     if(bg < 0x0016)
     104    if(bg < CUCUL_COLOR_DEFAULT)
    61105        bg = ansitab[bg];
     106    else if(bg == CUCUL_COLOR_DEFAULT)
     107        bg = ansitab[CUCUL_COLOR_BLACK];
     108    else if(bg == CUCUL_COLOR_TRANSPARENT)
     109        bg = 0x0fff;
    62110
    63111    argb[0] = bg >> 12;
Note: See TracChangeset for help on using the changeset viewer.