So I have a few minor changes to color.c :>

I actually posted this linked from sprunge.us a couple times in
#awesome and it wasn't correct all those times.  So this is meant to
be the final patch where everything is good and happy. :x

See attached.

Would love some peer review and maybe eventually accepted into master?
 The only validly good thing about this patch is it removes a problem
where if color_parse() failed it would warn about the color being
invalid twice.  (Since the warn() was sent again after the return of
color_parse() in color_init_unchecked()  Maybe this should be two
patches, or three, but I felt that all of it was simple enough to be
one :>  If it were 3 it would be "deduplicate warn() used in
color_parse() and color_init_unchecked()", make the bit harvesting in
color_parse() (possibly?) more understandable and combine the if's in
color_init_unchecked()", then "rework the RGB_8TO16/16TO8 macros and
use 16TO8 in luaA_push_color()"  But that's like 3 patches for pretty
simple changes. :>

The important thing to remember is 0xFFFF / 0xFF == 0x101 >.<

Toodles.

PS: The only reason I touched the if in color_parse() was to help it
short-circuit sooner.
From fec7b848f428ce85c2e55ac95328a362c63d2f56 Mon Sep 17 00:00:00 2001
From: Majic <[email protected]>
Date: Fri, 18 Nov 2011 18:27:26 -0800
Subject: [PATCH] Touch-up RGB_8TO16/16TO8 macros, use in luaA_push_color()

---
 color.c |   33 +++++++++++++++------------------
 1 files changed, 15 insertions(+), 18 deletions(-)

diff --git a/color.c b/color.c
index ceee8d4..e11b5c1 100644
--- a/color.c
+++ b/color.c
@@ -26,8 +26,9 @@
 #include "globalconf.h"
 #include "common/xutil.h"
 
-#define RGB_8TO16(i)   (0xffff * ((i) & 0xff) / 0xff)
-#define RGB_16TO8(i)   (0xff * ((i) & 0xffff) / 0xffff)
+/* 0xFFFF / 0xFF == 0x101 */
+#define RGB_8TO16(i) (((i) & 0xff)   * 0x101)
+#define RGB_16TO8(i) (((i) & 0xffff) / 0x101)
 
 /** Parse an hexadecimal color string to its component.
  * \param colstr The color string.
@@ -45,15 +46,18 @@ color_parse(const char *colstr, ssize_t len,
     char *p;
 
     colnum = strtoul(colstr + 1, &p, 16);
-    if(len != 7 || (p - colstr) != 7 || colstr[0] != '#')
+
+    if (len != 7 || colstr[0] != '#' || (p - colstr) != 7)
     {
         warn("awesome: error, invalid color '%s'", colstr);
         return false;
     }
 
-    *red   = (colnum >> 16) & 0xff;
-    *green = (colnum >> 8) & 0xff;
-    *blue  = colnum & 0xff;
+    *blue  = colnum & 0xFF;
+    colnum >>= 8;
+    *green = colnum & 0xFF;
+    colnum >>= 8;
+    *red   = colnum & 0xFF;
 
     return true;
 }
@@ -74,7 +78,8 @@ color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
 
     p_clear(&req, 1);
 
-    if(!len)
+    /* The color is given in RGB value */
+    if(len == 0 || !color_parse(colstr, len, &red, &green, &blue))
     {
         req.has_error = true;
         return req;
@@ -82,14 +87,6 @@ color_init_unchecked(color_t *color, const char *colstr, ssize_t len)
 
     req.color = color;
 
-    /* The color is given in RGB value */
-    if(!color_parse(colstr, len, &red, &green, &blue))
-    {
-        warn("awesome: error, invalid color '%s'", colstr);
-        req.has_error = true;
-        return req;
-    }
-
     req.cookie_hexa = xcb_alloc_color_unchecked(globalconf.connection,
                                                 globalconf.default_cmap,
                                                 RGB_8TO16(red),
@@ -138,9 +135,9 @@ color_init_reply(color_init_request_t req)
 int
 luaA_pushcolor(lua_State *L, const color_t c)
 {
-    uint8_t r = (unsigned) c.red   * 0xff / 0xffff;
-    uint8_t g = (unsigned) c.green * 0xff / 0xffff;
-    uint8_t b = (unsigned) c.blue  * 0xff / 0xffff;
+    uint8_t r = RGB_16TO8(c.red);
+    uint8_t g = RGB_16TO8(c.green);
+    uint8_t b = RGB_16TO8(c.blue);
     char s[10];
     int len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
     lua_pushlstring(L, s, len);
-- 
1.7.7.3

Reply via email to