Colorspec: Support name#%x

This commit is contained in:
Lars Mueller 2022-04-21 11:16:57 +02:00
parent 5dd5e50704
commit da0fc453cd
2 changed files with 5 additions and 1 deletions

@ -203,7 +203,7 @@ function colorspec.from_string(string)
if not number then if not number then
local name, alpha_text = string:match("^([a-z]+)" .. hex .. "$") local name, alpha_text = string:match("^([a-z]+)" .. hex .. "$")
if name then if name then
if alpha_text:len() ~= 2 then if alpha_text:len() > 2 then
return return
end end
number = named_colors[name] number = named_colors[name]
@ -211,6 +211,9 @@ function colorspec.from_string(string)
return return
end end
alpha = tonumber(alpha_text, 0x10) alpha = tonumber(alpha_text, 0x10)
if alpha_text:len() == 1 then
alpha = alpha * 0x11
end
end end
end end
if number then if number then

@ -389,6 +389,7 @@ local spec = colorspec.from_number_rgba(0xDDCCBBAA)
assertdump(table.equals(spec, {a = 0xAA, b = 0xBB, g = 0xCC, r = 0xDD}), spec) assertdump(table.equals(spec, {a = 0xAA, b = 0xBB, g = 0xCC, r = 0xDD}), spec)
test_from_string("aliceblue", 0xf0f8ffff) test_from_string("aliceblue", 0xf0f8ffff)
test_from_string("aliceblue#42", 0xf0f8ff42) test_from_string("aliceblue#42", 0xf0f8ff42)
test_from_string("aliceblue#3", 0xf0f8ff33)
test_from_string("#333", 0x333333FF) test_from_string("#333", 0x333333FF)
test_from_string("#694269", 0x694269FF) test_from_string("#694269", 0x694269FF)
test_from_string("#11223344", 0x11223344) test_from_string("#11223344", 0x11223344)