Colorspec: Support name#%x

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

View File

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

View File

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