ensure_pango_color: Support hex values with alpha

Reference: https://github.com/awesomeWM/awesome/pull/2129#discussion_r155397507
Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2017-12-07 10:47:58 +01:00 committed by Yauhen Kirylau
parent 9b6cea5a13
commit 8cc469322f
2 changed files with 11 additions and 1 deletions

View File

@ -338,6 +338,11 @@ end
-- @treturn string color if it is valid, else fallback. -- @treturn string color if it is valid, else fallback.
function color.ensure_pango_color(check_color, fallback) function color.ensure_pango_color(check_color, fallback)
check_color = tostring(check_color) check_color = tostring(check_color)
-- Pango markup supports alpha, PangoColor does not. Thus, check for this.
local len = #check_color
if string.match(check_color, "^#%x+$") and (len == 5 or len == 9 or len == 17) then
return check_color
end
return Pango.Color.parse(Pango.Color(), check_color) and check_color or fallback or "black" return Pango.Color.parse(Pango.Color(), check_color) and check_color or fallback or "black"
end end

View File

@ -271,7 +271,7 @@ describe("gears.color", function()
describe("ensure_pango_color", function() describe("ensure_pango_color", function()
-- Successful cases -- Successful cases
for _, value in ipairs{ "red", "cyan", "black", "#f00", "#014578", for _, value in ipairs{ "red", "cyan", "black", "#f00", "#014578",
"#01ef01ef01ef" "#01ef01ef01ef", "#f00f", "#014578ab", "#01ef01ef01ef01ef"
} do } do
it(value, function() it(value, function()
assert.is.same(value, color.ensure_pango_color(value)) assert.is.same(value, color.ensure_pango_color(value))
@ -282,6 +282,11 @@ describe("gears.color", function()
assert.is.same("black", color.ensure_pango_color("#abz")) assert.is.same("black", color.ensure_pango_color("#abz"))
end) end)
it("#fedcba98765432", function()
-- Only one, two or four characters per channel are supported
assert.is.same("black", color.ensure_pango_color("#fedcba98765432"))
end)
it("fallback", function() it("fallback", function()
assert.is.same("zzz", color.ensure_pango_color("#abz", "zzz")) assert.is.same("zzz", color.ensure_pango_color("#abz", "zzz"))
end) end)