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.
 
 
 
 
 
 

360 lines
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. /* 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: 0xfa50 instead of 0xfaa0. */
  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 character attribute.
  33. *
  34. * Set the default character attribute for drawing. Attributes define
  35. * foreground and background colour, transparency, bold, italics and
  36. * underline styles, as well as blink. String functions such as
  37. * caca_printf() and graphical primitive functions such as caca_draw_line()
  38. * will use this attribute.
  39. *
  40. * The attribute value is a 32-bit integer as returned by cucul_get_attr().
  41. * For more user-friendly versions of this function, see cucul_set_attr_ansi()
  42. * and cucul_set_attr_argb().
  43. *
  44. * If an error occurs, -1 is returned and \b errno is set accordingly:
  45. * - \c EINVAL The attribute value is out of the 32-bit range.
  46. *
  47. * \param cv A handle to the libcucul canvas.
  48. * \param attr The requested attribute value.
  49. * \return 0 in case of success, -1 if an error occurred.
  50. */
  51. int cucul_set_attr(cucul_canvas_t *cv, unsigned long int attr)
  52. {
  53. if(sizeof(unsigned long int) > sizeof(uint32_t) && attr > 0xffffffff)
  54. {
  55. #if defined(HAVE_ERRNO_H)
  56. errno = EINVAL;
  57. #endif
  58. return -1;
  59. }
  60. cv->curattr = attr;
  61. return 0;
  62. }
  63. /** \brief Set the default colour pair and text style (ANSI version).
  64. *
  65. * Set the default ANSI colour pair and text style for drawing. String
  66. * functions such as caca_printf() and graphical primitive functions such as
  67. * caca_draw_line() will use these attributes.
  68. *
  69. * Color values are those defined in cucul.h, such as CUCUL_COLOR_RED
  70. * or CUCUL_COLOR_TRANSPARENT.
  71. *
  72. * Style values are those defined in cucul.h, such as CUCUL_STYLE_UNDERLINE
  73. * or CUCUL_STYLE_BLINK. The values can be ORed to set several styles at
  74. * the same time.
  75. *
  76. * If an error occurs, -1 is returned and \b errno is set accordingly:
  77. * - \c EINVAL The colour values and/or the style mask are invalid.
  78. *
  79. * \param cv A handle to the libcucul canvas.
  80. * \param fg The requested foreground colour.
  81. * \param bg The requested background colour.
  82. * \param style The requested text styles.
  83. * \return 0 in case of success, -1 if an error occurred.
  84. */
  85. int cucul_set_attr_ansi(cucul_canvas_t *cv, unsigned char fg, unsigned char bg,
  86. unsigned char style)
  87. {
  88. uint32_t attr;
  89. if(fg > 0x20 || bg > 0x20 || style > 0x0f)
  90. {
  91. #if defined(HAVE_ERRNO_H)
  92. errno = EINVAL;
  93. #endif
  94. return -1;
  95. }
  96. attr = ((uint32_t)bg << 20) | ((uint32_t)fg << 4);
  97. if(style)
  98. attr |= (0x02004801 * style) & 0x10011001;
  99. cv->curattr = attr;
  100. return 0;
  101. }
  102. /* Legacy function for old programs */
  103. int cucul_set_color(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
  104. {
  105. return cucul_set_attr_ansi(cv, fg, bg, 0);
  106. }
  107. /** \brief Set the default colour pair and text style (truecolor version).
  108. *
  109. * Set the default colour pair and text style for drawing. String
  110. * functions such as caca_printf() and graphical primitive functions such as
  111. * caca_draw_line() will use these attributes.
  112. *
  113. * Colors are 16-bit ARGB values, each component being coded on 4 bits. For
  114. * instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
  115. * white with 50% alpha (A=8 R=15 G=15 B=15).
  116. *
  117. * Style values are those defined in cucul.h, such as CUCUL_STYLE_UNDERLINE
  118. * or CUCUL_STYLE_BLINK. The values can be ORed to set several styles at
  119. * the same time.
  120. *
  121. * If an error occurs, -1 is returned and \b errno is set accordingly:
  122. * - \c EINVAL At least one of the colour values is invalid.
  123. *
  124. * \param cv A handle to the libcucul canvas.
  125. * \param fg The requested foreground colour.
  126. * \param bg The requested background colour.
  127. * \param style The requested text styles.
  128. * \return 0 in case of success, -1 if an error occurred.
  129. */
  130. int cucul_set_attr_argb(cucul_canvas_t *cv, unsigned int fg, unsigned int bg,
  131. unsigned char style)
  132. {
  133. uint32_t attr;
  134. if(fg > 0xffff || bg > 0xffff || style > 0x0f)
  135. {
  136. #if defined(HAVE_ERRNO_H)
  137. errno = EINVAL;
  138. #endif
  139. return -1;
  140. }
  141. if(fg < 0x100)
  142. fg += 0x100;
  143. if(bg < 0x100)
  144. bg += 0x100;
  145. attr = (((uint32_t)bg << 16) | (uint32_t)fg) & 0xeffeeffe;
  146. if(style)
  147. attr |= (0x02004801 * style) & 0x10011001;
  148. cv->curattr = attr;
  149. return 0;
  150. }
  151. /* Legacy function for old programs */
  152. int cucul_set_truecolor(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  153. {
  154. return cucul_set_attr_argb(cv, fg, bg, 0);
  155. }
  156. /** \brief Get the text attribute at the given coordinates.
  157. *
  158. * Get the internal \e libcucul attribute value of the character at the
  159. * given coordinates. The attribute value has 32 significant bits,
  160. * organised as follows from MSB to LSB:
  161. * - 3 bits for the background alpha
  162. * - 1 bit for the blink flag
  163. * - 4 bits for the background red component
  164. * - 4 bits for the background green component
  165. * - 3 bits for the background blue component
  166. * - 1 bit for the underline flag
  167. * - 3 bits for the foreground alpha
  168. * - 1 bit for the italics flag
  169. * - 4 bits for the foreground red component
  170. * - 4 bits for the foreground green component
  171. * - 3 bits for the foreground blue component
  172. * - 1 bit for the bold flag
  173. *
  174. * If the coordinates are outside the canvas boundaries, the current
  175. * attribute is returned.
  176. *
  177. * This function never fails.
  178. *
  179. * \param cv A handle to the libcucul canvas.
  180. * \param x X coordinate.
  181. * \param y Y coordinate.
  182. * \return The requested attribute.
  183. */
  184. unsigned long int cucul_get_attr(cucul_canvas_t *cv, int x, int y)
  185. {
  186. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  187. return (unsigned long int)cv->curattr;
  188. return (unsigned long int)cv->attrs[x + y * cv->width];
  189. }
  190. /*
  191. * XXX: the following functions are local
  192. */
  193. static uint8_t nearest_ansi(uint16_t argb16, uint8_t def)
  194. {
  195. unsigned int i, best, dist;
  196. if(argb16 == (argb16 & 0x00f0))
  197. return argb16 >> 4;
  198. if(argb16 == (CUCUL_COLOR_DEFAULT << 4)
  199. || argb16 == (CUCUL_COLOR_TRANSPARENT << 4))
  200. return def;
  201. if(argb16 < 0x6fff) /* too transparent, return default colour */
  202. return def;
  203. best = def;
  204. dist = 0xffff;
  205. for(i = 0; i < 16; i++)
  206. {
  207. unsigned int d = 0;
  208. int a, b;
  209. a = (ansitab[i] >> 8) & 0xf;
  210. b = (argb16 >> 8) & 0xf;
  211. d += (a - b) * (a - b);
  212. a = (ansitab[i] >> 4) & 0xf;
  213. b = (argb16 >> 4) & 0xf;
  214. d += (a - b) * (a - b);
  215. a = ansitab[i] & 0xf;
  216. b = argb16 & 0xf;
  217. d += (a - b) * (a - b);
  218. if(d < dist)
  219. {
  220. dist = d;
  221. best = i;
  222. }
  223. }
  224. return best;
  225. }
  226. uint8_t _cucul_attr_to_ansi8(uint32_t attr)
  227. {
  228. uint16_t fg = attr & 0xeffe;
  229. uint16_t bg = (attr >> 16) & 0xeffe;
  230. return nearest_ansi(fg, CUCUL_COLOR_LIGHTGRAY)
  231. | (nearest_ansi(bg, CUCUL_COLOR_BLACK) << 4);
  232. }
  233. uint8_t _cucul_attr_to_ansi4fg(uint32_t attr)
  234. {
  235. return nearest_ansi(attr & 0xeffe, CUCUL_COLOR_LIGHTGRAY);
  236. }
  237. uint8_t _cucul_attr_to_ansi4bg(uint32_t attr)
  238. {
  239. return nearest_ansi((attr >> 16) & 0xeffe, CUCUL_COLOR_BLACK);
  240. }
  241. uint16_t _cucul_attr_to_rgb12fg(uint32_t attr)
  242. {
  243. uint16_t fg = attr & 0xeffe;
  244. if(fg == (fg & 0x00f0))
  245. return ansitab[fg >> 4] & 0x0fff;
  246. if(fg == (CUCUL_COLOR_DEFAULT << 4))
  247. return ansitab[CUCUL_COLOR_LIGHTGRAY] & 0x0fff;
  248. if(fg == (CUCUL_COLOR_TRANSPARENT << 4))
  249. return ansitab[CUCUL_COLOR_LIGHTGRAY] & 0x0fff;
  250. return fg & 0x0fff;
  251. }
  252. uint16_t _cucul_attr_to_rgb12bg(uint32_t attr)
  253. {
  254. uint16_t bg = (attr >> 16) & 0xeffe;
  255. if(bg == (bg & 0x00f0))
  256. return ansitab[bg >> 4] & 0x0fff;
  257. if(bg == (CUCUL_COLOR_DEFAULT << 4))
  258. return ansitab[CUCUL_COLOR_BLACK] & 0x0fff;
  259. if(bg == (CUCUL_COLOR_TRANSPARENT << 4))
  260. return ansitab[CUCUL_COLOR_BLACK] & 0x0fff;
  261. return bg & 0x0fff;
  262. }
  263. #define RGB12TO24(i) \
  264. (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
  265. | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
  266. | ((uint32_t)(i & 0x00f) * 0x000011))
  267. uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
  268. {
  269. return RGB12TO24(_cucul_attr_to_rgb12fg(attr));
  270. }
  271. uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
  272. {
  273. return RGB12TO24(_cucul_attr_to_rgb12bg(attr));
  274. }
  275. void _cucul_attr_to_argb4(uint32_t attr, uint8_t argb[8])
  276. {
  277. uint16_t fg = attr & 0xeffe;
  278. uint16_t bg = (attr >> 16) & 0xeffe;
  279. if(fg == (fg & 0x00f0))
  280. fg = ansitab[fg >> 4];
  281. else if(fg == (CUCUL_COLOR_DEFAULT << 4))
  282. fg = ansitab[CUCUL_COLOR_LIGHTGRAY];
  283. else if(fg == (CUCUL_COLOR_TRANSPARENT << 4))
  284. fg = 0x0fff;
  285. if(bg == (bg & 0x00f0))
  286. bg = ansitab[bg >> 4];
  287. else if(bg == (CUCUL_COLOR_DEFAULT << 4))
  288. bg = ansitab[CUCUL_COLOR_BLACK];
  289. else if(bg == (CUCUL_COLOR_TRANSPARENT << 4))
  290. bg = 0x0fff;
  291. argb[0] = bg >> 12;
  292. argb[1] = (bg >> 8) & 0xf;
  293. argb[2] = (bg >> 4) & 0xf;
  294. argb[3] = bg & 0xf;
  295. argb[4] = fg >> 12;
  296. argb[5] = (fg >> 8) & 0xf;
  297. argb[6] = (fg >> 4) & 0xf;
  298. argb[7] = fg & 0xf;
  299. }