No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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