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

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