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

Last change on this file since 764 was 764, checked in by Sam Hocevar, 14 years ago
  • Fixed colour conversion routines so that they work with ARGB too.
  • Property svn:keywords set to Id
File size: 2.8 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 764 2006-04-13 16:54:25Z 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    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}
72
73uint8_t _cucul_argb32_to_ansi8(uint32_t c)
74{
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);
80}
81
82uint8_t _cucul_argb32_to_ansi4fg(uint32_t c)
83{
84    return nearest_ansi(c & 0xffff, CUCUL_COLOR_LIGHTGRAY);
85}
86
87uint8_t _cucul_argb32_to_ansi4bg(uint32_t c)
88{
89    return nearest_ansi(c >> 16, CUCUL_COLOR_BLACK);
90}
91
92void _cucul_argb32_to_argb4(uint32_t c, uint8_t argb[8])
93{
94    uint16_t fg = c & 0xffff;
95    uint16_t bg = c >> 16;
96
97    if(fg < CUCUL_COLOR_DEFAULT)
98        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;
103
104    if(bg < CUCUL_COLOR_DEFAULT)
105        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;
110
111    argb[0] = bg >> 12;
112    argb[1] = (bg >> 8) & 0xf;
113    argb[2] = (bg >> 4) & 0xf;
114    argb[3] = bg & 0xf;
115
116    argb[4] = fg >> 12;
117    argb[5] = (fg >> 8) & 0xf;
118    argb[6] = (fg >> 4) & 0xf;
119    argb[7] = fg & 0xf;
120}
121
Note: See TracBrowser for help on using the repository browser.