diff --git a/colorstring_to_number.lua b/colorstring_to_number.lua index 0e06ea1..d0c2dd2 100644 --- a/colorstring_to_number.lua +++ b/colorstring_to_number.lua @@ -1,8 +1,7 @@ -- -- hud_fs: Render formspecs into HUDs -- --- This is the fallback parser for ColorStrings when --- minetest.colorspec_to_colorstring doesn't exist. +-- This is the fallback parser for ColorStrings when modlib isn't installed. -- -- From https://www.w3.org/TR/css-color-3/#svg-color diff --git a/init.lua b/init.lua index 86d1a7c..e03ab79 100644 --- a/init.lua +++ b/init.lua @@ -14,28 +14,23 @@ local DEFAULT_Z_INDEX = 0 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 function colorstring_to_number_fallback(col) - colorstring_to_number_fallback = dofile(minetest.get_modpath(modname) .. - "/colorstring_to_number.lua") - colorstring_to_number = colorstring_to_number_fallback - return colorstring_to_number_fallback(col) -end - -if minetest.colorspec_to_colorstring then +if minetest.global_exists("modlib") and modlib.minetest.colorspec and + modlib.minetest.colorspec.from_number_rgb then + local pcall, from_any = pcall, modlib.minetest.colorspec.from_any function colorstring_to_number(col) - local res = minetest.colorspec_to_colorstring(col) - if res and (res:byte(1) ~= 35 or #res < 7) then - -- Unexpected return value, go back to using the fallback parser. - 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) + local ok, spec = pcall(from_any, col) + if not ok then return end + return spec:to_number_rgb() end 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 -- Hacks to allow colorize() to work to some extent on labels diff --git a/mod.conf b/mod.conf index a212968..1918b18 100644 --- a/mod.conf +++ b/mod.conf @@ -1,2 +1,3 @@ name = hud_fs depends = formspec_ast +optional_depends = modlib