You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

224 line
5.3 KiB

  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$
  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. * This file contains functions for converting colour values between
  15. * various colourspaces.
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include "cucul.h"
  20. #include "cucul_internals.h"
  21. static const uint16_t ansitab[16] =
  22. {
  23. 0xf000, 0xf008, 0xf080, 0xf088, 0xf800, 0xf808, 0xf880, 0xf888,
  24. 0xf444, 0xf44f, 0xf4f4, 0xf4ff, 0xff44, 0xff4f, 0xfff4, 0xffff,
  25. };
  26. /** \brief Set the default colour pair.
  27. *
  28. * This function sets the default ANSI colour pair. String functions such as
  29. * caca_printf() and graphical primitive functions such as caca_draw_line()
  30. * will use these colours.
  31. *
  32. * Color values are those defined in cucul.h, such as CUCUL_COLOR_RED
  33. * or CUCUL_COLOR_TRANSPARENT.
  34. *
  35. * \param cv A handle to the libcucul canvas.
  36. * \param fg The requested foreground colour.
  37. * \param bg The requested background colour.
  38. */
  39. void cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
  40. {
  41. if(fg > 0x20 || bg > 0x20)
  42. return;
  43. cv->fgcolor = fg;
  44. cv->bgcolor = bg;
  45. }
  46. /** \brief Set the default colour pair (truecolor version).
  47. *
  48. * This function sets the default colour pair. String functions such as
  49. * caca_printf() and graphical primitive functions such as caca_draw_line()
  50. * will use these colours.
  51. *
  52. * Colors are 16-bit ARGB values, each component being coded on 4 bits. For
  53. * instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
  54. * white with 50% alpha (A=8 R=15 G=15 B=15).
  55. *
  56. * \param cv A handle to the libcucul canvas.
  57. * \param fg The requested foreground colour.
  58. * \param bg The requested background colour.
  59. */
  60. void cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  61. {
  62. if(fg > 0xffff || bg > 0xffff)
  63. return;
  64. if(fg < 0x100)
  65. fg += 0x100;
  66. if(bg < 0x100)
  67. bg += 0x100;
  68. cv->fgcolor = fg;
  69. cv->bgcolor = bg;
  70. }
  71. /*
  72. * XXX: the following functions are local
  73. */
  74. static uint8_t nearest_ansi(uint16_t argb16, uint8_t def)
  75. {
  76. unsigned int i, best, dist;
  77. if(argb16 < CUCUL_COLOR_DEFAULT)
  78. return argb16;
  79. if(argb16 == CUCUL_COLOR_DEFAULT || argb16 == CUCUL_COLOR_TRANSPARENT)
  80. return def;
  81. if(argb16 < 0x5fff) /* too transparent, return default colour */
  82. return def;
  83. best = def;
  84. dist = 0xffff;
  85. for(i = 0; i < 16; i++)
  86. {
  87. unsigned int d = 0;
  88. int a, b;
  89. a = (ansitab[i] >> 8) & 0xf;
  90. b = (argb16 >> 8) & 0xf;
  91. d += (a - b) * (a - b);
  92. a = (ansitab[i] >> 4) & 0xf;
  93. b = (argb16 >> 4) & 0xf;
  94. d += (a - b) * (a - b);
  95. a = ansitab[i] & 0xf;
  96. b = argb16 & 0xf;
  97. d += (a - b) * (a - b);
  98. if(d < dist)
  99. {
  100. dist = d;
  101. best = i;
  102. }
  103. }
  104. return best;
  105. }
  106. uint8_t _cucul_argb32_to_ansi8(uint32_t ch)
  107. {
  108. uint16_t fg = ch & 0xffff;
  109. uint16_t bg = ch >> 16;
  110. return nearest_ansi(fg, CUCUL_COLOR_LIGHTGRAY)
  111. | (nearest_ansi(bg, CUCUL_COLOR_BLACK) << 4);
  112. }
  113. uint8_t _cucul_argb32_to_ansi4fg(uint32_t ch)
  114. {
  115. return nearest_ansi(ch & 0xffff, CUCUL_COLOR_LIGHTGRAY);
  116. }
  117. uint8_t _cucul_argb32_to_ansi4bg(uint32_t ch)
  118. {
  119. return nearest_ansi(ch >> 16, CUCUL_COLOR_BLACK);
  120. }
  121. uint16_t _cucul_argb32_to_rgb12fg(uint32_t ch)
  122. {
  123. uint16_t fg = ch & 0xffff;
  124. if(fg < CUCUL_COLOR_DEFAULT)
  125. return ansitab[fg] & 0x0fff;
  126. if(fg == CUCUL_COLOR_DEFAULT)
  127. return ansitab[CUCUL_COLOR_LIGHTGRAY] & 0x0fff;
  128. if(fg == CUCUL_COLOR_TRANSPARENT)
  129. return 0x0fff;
  130. return fg & 0x0fff;
  131. }
  132. uint16_t _cucul_argb32_to_rgb12bg(uint32_t ch)
  133. {
  134. uint16_t bg = ch >> 16;
  135. if(bg < CUCUL_COLOR_DEFAULT)
  136. return ansitab[bg] & 0x0fff;
  137. if(bg == CUCUL_COLOR_DEFAULT)
  138. return ansitab[CUCUL_COLOR_BLACK] & 0x0fff;
  139. if(bg == CUCUL_COLOR_TRANSPARENT)
  140. return 0x0fff;
  141. return bg & 0x0fff;
  142. }
  143. #define RGB12TO24(i) \
  144. (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
  145. | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
  146. | ((uint32_t)(i & 0x00f) * 0x000011))
  147. uint32_t _cucul_argb32_to_rgb24fg(uint32_t ch)
  148. {
  149. return RGB12TO24(_cucul_argb32_to_rgb12fg(ch));
  150. }
  151. uint32_t _cucul_argb32_to_rgb24bg(uint32_t ch)
  152. {
  153. return RGB12TO24(_cucul_argb32_to_rgb12bg(ch));
  154. }
  155. void _cucul_argb32_to_argb4(uint32_t ch, uint8_t argb[8])
  156. {
  157. uint16_t fg = ch & 0xffff;
  158. uint16_t bg = ch >> 16;
  159. if(fg < CUCUL_COLOR_DEFAULT)
  160. fg = ansitab[fg];
  161. else if(fg == CUCUL_COLOR_DEFAULT)
  162. fg = ansitab[CUCUL_COLOR_LIGHTGRAY];
  163. else if(fg == CUCUL_COLOR_TRANSPARENT)
  164. fg = 0x0fff;
  165. if(bg < CUCUL_COLOR_DEFAULT)
  166. bg = ansitab[bg];
  167. else if(bg == CUCUL_COLOR_DEFAULT)
  168. bg = ansitab[CUCUL_COLOR_BLACK];
  169. else if(bg == CUCUL_COLOR_TRANSPARENT)
  170. bg = 0x0fff;
  171. argb[0] = bg >> 12;
  172. argb[1] = (bg >> 8) & 0xf;
  173. argb[2] = (bg >> 4) & 0xf;
  174. argb[3] = bg & 0xf;
  175. argb[4] = fg >> 12;
  176. argb[5] = (fg >> 8) & 0xf;
  177. argb[6] = (fg >> 4) & 0xf;
  178. argb[7] = fg & 0xf;
  179. }