mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 01:03:46 +01:00
colorspec.from_string(): return nothing if invalid
This commit is contained in:
parent
aac9eea46e
commit
e048fb3b03
@ -203,22 +203,29 @@ 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
|
||||||
assert(alpha_text:len() == 2)
|
if alpha_text:len() ~= 2 then
|
||||||
number = assert(named_colors[name])
|
return
|
||||||
|
end
|
||||||
|
number = named_colors[name]
|
||||||
|
if not number then
|
||||||
|
return
|
||||||
|
end
|
||||||
alpha = tonumber(alpha_text, 16)
|
alpha = tonumber(alpha_text, 16)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if number then
|
if number then
|
||||||
return colorspec.from_number(number * 0x100 + alpha)
|
return colorspec.from_number_rgba(number * 0x100 + alpha)
|
||||||
end
|
end
|
||||||
local hex_text = string:match("^" .. hex .. "$")
|
local hex_text = string:match("^" .. hex .. "$")
|
||||||
assert(hex_text, "invalid colorstring")
|
if not hex_text then
|
||||||
|
return
|
||||||
|
end
|
||||||
local len, num = hex_text:len(), tonumber(hex_text, 16)
|
local len, num = hex_text:len(), tonumber(hex_text, 16)
|
||||||
if len == 8 then
|
if len == 8 then
|
||||||
return colorspec.from_number(num)
|
return colorspec.from_number_rgba(num)
|
||||||
end
|
end
|
||||||
if len == 6 then
|
if len == 6 then
|
||||||
return colorspec.from_number(num * 0x100 + 0xFF)
|
return colorspec.from_number_rgba(num * 0x100 + 0xFF)
|
||||||
end
|
end
|
||||||
if len == 4 then
|
if len == 4 then
|
||||||
return colorspec.from_table{
|
return colorspec.from_table{
|
||||||
@ -235,8 +242,6 @@ function colorspec.from_string(string)
|
|||||||
r = (floor(num / (16 ^ 2)) % 16) * 17
|
r = (floor(num / (16 ^ 2)) % 16) * 17
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
-- TODO return nil
|
|
||||||
error"invalid colorstring"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
colorspec.from_text = colorspec.from_string
|
colorspec.from_text = colorspec.from_string
|
||||||
@ -308,5 +313,9 @@ function colorspec:to_number()
|
|||||||
end
|
end
|
||||||
|
|
||||||
colorspec_to_colorstring = minetest.colorspec_to_colorstring or function(spec)
|
colorspec_to_colorstring = minetest.colorspec_to_colorstring or function(spec)
|
||||||
return colorspec.from_any(spec):to_string()
|
local color = colorspec.from_any(spec)
|
||||||
|
if not color then
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
return color:to_string()
|
||||||
end
|
end
|
||||||
|
4
test.lua
4
test.lua
@ -331,7 +331,7 @@ assert(minetest.luon:read_string(minetest.luon:write_string(ItemStack"")))
|
|||||||
local colorspec = minetest.colorspec
|
local colorspec = minetest.colorspec
|
||||||
local function test_from_string(string, number)
|
local function test_from_string(string, number)
|
||||||
local spec = colorspec.from_string(string)
|
local spec = colorspec.from_string(string)
|
||||||
local expected = colorspec.from_number(number)
|
local expected = colorspec.from_number_rgba(number)
|
||||||
assertdump(table.equals(spec, expected), {expected = expected, actual = spec})
|
assertdump(table.equals(spec, expected), {expected = expected, actual = spec})
|
||||||
end
|
end
|
||||||
local spec = colorspec.from_number(0xDDCCBBAA)
|
local spec = colorspec.from_number(0xDDCCBBAA)
|
||||||
@ -341,7 +341,7 @@ test_from_string("aliceblue#42", 0xf0f8ff42)
|
|||||||
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)
|
||||||
assert(colorspec.from_string"#694269":to_string() == "694269")
|
assert(colorspec.from_string"#694269":to_string() == "#694269")
|
||||||
|
|
||||||
-- Persistence
|
-- Persistence
|
||||||
local function test_logfile(reference_strings)
|
local function test_logfile(reference_strings)
|
||||||
|
Loading…
Reference in New Issue
Block a user