Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

253 рядки
6.1 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. #if defined(HAVE_ERRNO_H)
  20. # include <errno.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. /* RGB colours for the ANSI palette. There is no real standard, so we
  25. * use the same values as gnome-terminal. The 7th colour (brown) is a bit
  26. * special. */
  27. static const uint16_t ansitab[16] =
  28. {
  29. 0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
  30. 0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
  31. };
  32. /** \brief Set the default colour pair.
  33. *
  34. * This function sets the default ANSI colour pair. String functions such as
  35. * caca_printf() and graphical primitive functions such as caca_draw_line()
  36. * will use these colours.
  37. *
  38. * Color values are those defined in cucul.h, such as CUCUL_COLOR_RED
  39. * or CUCUL_COLOR_TRANSPARENT.
  40. *
  41. * If an error occurs, -1 is returned and \b errno is set accordingly:
  42. * - \c EINVAL At least one of the colour values is invalid.
  43. *
  44. * \param cv A handle to the libcucul canvas.
  45. * \param fg The requested foreground colour.
  46. * \param bg The requested background colour.
  47. * \return 0 in case of success, -1 if an error occurred.
  48. */
  49. int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
  50. {
  51. if(fg > 0x20 || bg > 0x20)
  52. {
  53. #if defined(HAVE_ERRNO_H)
  54. errno = EINVAL;
  55. #endif
  56. return -1;
  57. }
  58. cv->fgcolor = fg;
  59. cv->bgcolor = bg;
  60. return 0;
  61. }
  62. /** \brief Set the default colour pair (truecolor version).
  63. *
  64. * This function sets the default colour pair. String functions such as
  65. * caca_printf() and graphical primitive functions such as caca_draw_line()
  66. * will use these colours.
  67. *
  68. * Colors are 16-bit ARGB values, each component being coded on 4 bits. For
  69. * instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
  70. * white with 50% alpha (A=8 R=15 G=15 B=15).
  71. *
  72. * If an error occurs, -1 is returned and \b errno is set accordingly:
  73. * - \c EINVAL At least one of the colour values is invalid.
  74. *
  75. * \param cv A handle to the libcucul canvas.
  76. * \param fg The requested foreground colour.
  77. * \param bg The requested background colour.
  78. * \return 0 in case of success, -1 if an error occurred.
  79. */
  80. int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  81. {
  82. if(fg > 0xffff || bg > 0xffff)
  83. {
  84. #if defined(HAVE_ERRNO_H)
  85. errno = EINVAL;
  86. #endif
  87. return -1;
  88. }
  89. if(fg < 0x100)
  90. fg += 0x100;
  91. if(bg < 0x100)
  92. bg += 0x100;
  93. cv->fgcolor = fg;
  94. cv->bgcolor = bg;
  95. return 0;
  96. }
  97. /*
  98. * XXX: the following functions are local
  99. */
  100. static uint8_t nearest_ansi(uint16_t argb16, uint8_t def)
  101. {
  102. unsigned int i, best, dist;
  103. if(argb16 < CUCUL_COLOR_DEFAULT)
  104. return argb16;
  105. if(argb16 == CUCUL_COLOR_DEFAULT || argb16 == CUCUL_COLOR_TRANSPARENT)
  106. return def;
  107. if(argb16 < 0x5fff) /* too transparent, return default colour */
  108. return def;
  109. best = def;
  110. dist = 0xffff;
  111. for(i = 0; i < 16; i++)
  112. {
  113. unsigned int d = 0;
  114. int a, b;
  115. a = (ansitab[i] >> 8) & 0xf;
  116. b = (argb16 >> 8) & 0xf;
  117. d += (a - b) * (a - b);
  118. a = (ansitab[i] >> 4) & 0xf;
  119. b = (argb16 >> 4) & 0xf;
  120. d += (a - b) * (a - b);
  121. a = ansitab[i] & 0xf;
  122. b = argb16 & 0xf;
  123. d += (a - b) * (a - b);
  124. if(d < dist)
  125. {
  126. dist = d;
  127. best = i;
  128. }
  129. }
  130. return best;
  131. }
  132. uint8_t _cucul_argb32_to_ansi8(uint32_t ch)
  133. {
  134. uint16_t fg = ch & 0xffff;
  135. uint16_t bg = ch >> 16;
  136. return nearest_ansi(fg, CUCUL_COLOR_LIGHTGRAY)
  137. | (nearest_ansi(bg, CUCUL_COLOR_BLACK) << 4);
  138. }
  139. uint8_t _cucul_argb32_to_ansi4fg(uint32_t ch)
  140. {
  141. return nearest_ansi(ch & 0xffff, CUCUL_COLOR_LIGHTGRAY);
  142. }
  143. uint8_t _cucul_argb32_to_ansi4bg(uint32_t ch)
  144. {
  145. return nearest_ansi(ch >> 16, CUCUL_COLOR_BLACK);
  146. }
  147. uint16_t _cucul_argb32_to_rgb12fg(uint32_t ch)
  148. {
  149. uint16_t fg = ch & 0xffff;
  150. if(fg < CUCUL_COLOR_DEFAULT)
  151. return ansitab[fg] & 0x0fff;
  152. if(fg == CUCUL_COLOR_DEFAULT)
  153. return ansitab[CUCUL_COLOR_LIGHTGRAY] & 0x0fff;
  154. if(fg == CUCUL_COLOR_TRANSPARENT)
  155. return ansitab[CUCUL_COLOR_LIGHTGRAY] & 0x0fff;
  156. return fg & 0x0fff;
  157. }
  158. uint16_t _cucul_argb32_to_rgb12bg(uint32_t ch)
  159. {
  160. uint16_t bg = ch >> 16;
  161. if(bg < CUCUL_COLOR_DEFAULT)
  162. return ansitab[bg] & 0x0fff;
  163. if(bg == CUCUL_COLOR_DEFAULT)
  164. return ansitab[CUCUL_COLOR_BLACK] & 0x0fff;
  165. if(bg == CUCUL_COLOR_TRANSPARENT)
  166. return ansitab[CUCUL_COLOR_BLACK] & 0x0fff;
  167. return bg & 0x0fff;
  168. }
  169. #define RGB12TO24(i) \
  170. (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
  171. | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
  172. | ((uint32_t)(i & 0x00f) * 0x000011))
  173. uint32_t _cucul_argb32_to_rgb24fg(uint32_t ch)
  174. {
  175. return RGB12TO24(_cucul_argb32_to_rgb12fg(ch));
  176. }
  177. uint32_t _cucul_argb32_to_rgb24bg(uint32_t ch)
  178. {
  179. return RGB12TO24(_cucul_argb32_to_rgb12bg(ch));
  180. }
  181. void _cucul_argb32_to_argb4(uint32_t ch, uint8_t argb[8])
  182. {
  183. uint16_t fg = ch & 0xffff;
  184. uint16_t bg = ch >> 16;
  185. if(fg < CUCUL_COLOR_DEFAULT)
  186. fg = ansitab[fg];
  187. else if(fg == CUCUL_COLOR_DEFAULT)
  188. fg = ansitab[CUCUL_COLOR_LIGHTGRAY];
  189. else if(fg == CUCUL_COLOR_TRANSPARENT)
  190. fg = 0x0fff;
  191. if(bg < CUCUL_COLOR_DEFAULT)
  192. bg = ansitab[bg];
  193. else if(bg == CUCUL_COLOR_DEFAULT)
  194. bg = ansitab[CUCUL_COLOR_BLACK];
  195. else if(bg == CUCUL_COLOR_TRANSPARENT)
  196. bg = 0x0fff;
  197. argb[0] = bg >> 12;
  198. argb[1] = (bg >> 8) & 0xf;
  199. argb[2] = (bg >> 4) & 0xf;
  200. argb[3] = bg & 0xf;
  201. argb[4] = fg >> 12;
  202. argb[5] = (fg >> 8) & 0xf;
  203. argb[6] = (fg >> 4) & 0xf;
  204. argb[7] = fg & 0xf;
  205. }