mirror of
https://github.com/1bardesign/batteries.git
synced 2024-12-01 17:24:34 +00:00
colour: Don't depend on bit library.
This commit is contained in:
parent
be48dd9115
commit
fabbd3f702
66
colour.lua
66
colour.lua
@ -13,79 +13,49 @@ local colour = {}
|
|||||||
-- hex handling routines
|
-- hex handling routines
|
||||||
-- pack and unpack into 24 or 32 bit hex numbers
|
-- pack and unpack into 24 or 32 bit hex numbers
|
||||||
|
|
||||||
local ok, bit = pcall(require, "bit")
|
local floor = math.floor
|
||||||
if not ok then
|
|
||||||
for _, v in ipairs{
|
|
||||||
"pack_rgb",
|
|
||||||
"unpack_rgb",
|
|
||||||
"pack_argb",
|
|
||||||
"unpack_argb",
|
|
||||||
"pack_rgba",
|
|
||||||
"unpack_rgba",
|
|
||||||
} do
|
|
||||||
colour[v] = function()
|
|
||||||
error(
|
|
||||||
"batteries.colour requires the bit operations module for pack/unpack functionality.\n"
|
|
||||||
.."\tsee https://bitop.luajit.org/\n\n"
|
|
||||||
.."error from require(\"bit\"):\n\n"..bit)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else
|
|
||||||
local band, bor = bit.band, bit.bor
|
|
||||||
local lshift, rshift = bit.lshift, bit.rshift
|
|
||||||
|
|
||||||
--rgb only (no alpha)
|
--rgb only (no alpha)
|
||||||
|
|
||||||
function colour.pack_rgb(r, g, b)
|
function colour.pack_rgb(r, g, b)
|
||||||
local br = lshift(band(0xff, r * 255), 16)
|
local br = floor(0xff * r) % 0x100 * 0x10000
|
||||||
local bg = lshift(band(0xff, g * 255), 8)
|
local bg = floor(0xff * g) % 0x100 * 0x100
|
||||||
local bb = lshift(band(0xff, b * 255), 0)
|
local bb = floor(0xff * b) % 0x100
|
||||||
return bor( br, bg, bb )
|
return br + bg + bb
|
||||||
end
|
end
|
||||||
|
|
||||||
function colour.unpack_rgb(rgb)
|
function colour.unpack_rgb(rgb)
|
||||||
local r = rshift(band(rgb, 0x00ff0000), 16) / 255
|
local r = floor(rgb / 0x10000) % 0x100
|
||||||
local g = rshift(band(rgb, 0x0000ff00), 8) / 255
|
local g = floor(rgb / 0x100) % 0x100
|
||||||
local b = rshift(band(rgb, 0x000000ff), 0) / 255
|
local b = floor(rgb) % 0x100
|
||||||
return r, g, b
|
return r / 255, g / 255, b / 255
|
||||||
end
|
end
|
||||||
|
|
||||||
--argb format (common for shared hex)
|
--argb format (common for shared hex)
|
||||||
|
|
||||||
function colour.pack_argb(r, g, b, a)
|
function colour.pack_argb(r, g, b, a)
|
||||||
local ba = lshift(band(0xff, a * 255), 24)
|
local ba = floor(0xff * a) % 0x100 * 0x1000000
|
||||||
local br = lshift(band(0xff, r * 255), 16)
|
return colour.pack_rgb(r, g, b) + ba
|
||||||
local bg = lshift(band(0xff, g * 255), 8)
|
|
||||||
local bb = lshift(band(0xff, b * 255), 0)
|
|
||||||
return bor( br, bg, bb, ba )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function colour.unpack_argb(argb)
|
function colour.unpack_argb(argb)
|
||||||
local r = rshift(band(argb, 0x00ff0000), 16) / 255
|
local r, g, b = colour.unpack_rgb(argb)
|
||||||
local g = rshift(band(argb, 0x0000ff00), 8) / 255
|
local a = floor(argb / 0x1000000) % 0x100
|
||||||
local b = rshift(band(argb, 0x000000ff), 0) / 255
|
return r, g, b, a / 255
|
||||||
local a = rshift(band(argb, 0xff000000), 24) / 255
|
|
||||||
return r, g, b, a
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--rgba format
|
--rgba format
|
||||||
|
|
||||||
function colour.pack_rgba(r, g, b, a)
|
function colour.pack_rgba(r, g, b, a)
|
||||||
local br = lshift(band(0xff, r * 255), 24)
|
local ba = floor(0xff * a) % 0x100
|
||||||
local bg = lshift(band(0xff, g * 255), 16)
|
return colour.pack_rgb(r, g, b) * 0x100 + ba
|
||||||
local bb = lshift(band(0xff, b * 255), 8)
|
|
||||||
local ba = lshift(band(0xff, a * 255), 0)
|
|
||||||
return bor( br, bg, bb, ba )
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function colour.unpack_rgba(rgba)
|
function colour.unpack_rgba(rgba)
|
||||||
local r = rshift(band(rgba, 0xff000000), 24) / 255
|
local r, g, b = colour.unpack_rgb(floor(rgba / 0x100))
|
||||||
local g = rshift(band(rgba, 0x00ff0000), 16) / 255
|
local a = floor(rgba) % 0x100
|
||||||
local b = rshift(band(rgba, 0x0000ff00), 8) / 255
|
|
||||||
local a = rshift(band(rgba, 0x000000ff), 0) / 255
|
|
||||||
return r, g, b, a
|
return r, g, b, a
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
|
Loading…
Reference in New Issue
Block a user