Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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