Browse Source

Correctly convert colors with alpha to a Lua string

Signed-off-by: Uli Schlachter <psychon@znc.in>
Uli Schlachter 1 year ago
parent
commit
8a44d59716
3 changed files with 27 additions and 3 deletions
  1. 10
    3
      color.c
  2. 1
    0
      color.h
  3. 16
    0
      tests/test-focus.lua

+ 10
- 3
color.c View File

@@ -156,6 +156,7 @@ color_init_unchecked(color_t *color, const char *colstr, ssize_t len, xcb_visual
156 156
         req.color->red   = RGB_8TO16(red);
157 157
         req.color->green = RGB_8TO16(green);
158 158
         req.color->blue  = RGB_8TO16(blue);
159
+        req.color->alpha = RGB_8TO16(alpha);
159 160
         req.color->initialized = true;
160 161
         return req;
161 162
     }
@@ -190,6 +191,7 @@ color_init_reply(color_init_request_t req)
190 191
         req.color->red   = hexa_color->red;
191 192
         req.color->green = hexa_color->green;
192 193
         req.color->blue  = hexa_color->blue;
194
+        req.color->alpha = 0xffff;
193 195
         req.color->initialized = true;
194 196
         p_delete(&hexa_color);
195 197
         return true;
@@ -210,9 +212,14 @@ luaA_pushcolor(lua_State *L, const color_t c)
210 212
     uint8_t r = RGB_16TO8(c.red);
211 213
     uint8_t g = RGB_16TO8(c.green);
212 214
     uint8_t b = RGB_16TO8(c.blue);
213
-
214
-    char s[10];
215
-    int len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
215
+    uint8_t a = RGB_16TO8(c.alpha);
216
+
217
+    char s[1 + 4*2 + 1];
218
+    int len;
219
+    if (a >= 0xff)
220
+        len = snprintf(s, sizeof(s), "#%02x%02x%02x", r, g, b);
221
+    else
222
+        len = snprintf(s, sizeof(s), "#%02x%02x%02x%02x", r, g, b, a);
216 223
     lua_pushlstring(L, s, len);
217 224
     return 1;
218 225
 }

+ 1
- 0
color.h View File

@@ -33,6 +33,7 @@ typedef struct
33 33
     uint16_t red;
34 34
     uint16_t green;
35 35
     uint16_t blue;
36
+    uint16_t alpha;
36 37
     bool initialized;
37 38
 } color_t;
38 39
 

+ 16
- 0
tests/test-focus.lua View File

@@ -49,6 +49,22 @@ local steps = {
49 49
 
50 50
             end
51 51
         end
52
+    end,
53
+
54
+    -- Test if alpha works as intended
55
+    function()
56
+        local c = client.get()[1]
57
+
58
+        local function test(set, expected)
59
+            expected = expected or set
60
+            c.border_color = set
61
+            assert_equals(c.border_color, expected)
62
+        end
63
+
64
+        test("#123456")
65
+        test("#12345678")
66
+        test("#123456ff", "#123456")
67
+        return true
52 68
     end
53 69
 }
54 70
 

Loading…
Cancel
Save