source: libcaca/trunk/cucul/colour.c @ 765

Last change on this file since 765 was 765, checked in by Sam Hocevar, 15 years ago
  • Fixed compilation warning.
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1/*
2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file colour.c
13 *  \version \$Id: colour.c 765 2006-04-13 17:08:16Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Colour handling
16 *
17 *  This file contains functions for converting colour values between
18 *  various colourspaces.
19 */
20
21#include "config.h"
22
23#include "cucul.h"
24#include "cucul_internals.h"
25
26static const uint16_t ansitab[16] =
27{
28    0xf000, 0xf008, 0xf080, 0xf088, 0xf800, 0xf808, 0xf880, 0xf888,
29    0xf444, 0xf44f, 0xf4f4, 0xf4ff, 0xff44, 0xff4f, 0xfff4, 0xffff,
30};
31
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    best = def;
46    dist = 0xffff;
47    for(i = 0; i < 16; i++)
48    {
49        unsigned int d = 0;
50        int a, b;
51
52        a = (ansitab[i] >> 8) & 0xf;
53        b = (argb16 >> 8) & 0xf;
54        d += (a - b) * (a - b);
55
56        a = (ansitab[i] >> 4) & 0xf;
57        b = (argb16 >> 4) & 0xf;
58        d += (a - b) * (a - b);
59
60        a = ansitab[i] & 0xf;
61        b = argb16 & 0xf;
62        d += (a - b) * (a - b);
63
64        if(d < dist)
65        {
66            dist = d;
67            best = i;
68        }
69    }
70
71    return best;
72}
73
74uint8_t _cucul_argb32_to_ansi8(uint32_t c)
75{
76    uint16_t fg = c & 0xffff;
77    uint16_t bg = c >> 16;
78
79    return nearest_ansi(fg, CUCUL_COLOR_LIGHTGRAY)
80            | (nearest_ansi(bg, CUCUL_COLOR_BLACK) << 4);
81}
82
83uint8_t _cucul_argb32_to_ansi4fg(uint32_t c)
84{
85    return nearest_ansi(c & 0xffff, CUCUL_COLOR_LIGHTGRAY);
86}
87
88uint8_t _cucul_argb32_to_ansi4bg(uint32_t c)
89{
90    return nearest_ansi(c >> 16, CUCUL_COLOR_BLACK);
91}
92
93void _cucul_argb32_to_argb4(uint32_t c, uint8_t argb[8])
94{
95    uint16_t fg = c & 0xffff;
96    uint16_t bg = c >> 16;
97
98    if(fg < CUCUL_COLOR_DEFAULT)
99        fg = ansitab[fg];
100    else if(fg == CUCUL_COLOR_DEFAULT)
101        fg = ansitab[CUCUL_COLOR_LIGHTGRAY];
102    else if(fg == CUCUL_COLOR_TRANSPARENT)
103        fg = 0x0fff;
104
105    if(bg < CUCUL_COLOR_DEFAULT)
106        bg = ansitab[bg];
107    else if(bg == CUCUL_COLOR_DEFAULT)
108        bg = ansitab[CUCUL_COLOR_BLACK];
109    else if(bg == CUCUL_COLOR_TRANSPARENT)
110        bg = 0x0fff;
111
112    argb[0] = bg >> 12;
113    argb[1] = (bg >> 8) & 0xf;
114    argb[2] = (bg >> 4) & 0xf;
115    argb[3] = bg & 0xf;
116
117    argb[4] = fg >> 12;
118    argb[5] = (fg >> 8) & 0xf;
119    argb[6] = (fg >> 4) & 0xf;
120    argb[7] = fg & 0xf;
121}
122
Note: See TracBrowser for help on using the repository browser.