Mirror of Awesome WM window manager
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.

color.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * color.c - color functions
  3. *
  4. * Copyright © 2008-2009 Julien Danjou <julien@danjou.info>
  5. * Copyright © 2009 Uli Schlachter <psychon@znc.in>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. */
  22. #include "color.h"
  23. #include "globalconf.h"
  24. #include <ctype.h>
  25. /* 0xFFFF / 0xFF == 0x101 (257) */
  26. #define RGB_8TO16(i) (((i) & 0xff) * 0x101)
  27. #define RGB_16TO8(i) (((i) & 0xffff) / 0x101)
  28. /** Parse an hexadecimal color string to its component.
  29. * \param colstr The color string.
  30. * \param len The color string length.
  31. * \param red A pointer to the red color to fill.
  32. * \param green A pointer to the green color to fill.
  33. * \param blue A pointer to the blue color to fill.
  34. * \return True if everything alright.
  35. */
  36. static bool
  37. color_parse(const char *colstr, ssize_t len,
  38. uint8_t *red, uint8_t *green, uint8_t *blue, uint8_t *alpha)
  39. {
  40. unsigned long colnum;
  41. char *p;
  42. *alpha = 0xff;
  43. colnum = strtoul(colstr + 1, &p, 16);
  44. if(len == 9 && (p - colstr) == 9)
  45. {
  46. *alpha = colnum & 0xff;
  47. colnum >>= 8;
  48. len -= 2;
  49. p -= 2;
  50. }
  51. if(len != 7 || colstr[0] != '#' || (p - colstr) != 7)
  52. {
  53. warn("awesome: error, invalid color '%s'", colstr);
  54. return false;
  55. }
  56. *red = (colnum >> 16) & 0xff;
  57. *green = (colnum >> 8) & 0xff;
  58. *blue = colnum & 0xff;
  59. return true;
  60. }
  61. /** Given a color component value in the range from 0x00 to 0xff and a mask that
  62. * specifies where the component is, move the component into place. For example,
  63. * component=0x12 and mask=0xff00 return 0x1200. Note that the mask can have a
  64. * different width, for example component=0x12 and mask=0xf00 return 0x100.
  65. * \param component The intensity of a color component.
  66. * \param mask A mask containing a consecutive number of bits set to 1 defining
  67. * where the component is.
  68. * \return A pixel value containing the giving component in the given component.
  69. */
  70. static uint32_t
  71. apply_mask(uint8_t component, uint32_t mask)
  72. {
  73. unsigned int shift = 0;
  74. unsigned int width = 0;
  75. // Shift the mask right until the first set bit appears
  76. while (mask != 0 && (mask & 0x1) == 0) {
  77. shift++;
  78. mask >>= 1;
  79. }
  80. // Shift the mask right until we saw all set bits
  81. while ((mask & 0x1) == 1) {
  82. width++;
  83. mask >>= 1;
  84. }
  85. // The mask consists of [width] set bits followed by [shift] unset bits.
  86. // Modify the component accordingly.
  87. uint32_t result = component;
  88. // Scale the result up to the desired width
  89. if (width < 8)
  90. result >>= (8 - width);
  91. if (width > 8)
  92. result <<= (width - 8);
  93. return result << shift;
  94. }
  95. /** Send a request to initialize a X color.
  96. * \param color color_t struct to store color into.
  97. * \param colstr Color specification.
  98. * \param len The length of colstr (which still MUST be NULL terminated).
  99. * \param visual The visual for which the color is to be allocated.
  100. * \return request informations.
  101. */
  102. color_init_request_t
  103. color_init_unchecked(color_t *color, const char *colstr, ssize_t len, xcb_visualtype_t *visual)
  104. {
  105. color_init_request_t req;
  106. uint8_t red, green, blue, alpha;
  107. p_clear(&req, 1);
  108. if(!len)
  109. {
  110. req.has_error = true;
  111. return req;
  112. }
  113. req.color = color;
  114. /* The color is given in RGB value */
  115. if(!color_parse(colstr, len, &red, &green, &blue, &alpha))
  116. {
  117. warn("awesome: error, invalid color '%s'", colstr);
  118. req.has_error = true;
  119. return req;
  120. }
  121. req.colstr = colstr;
  122. req.has_error = false;
  123. if (visual->_class == XCB_VISUAL_CLASS_TRUE_COLOR || visual->_class == XCB_VISUAL_CLASS_DIRECT_COLOR) {
  124. uint32_t pixel = 0;
  125. pixel |= apply_mask(red, visual->red_mask);
  126. pixel |= apply_mask(blue, visual->blue_mask);
  127. pixel |= apply_mask(green, visual->green_mask);
  128. if (draw_visual_depth(globalconf.screen, visual->visual_id) == 32) {
  129. /* This is not actually in the X11 protocol, but we assume that this
  130. * is an ARGB visual and everything unset in some mask is alpha.
  131. */
  132. pixel |= apply_mask(alpha, ~(visual->red_mask | visual->blue_mask | visual->green_mask));
  133. }
  134. req.color->pixel = pixel;
  135. req.color->red = RGB_8TO16(red);
  136. req.color->green = RGB_8TO16(green);
  137. req.color->blue = RGB_8TO16(blue);
  138. req.color->alpha = RGB_8TO16(alpha);
  139. req.color->initialized = true;
  140. return req;
  141. }
  142. req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
  143. globalconf.default_cmap,
  144. RGB_8TO16(red),
  145. RGB_8TO16(green),
  146. RGB_8TO16(blue));
  147. return req;
  148. }
  149. /** Initialize a X color.
  150. * \param req color_init request.
  151. * \return True if color allocation was successful.
  152. */
  153. bool
  154. color_init_reply(color_init_request_t req)
  155. {
  156. if(req.has_error)
  157. return false;
  158. if(req.color->initialized)
  159. return true;
  160. xcb_alloc_color_reply_t *hexa_color;
  161. if((hexa_color = xcb_alloc_color_reply(globalconf.connection,
  162. req.cookie_hexa, NULL)))
  163. {
  164. req.color->pixel = hexa_color->pixel;
  165. req.color->red = hexa_color->red;
  166. req.color->green = hexa_color->green;
  167. req.color->blue = hexa_color->blue;
  168. req.color->alpha = 0xffff;
  169. req.color->initialized = true;
  170. p_delete(&hexa_color);
  171. return true;
  172. }
  173. warn("awesome: error, cannot allocate color '%s'", req.colstr);
  174. return false;
  175. }
  176. /** Push a color as a string onto the stack
  177. * \param L The Lua VM state.
  178. * \param c The color to push.
  179. * \return The number of elements pushed on stack.
  180. */
  181. int
  182. luaA_pushcolor(lua_State *L, const color_t c)
  183. {
  184. uint8_t r = RGB_16TO8(c.red);
  185. uint8_t g = RGB_16TO8(c.green);
  186. uint8_t b = RGB_16TO8(c.blue);
  187. uint8_t a = RGB_16TO8(c.alpha);
  188. char s[1 + 4*2 + 1];
  189. int len;
  190. if (a >= 0xff)
  191. len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
  192. else
  193. len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
  194. lua_pushlstring(L, s, len);
  195. return 1;
  196. }
  197. // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80