Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

colour.c 5.3 KiB

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