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.
 
 
 
 
 
 

356 lignes
9.7 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. /** \brief Get the text attribute at the given coordinates.
  25. *
  26. * Get the internal \e libcucul attribute value of the character at the
  27. * given coordinates. The attribute value has 32 significant bits,
  28. * organised as follows from MSB to LSB:
  29. * - 3 bits for the background alpha
  30. * - 4 bits for the background red component
  31. * - 4 bits for the background green component
  32. * - 3 bits for the background blue component
  33. * - 3 bits for the foreground alpha
  34. * - 4 bits for the foreground red component
  35. * - 4 bits for the foreground green component
  36. * - 3 bits for the foreground blue component
  37. * - 4 bits for the bold, italics, underline and blink flags
  38. *
  39. * If the coordinates are outside the canvas boundaries, the current
  40. * attribute is returned.
  41. *
  42. * This function never fails.
  43. *
  44. * \param cv A handle to the libcucul canvas.
  45. * \param x X coordinate.
  46. * \param y Y coordinate.
  47. * \return The requested attribute.
  48. */
  49. unsigned long int cucul_get_attr(cucul_canvas_t *cv, int x, int y)
  50. {
  51. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  52. return (unsigned long int)cv->curattr;
  53. return (unsigned long int)cv->attrs[x + y * cv->width];
  54. }
  55. /** \brief Set the default character attribute.
  56. *
  57. * Set the default character attribute for drawing. Attributes define
  58. * foreground and background colour, transparency, bold, italics and
  59. * underline styles, as well as blink. String functions such as
  60. * caca_printf() and graphical primitive functions such as caca_draw_line()
  61. * will use this attribute.
  62. *
  63. * The value of \e attr is either:
  64. * - a 32-bit integer as returned by cucul_get_attr(), in which case it
  65. * also contains colour information,
  66. * - a combination (bitwise OR) of style values (\e CUCUL_UNDERLINE,
  67. * \e CUCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case
  68. * setting the attribute does not modify the current colour information.
  69. *
  70. * If an error occurs, -1 is returned and \b errno is set accordingly:
  71. * - \c EINVAL The attribute value is out of the 32-bit range.
  72. *
  73. * \param cv A handle to the libcucul canvas.
  74. * \param attr The requested attribute value.
  75. * \return 0 in case of success, -1 if an error occurred.
  76. */
  77. int cucul_set_attr(cucul_canvas_t *cv, unsigned long int attr)
  78. {
  79. if(sizeof(unsigned long int) > sizeof(uint32_t) && attr > 0xffffffff)
  80. {
  81. #if defined(HAVE_ERRNO_H)
  82. errno = EINVAL;
  83. #endif
  84. return -1;
  85. }
  86. if(attr < 0x00000010)
  87. attr = (cv->curattr & 0xfffffff0) | attr;
  88. cv->curattr = attr;
  89. return 0;
  90. }
  91. /** \brief Set the default colour pair for text (ANSI version).
  92. *
  93. * Set the default ANSI colour pair for text drawing. String functions such
  94. * as caca_printf() and graphical primitive functions such as caca_draw_line()
  95. * will use these attributes.
  96. *
  97. * Color values are those defined in cucul.h, such as CUCUL_RED
  98. * or CUCUL_TRANSPARENT.
  99. *
  100. * If an error occurs, 0 is returned and \b errno is set accordingly:
  101. * - \c EINVAL At least one of the colour values is invalid.
  102. *
  103. * \param cv A handle to the libcucul canvas.
  104. * \param fg The requested ANSI foreground colour.
  105. * \param bg The requested ANSI background colour.
  106. * \return 0 in case of success, -1 if an error occurred.
  107. */
  108. int cucul_set_color_ansi(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
  109. {
  110. uint32_t attr;
  111. if(fg > 0x20 || bg > 0x20)
  112. {
  113. #if defined(HAVE_ERRNO_H)
  114. errno = EINVAL;
  115. #endif
  116. return -1;
  117. }
  118. attr = ((uint32_t)(bg | 0x40) << 18) | ((uint32_t)(fg | 0x40) << 4);
  119. cv->curattr = (cv->curattr & 0x0000000f) | attr;
  120. return 0;
  121. }
  122. /* Legacy function for old programs */
  123. int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
  124. {
  125. return cucul_set_color_ansi(cv, fg, bg);
  126. }
  127. /** \brief Set the default colour pair for text (truecolor version).
  128. *
  129. * Set the default ARGB colour pair for text drawing. String functions such
  130. * as caca_printf() and graphical primitive functions such as caca_draw_line()
  131. * will use these attributes.
  132. *
  133. * Colors are 16-bit ARGB values, each component being coded on 4 bits. For
  134. * instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
  135. * white with 50% alpha (A=8 R=15 G=15 B=15).
  136. *
  137. * If an error occurs, 0 is returned and \b errno is set accordingly:
  138. * - \c EINVAL At least one of the colour values is invalid.
  139. *
  140. * \param cv A handle to the libcucul canvas.
  141. * \param fg The requested ARGB foreground colour.
  142. * \param bg The requested ARGB background colour.
  143. * \return 0 in case of success, -1 if an error occurred.
  144. */
  145. int cucul_set_color_argb(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  146. {
  147. uint32_t attr;
  148. if(fg > 0xffff || bg > 0xffff)
  149. {
  150. #if defined(HAVE_ERRNO_H)
  151. errno = EINVAL;
  152. #endif
  153. return -1;
  154. }
  155. if(fg < 0x100)
  156. fg += 0x100;
  157. if(bg < 0x100)
  158. bg += 0x100;
  159. fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
  160. bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);
  161. attr = ((uint32_t)bg << 18) | ((uint32_t)fg << 4);
  162. cv->curattr = (cv->curattr & 0x0000000f) | attr;
  163. return 0;
  164. }
  165. /* Legacy function for old programs */
  166. int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  167. {
  168. return cucul_set_color_argb(cv, fg, bg);
  169. }
  170. /*
  171. * XXX: the following functions are local
  172. */
  173. /* RGB colours for the ANSI palette. There is no real standard, so we
  174. * use the same values as gnome-terminal. The 7th colour (brown) is a bit
  175. * special: 0xfa50 instead of 0xfaa0. */
  176. static const uint16_t ansitab16[16] =
  177. {
  178. 0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
  179. 0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
  180. };
  181. /* Same table, except on 14 bits (3-4-4-3) */
  182. static const uint16_t ansitab14[16] =
  183. {
  184. 0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
  185. 0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
  186. };
  187. static uint8_t nearest_ansi(uint16_t argb14, uint8_t def)
  188. {
  189. unsigned int i, best, dist;
  190. if(argb14 < 0x0050)
  191. return argb14 ^ 0x40;
  192. if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
  193. return def;
  194. if(argb14 < 0x0fff) /* too transparent, return default colour */
  195. return def;
  196. best = def;
  197. dist = 0x3fff;
  198. for(i = 0; i < 16; i++)
  199. {
  200. unsigned int d = 0;
  201. int a, b;
  202. a = (ansitab14[i] >> 7) & 0xf;
  203. b = (argb14 >> 7) & 0xf;
  204. d += (a - b) * (a - b);
  205. a = (ansitab14[i] >> 3) & 0xf;
  206. b = (argb14 >> 3) & 0xf;
  207. d += (a - b) * (a - b);
  208. a = (ansitab14[i] << 1) & 0xf;
  209. b = (argb14 << 1) & 0xf;
  210. d += (a - b) * (a - b);
  211. if(d < dist)
  212. {
  213. dist = d;
  214. best = i;
  215. }
  216. }
  217. return best;
  218. }
  219. uint8_t _cucul_attr_to_ansi8(uint32_t attr)
  220. {
  221. uint16_t fg = (attr >> 4) & 0x3fff;
  222. uint16_t bg = attr >> 18;
  223. return nearest_ansi(fg, CUCUL_LIGHTGRAY)
  224. | (nearest_ansi(bg, CUCUL_BLACK) << 4);
  225. }
  226. uint8_t _cucul_attr_to_ansi4fg(uint32_t attr)
  227. {
  228. return nearest_ansi((attr >> 4) & 0x3fff, CUCUL_LIGHTGRAY);
  229. }
  230. uint8_t _cucul_attr_to_ansi4bg(uint32_t attr)
  231. {
  232. return nearest_ansi(attr >> 18, CUCUL_BLACK);
  233. }
  234. uint16_t _cucul_attr_to_rgb12fg(uint32_t attr)
  235. {
  236. uint16_t fg = (attr >> 4) & 0x3fff;
  237. if(fg < 0x0050)
  238. return ansitab16[fg ^ 0x40] & 0x0fff;
  239. if(fg == (CUCUL_DEFAULT | 0x40))
  240. return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
  241. if(fg == (CUCUL_TRANSPARENT | 0x40))
  242. return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
  243. return (fg << 1) & 0x0fff;
  244. }
  245. uint16_t _cucul_attr_to_rgb12bg(uint32_t attr)
  246. {
  247. uint16_t bg = attr >> 18;
  248. if(bg < 0x0050)
  249. return ansitab16[bg ^ 0x40] & 0x0fff;
  250. if(bg == (CUCUL_DEFAULT | 0x40))
  251. return ansitab16[CUCUL_BLACK] & 0x0fff;
  252. if(bg == (CUCUL_TRANSPARENT | 0x40))
  253. return ansitab16[CUCUL_BLACK] & 0x0fff;
  254. return (bg << 1) & 0x0fff;
  255. }
  256. #define RGB12TO24(i) \
  257. (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
  258. | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
  259. | ((uint32_t)(i & 0x00f) * 0x000011))
  260. uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
  261. {
  262. return RGB12TO24(_cucul_attr_to_rgb12fg(attr));
  263. }
  264. uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
  265. {
  266. return RGB12TO24(_cucul_attr_to_rgb12bg(attr));
  267. }
  268. void _cucul_attr_to_argb4(uint32_t attr, uint8_t argb[8])
  269. {
  270. uint16_t fg = (attr >> 4) & 0x3fff;
  271. uint16_t bg = attr >> 18;
  272. if(bg < 0x0050)
  273. bg = ansitab16[bg ^ 0x40];
  274. else if(bg == (CUCUL_DEFAULT | 0x40))
  275. bg = ansitab16[CUCUL_BLACK];
  276. else if(bg == (CUCUL_TRANSPARENT | 0x40))
  277. bg = 0x0fff;
  278. else
  279. bg = ((bg << 2) & 0xf000) | ((bg << 1) & 0x0fff);
  280. argb[0] = bg >> 12;
  281. argb[1] = (bg >> 8) & 0xf;
  282. argb[2] = (bg >> 4) & 0xf;
  283. argb[3] = bg & 0xf;
  284. if(fg < 0x0050)
  285. fg = ansitab16[fg ^ 0x40];
  286. else if(fg == (CUCUL_DEFAULT | 0x40))
  287. fg = ansitab16[CUCUL_LIGHTGRAY];
  288. else if(fg == (CUCUL_TRANSPARENT | 0x40))
  289. fg = 0x0fff;
  290. else
  291. fg = ((fg << 2) & 0xf000) | ((fg << 1) & 0x0fff);
  292. argb[4] = fg >> 12;
  293. argb[5] = (fg >> 8) & 0xf;
  294. argb[6] = (fg >> 4) & 0xf;
  295. argb[7] = fg & 0xf;
  296. }