Use modlib's colorspec helpers if installed
This commit is contained in:
parent
936f665017
commit
122eca40eb
|
@ -1,8 +1,7 @@
|
||||||
--
|
--
|
||||||
-- hud_fs: Render formspecs into HUDs
|
-- hud_fs: Render formspecs into HUDs
|
||||||
--
|
--
|
||||||
-- This is the fallback parser for ColorStrings when
|
-- This is the fallback parser for ColorStrings when modlib isn't installed.
|
||||||
-- minetest.colorspec_to_colorstring doesn't exist.
|
|
||||||
--
|
--
|
||||||
|
|
||||||
-- From https://www.w3.org/TR/css-color-3/#svg-color
|
-- From https://www.w3.org/TR/css-color-3/#svg-color
|
||||||
|
|
31
init.lua
31
init.lua
|
@ -14,28 +14,23 @@ local DEFAULT_Z_INDEX = 0
|
||||||
|
|
||||||
local floor, type, pairs, max = math.floor, type, pairs, math.max
|
local floor, type, pairs, max = math.floor, type, pairs, math.max
|
||||||
|
|
||||||
|
-- Attempt to use modlib's parser
|
||||||
|
-- The to_number_rgb function was broken before the addition of from_number_rgb
|
||||||
local colorstring_to_number
|
local colorstring_to_number
|
||||||
local function colorstring_to_number_fallback(col)
|
if minetest.global_exists("modlib") and modlib.minetest.colorspec and
|
||||||
colorstring_to_number_fallback = dofile(minetest.get_modpath(modname) ..
|
modlib.minetest.colorspec.from_number_rgb then
|
||||||
"/colorstring_to_number.lua")
|
local pcall, from_any = pcall, modlib.minetest.colorspec.from_any
|
||||||
colorstring_to_number = colorstring_to_number_fallback
|
|
||||||
return colorstring_to_number_fallback(col)
|
|
||||||
end
|
|
||||||
|
|
||||||
if minetest.colorspec_to_colorstring then
|
|
||||||
function colorstring_to_number(col)
|
function colorstring_to_number(col)
|
||||||
local res = minetest.colorspec_to_colorstring(col)
|
local ok, spec = pcall(from_any, col)
|
||||||
if res and (res:byte(1) ~= 35 or #res < 7) then
|
if not ok then return end
|
||||||
-- Unexpected return value, go back to using the fallback parser.
|
return spec:to_number_rgb()
|
||||||
minetest.log("warning", ("[hud_fs] Unexpected value returned by" ..
|
|
||||||
" minetest.colorspec_to_colorstring(%q): %q"):format(col,
|
|
||||||
res))
|
|
||||||
return colorstring_to_number_fallback(col)
|
|
||||||
end
|
|
||||||
return res and tonumber(res:sub(2, 7), 16)
|
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
colorstring_to_number = colorstring_to_number_fallback
|
function colorstring_to_number(col)
|
||||||
|
colorstring_to_number = dofile(minetest.get_modpath(modname) ..
|
||||||
|
"/colorstring_to_number.lua")
|
||||||
|
return colorstring_to_number(col)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Hacks to allow colorize() to work to some extent on labels
|
-- Hacks to allow colorize() to work to some extent on labels
|
||||||
|
|
Loading…
Reference in New Issue