Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

346 řádky
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 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. /** \brief Set the default colour pair for text (truecolor version).
  125. *
  126. * Set the default ARGB colour pair for text drawing. String functions such
  127. * as caca_printf() and graphical primitive functions such as caca_draw_line()
  128. * will use these attributes.
  129. *
  130. * Colors are 16-bit ARGB values, each component being coded on 4 bits. For
  131. * instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
  132. * white with 50% alpha (A=8 R=15 G=15 B=15).
  133. *
  134. * If an error occurs, 0 is returned and \b errno is set accordingly:
  135. * - \c EINVAL At least one of the colour values is invalid.
  136. *
  137. * \param cv A handle to the libcucul canvas.
  138. * \param fg The requested ARGB foreground colour.
  139. * \param bg The requested ARGB background colour.
  140. * \return 0 in case of success, -1 if an error occurred.
  141. */
  142. int cucul_set_color_argb(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  143. {
  144. uint32_t attr;
  145. if(fg > 0xffff || bg > 0xffff)
  146. {
  147. #if defined(HAVE_ERRNO_H)
  148. errno = EINVAL;
  149. #endif
  150. return -1;
  151. }
  152. if(fg < 0x100)
  153. fg += 0x100;
  154. if(bg < 0x100)
  155. bg += 0x100;
  156. fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
  157. bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);
  158. attr = ((uint32_t)bg << 18) | ((uint32_t)fg << 4);
  159. cv->curattr = (cv->curattr & 0x0000000f) | attr;
  160. return 0;
  161. }
  162. /*
  163. * XXX: the following functions are local
  164. */
  165. /* RGB colours for the ANSI palette. There is no real standard, so we
  166. * use the same values as gnome-terminal. The 7th colour (brown) is a bit
  167. * special: 0xfa50 instead of 0xfaa0. */
  168. static const uint16_t ansitab16[16] =
  169. {
  170. 0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
  171. 0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
  172. };
  173. /* Same table, except on 14 bits (3-4-4-3) */
  174. static const uint16_t ansitab14[16] =
  175. {
  176. 0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
  177. 0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
  178. };
  179. static uint8_t nearest_ansi(uint16_t argb14, uint8_t def)
  180. {
  181. unsigned int i, best, dist;
  182. if(argb14 < 0x0050)
  183. return argb14 ^ 0x40;
  184. if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
  185. return def;
  186. if(argb14 < 0x0fff) /* too transparent, return default colour */
  187. return def;
  188. best = def;
  189. dist = 0x3fff;
  190. for(i = 0; i < 16; i++)
  191. {
  192. unsigned int d = 0;
  193. int a, b;
  194. a = (ansitab14[i] >> 7) & 0xf;
  195. b = (argb14 >> 7) & 0xf;
  196. d += (a - b) * (a - b);
  197. a = (ansitab14[i] >> 3) & 0xf;
  198. b = (argb14 >> 3) & 0xf;
  199. d += (a - b) * (a - b);
  200. a = (ansitab14[i] << 1) & 0xf;
  201. b = (argb14 << 1) & 0xf;
  202. d += (a - b) * (a - b);
  203. if(d < dist)
  204. {
  205. dist = d;
  206. best = i;
  207. }
  208. }
  209. return best;
  210. }
  211. uint8_t _cucul_attr_to_ansi8(uint32_t attr)
  212. {
  213. uint16_t fg = (attr >> 4) & 0x3fff;
  214. uint16_t bg = attr >> 18;
  215. return nearest_ansi(fg, CUCUL_LIGHTGRAY)
  216. | (nearest_ansi(bg, CUCUL_BLACK) << 4);
  217. }
  218. uint8_t _cucul_attr_to_ansi4fg(uint32_t attr)
  219. {
  220. return nearest_ansi((attr >> 4) & 0x3fff, CUCUL_LIGHTGRAY);
  221. }
  222. uint8_t _cucul_attr_to_ansi4bg(uint32_t attr)
  223. {
  224. return nearest_ansi(attr >> 18, CUCUL_BLACK);
  225. }
  226. uint16_t _cucul_attr_to_rgb12fg(uint32_t attr)
  227. {
  228. uint16_t fg = (attr >> 4) & 0x3fff;
  229. if(fg < 0x0050)
  230. return ansitab16[fg ^ 0x40] & 0x0fff;
  231. if(fg == (CUCUL_DEFAULT | 0x40))
  232. return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
  233. if(fg == (CUCUL_TRANSPARENT | 0x40))
  234. return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
  235. return (fg << 1) & 0x0fff;
  236. }
  237. uint16_t _cucul_attr_to_rgb12bg(uint32_t attr)
  238. {
  239. uint16_t bg = attr >> 18;
  240. if(bg < 0x0050)
  241. return ansitab16[bg ^ 0x40] & 0x0fff;
  242. if(bg == (CUCUL_DEFAULT | 0x40))
  243. return ansitab16[CUCUL_BLACK] & 0x0fff;
  244. if(bg == (CUCUL_TRANSPARENT | 0x40))
  245. return ansitab16[CUCUL_BLACK] & 0x0fff;
  246. return (bg << 1) & 0x0fff;
  247. }
  248. #define RGB12TO24(i) \
  249. (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
  250. | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
  251. | ((uint32_t)(i & 0x00f) * 0x000011))
  252. uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
  253. {
  254. return RGB12TO24(_cucul_attr_to_rgb12fg(attr));
  255. }
  256. uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
  257. {
  258. return RGB12TO24(_cucul_attr_to_rgb12bg(attr));
  259. }
  260. void _cucul_attr_to_argb4(uint32_t attr, uint8_t argb[8])
  261. {
  262. uint16_t fg = (attr >> 4) & 0x3fff;
  263. uint16_t bg = attr >> 18;
  264. if(bg < 0x0050)
  265. bg = ansitab16[bg ^ 0x40];
  266. else if(bg == (CUCUL_DEFAULT | 0x40))
  267. bg = ansitab16[CUCUL_BLACK];
  268. else if(bg == (CUCUL_TRANSPARENT | 0x40))
  269. bg = 0x0fff;
  270. else
  271. bg = ((bg << 2) & 0xf000) | ((bg << 1) & 0x0fff);
  272. argb[0] = bg >> 12;
  273. argb[1] = (bg >> 8) & 0xf;
  274. argb[2] = (bg >> 4) & 0xf;
  275. argb[3] = bg & 0xf;
  276. if(fg < 0x0050)
  277. fg = ansitab16[fg ^ 0x40];
  278. else if(fg == (CUCUL_DEFAULT | 0x40))
  279. fg = ansitab16[CUCUL_LIGHTGRAY];
  280. else if(fg == (CUCUL_TRANSPARENT | 0x40))
  281. fg = 0x0fff;
  282. else
  283. fg = ((fg << 2) & 0xf000) | ((fg << 1) & 0x0fff);
  284. argb[4] = fg >> 12;
  285. argb[5] = (fg >> 8) & 0xf;
  286. argb[6] = (fg >> 4) & 0xf;
  287. argb[7] = fg & 0xf;
  288. }