mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-10 02:31:48 +00:00
Added (more helpful) error on first use rather than on require when missing bitops library for colour.pack/unpack methods
fixes https://github.com/1bardesign/batteries/issues/35
This commit is contained in:
parent
5b49cbdc17
commit
c10338fa5d
48
colour.lua
48
colour.lua
@ -4,9 +4,6 @@
|
||||
|
||||
local path = (...):gsub("colour", "")
|
||||
|
||||
local bit = require("bit")
|
||||
local band, bor = bit.band, bit.bor
|
||||
local lshift, rshift = bit.lshift, bit.rshift
|
||||
|
||||
local math = require(path.."mathx")
|
||||
|
||||
@ -16,6 +13,27 @@ local colour = {}
|
||||
-- hex handling routines
|
||||
-- pack and unpack into 24 or 32 bit hex numbers
|
||||
|
||||
local ok, bit = pcall(require, "bit")
|
||||
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)
|
||||
|
||||
function colour.pack_rgb(r, g, b)
|
||||
@ -26,9 +44,9 @@ function colour.pack_rgb(r, g, b)
|
||||
end
|
||||
|
||||
function colour.unpack_rgb(rgb)
|
||||
local r = rshift(band(rgb, 0x00ff0000), 16) / 255.0
|
||||
local g = rshift(band(rgb, 0x0000ff00), 8) / 255.0
|
||||
local b = rshift(band(rgb, 0x000000ff), 0) / 255.0
|
||||
local r = rshift(band(rgb, 0x00ff0000), 16) / 255
|
||||
local g = rshift(band(rgb, 0x0000ff00), 8) / 255
|
||||
local b = rshift(band(rgb, 0x000000ff), 0) / 255
|
||||
return r, g, b
|
||||
end
|
||||
|
||||
@ -43,10 +61,10 @@ function colour.pack_argb(r, g, b, a)
|
||||
end
|
||||
|
||||
function colour.unpack_argb(argb)
|
||||
local r = rshift(band(argb, 0x00ff0000), 16) / 255.0
|
||||
local g = rshift(band(argb, 0x0000ff00), 8) / 255.0
|
||||
local b = rshift(band(argb, 0x000000ff), 0) / 255.0
|
||||
local a = rshift(band(argb, 0xff000000), 24) / 255.0
|
||||
local r = rshift(band(argb, 0x00ff0000), 16) / 255
|
||||
local g = rshift(band(argb, 0x0000ff00), 8) / 255
|
||||
local b = rshift(band(argb, 0x000000ff), 0) / 255
|
||||
local a = rshift(band(argb, 0xff000000), 24) / 255
|
||||
return r, g, b, a
|
||||
end
|
||||
|
||||
@ -61,12 +79,14 @@ function colour.pack_rgba(r, g, b, a)
|
||||
end
|
||||
|
||||
function colour.unpack_rgba(rgba)
|
||||
local r = rshift(band(rgba, 0xff000000), 24) / 255.0
|
||||
local g = rshift(band(rgba, 0x00ff0000), 16) / 255.0
|
||||
local b = rshift(band(rgba, 0x0000ff00), 8) / 255.0
|
||||
local a = rshift(band(rgba, 0x000000ff), 0) / 255.0
|
||||
local r = rshift(band(rgba, 0xff000000), 24) / 255
|
||||
local g = rshift(band(rgba, 0x00ff0000), 16) / 255
|
||||
local b = rshift(band(rgba, 0x0000ff00), 8) / 255
|
||||
local a = rshift(band(rgba, 0x000000ff), 0) / 255
|
||||
return r, g, b, a
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- colour space conversion
|
||||
|
Loading…
Reference in New Issue
Block a user