Colorspec: Fix from_any(), add to/from ARGB number

This commit is contained in:
Lars Mueller 2021-10-21 16:13:27 +02:00
parent 578d504575
commit 084d5e90c0

@ -1,5 +1,6 @@
-- Localize globals -- Localize globals
local assert, error, math, minetest, setmetatable, tonumber, type = assert, error, math, minetest, setmetatable, tonumber, type local assert, error, math, minetest, setmetatable, tonumber, type = assert, error, math, minetest, setmetatable, tonumber, type
local floor = math.floor
-- Set environment -- Set environment
local _ENV = ... local _ENV = ...
@ -194,7 +195,6 @@ function colorspec.from_string(string)
if len == 6 then if len == 6 then
return colorspec.from_number(num * 0x100 + 0xFF) return colorspec.from_number(num * 0x100 + 0xFF)
end end
local floor = math.floor
if len == 4 then if len == 4 then
return colorspec.from_table{ return colorspec.from_table{
a = (num % 16) * 17, a = (num % 16) * 17,
@ -210,13 +210,14 @@ 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" error"invalid colorstring"
end end
colorspec.from_text = colorspec.from_string colorspec.from_text = colorspec.from_string
--! This does not take a Minetest-accepted number, use :to_number_argb() instead
function colorspec.from_number(number) function colorspec.from_number(number)
local floor = math.floor
return colorspec.from_table{ return colorspec.from_table{
a = number % 0x100, a = number % 0x100,
b = floor(number / 0x100) % 0x100, b = floor(number / 0x100) % 0x100,
@ -226,7 +227,6 @@ function colorspec.from_number(number)
end end
function colorspec.from_number_rgb(number) function colorspec.from_number_rgb(number)
local floor = math.floor
return colorspec.from_table{ return colorspec.from_table{
a = 0xFF, a = 0xFF,
b = number % 0x100, b = number % 0x100,
@ -235,6 +235,15 @@ function colorspec.from_number_rgb(number)
} }
end end
function colorspec.from_number_argb(number)
return colorspec.from_table{
b = number % 0x100,
g = floor(number / 0x100) % 0x100,
r = floor(number / 0x10000) % 0x100,
a = floor(number / 0x1000000)
}
end
function colorspec.from_any(value) function colorspec.from_any(value)
local type = type(value) local type = type(value)
if type == "table" then if type == "table" then
@ -244,7 +253,7 @@ function colorspec.from_any(value)
return colorspec.from_string(value) return colorspec.from_string(value)
end end
if type == "number" then if type == "number" then
return colorspec.from_number(value) return colorspec.from_number_argb(value)
end end
error("Unsupported type " .. type) error("Unsupported type " .. type)
end end
@ -261,6 +270,7 @@ function colorspec:to_string()
return ("%02X%02X%02X%02X"):format(self.r, self.g, self.b, self.a) return ("%02X%02X%02X%02X"):format(self.r, self.g, self.b, self.a)
end end
--! This is not a Minetest-accepted number, use :to_number_argb() instead
function colorspec:to_number() function colorspec:to_number()
return self.r * 0x1000000 + self.g * 0x10000 + self.b * 0x100 + self.a return self.r * 0x1000000 + self.g * 0x10000 + self.b * 0x100 + self.a
end end
@ -269,6 +279,10 @@ function colorspec:to_number_rgb()
return self.r * 0x10000 + self.g * 0x100 + self.b return self.r * 0x10000 + self.g * 0x100 + self.b
end end
function colorspec:to_number_argb()
return self.a * 0x1000000 + self.r * 0x10000 + self.g * 0x100 + self.b
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() return colorspec.from_any(spec):to_string()
end end