mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-10 02:31:48 +00:00
keep both bitop and float-only pack/unpack functions in colour
This commit is contained in:
parent
fabbd3f702
commit
4f86dc0a5e
128
colour.lua
128
colour.lua
@ -13,55 +13,110 @@ 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 floor = math.floor
|
local ok, bit = pcall(require, "bit")
|
||||||
|
if ok then
|
||||||
|
--we have bit operations module, use the fast path
|
||||||
|
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)
|
||||||
|
local br = lshift(band(0xff, r * 255), 16)
|
||||||
|
local bg = lshift(band(0xff, g * 255), 8)
|
||||||
|
local bb = lshift(band(0xff, b * 255), 0)
|
||||||
|
return bor( br, bg, bb )
|
||||||
|
end
|
||||||
|
|
||||||
function colour.pack_rgb(r, g, b)
|
function colour.unpack_rgb(rgb)
|
||||||
local br = floor(0xff * r) % 0x100 * 0x10000
|
local r = rshift(band(rgb, 0x00ff0000), 16) / 255
|
||||||
local bg = floor(0xff * g) % 0x100 * 0x100
|
local g = rshift(band(rgb, 0x0000ff00), 8) / 255
|
||||||
local bb = floor(0xff * b) % 0x100
|
local b = rshift(band(rgb, 0x000000ff), 0) / 255
|
||||||
return br + bg + bb
|
return r, g, b
|
||||||
end
|
end
|
||||||
|
|
||||||
function colour.unpack_rgb(rgb)
|
--argb format (common for shared hex)
|
||||||
local r = floor(rgb / 0x10000) % 0x100
|
|
||||||
local g = floor(rgb / 0x100) % 0x100
|
|
||||||
local b = floor(rgb) % 0x100
|
|
||||||
return r / 255, g / 255, b / 255
|
|
||||||
end
|
|
||||||
|
|
||||||
--argb format (common for shared hex)
|
function colour.pack_argb(r, g, b, a)
|
||||||
|
local ba = lshift(band(0xff, a * 255), 24)
|
||||||
|
local br = lshift(band(0xff, r * 255), 16)
|
||||||
|
local bg = lshift(band(0xff, g * 255), 8)
|
||||||
|
local bb = lshift(band(0xff, b * 255), 0)
|
||||||
|
return bor( br, bg, bb, ba )
|
||||||
|
end
|
||||||
|
|
||||||
function colour.pack_argb(r, g, b, a)
|
function colour.unpack_argb(argb)
|
||||||
local ba = floor(0xff * a) % 0x100 * 0x1000000
|
local r = rshift(band(argb, 0x00ff0000), 16) / 255
|
||||||
return colour.pack_rgb(r, g, b) + ba
|
local g = rshift(band(argb, 0x0000ff00), 8) / 255
|
||||||
end
|
local b = rshift(band(argb, 0x000000ff), 0) / 255
|
||||||
|
local a = rshift(band(argb, 0xff000000), 24) / 255
|
||||||
|
return r, g, b, a
|
||||||
|
end
|
||||||
|
|
||||||
function colour.unpack_argb(argb)
|
--rgba format
|
||||||
local r, g, b = colour.unpack_rgb(argb)
|
function colour.pack_rgba(r, g, b, a)
|
||||||
local a = floor(argb / 0x1000000) % 0x100
|
local br = lshift(band(0xff, r * 255), 24)
|
||||||
return r, g, b, a / 255
|
local bg = lshift(band(0xff, g * 255), 16)
|
||||||
end
|
local bb = lshift(band(0xff, b * 255), 8)
|
||||||
|
local ba = lshift(band(0xff, a * 255), 0)
|
||||||
|
return bor( br, bg, bb, ba )
|
||||||
|
end
|
||||||
|
|
||||||
--rgba format
|
function colour.unpack_rgba(rgba)
|
||||||
|
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
|
||||||
|
else
|
||||||
|
--we don't have bitops, use a slower pure-float path
|
||||||
|
local floor = math.floor
|
||||||
|
|
||||||
function colour.pack_rgba(r, g, b, a)
|
--rgb only (no alpha)
|
||||||
local ba = floor(0xff * a) % 0x100
|
function colour.pack_rgb(r, g, b)
|
||||||
return colour.pack_rgb(r, g, b) * 0x100 + ba
|
local br = floor(0xff * r) % 0x100 * 0x10000
|
||||||
end
|
local bg = floor(0xff * g) % 0x100 * 0x100
|
||||||
|
local bb = floor(0xff * b) % 0x100
|
||||||
|
return br + bg + bb
|
||||||
|
end
|
||||||
|
|
||||||
function colour.unpack_rgba(rgba)
|
function colour.unpack_rgb(rgb)
|
||||||
local r, g, b = colour.unpack_rgb(floor(rgba / 0x100))
|
local r = floor(rgb / 0x10000) % 0x100
|
||||||
local a = floor(rgba) % 0x100
|
local g = floor(rgb / 0x100) % 0x100
|
||||||
return r, g, b, a
|
local b = floor(rgb) % 0x100
|
||||||
|
return r / 255, g / 255, b / 255
|
||||||
|
end
|
||||||
|
|
||||||
|
--argb format (common for shared hex)
|
||||||
|
function colour.pack_argb(r, g, b, a)
|
||||||
|
local ba = floor(0xff * a) % 0x100 * 0x1000000
|
||||||
|
return colour.pack_rgb(r, g, b) + ba
|
||||||
|
end
|
||||||
|
|
||||||
|
function colour.unpack_argb(argb)
|
||||||
|
local r, g, b = colour.unpack_rgb(argb)
|
||||||
|
local a = floor(argb / 0x1000000) % 0x100
|
||||||
|
return r, g, b, a / 255
|
||||||
|
end
|
||||||
|
|
||||||
|
--rgba format
|
||||||
|
function colour.pack_rgba(r, g, b, a)
|
||||||
|
local ba = floor(0xff * a) % 0x100
|
||||||
|
return colour.pack_rgb(r, g, b) * 0x100 + ba
|
||||||
|
end
|
||||||
|
|
||||||
|
function colour.unpack_rgba(rgba)
|
||||||
|
local r, g, b = colour.unpack_rgb(floor(rgba / 0x100))
|
||||||
|
local a = floor(rgba) % 0x100
|
||||||
|
return r, g, b, a
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------
|
||||||
-- colour space conversion
|
-- colour space conversion
|
||||||
-- rgb is the common language but it's useful to have other spaces to work in
|
-- rgb is the common language for computers
|
||||||
-- for us as humans :)
|
-- but it's useful to have other spaces to work in for us as humans :)
|
||||||
|
|
||||||
--convert hsl to rgb
|
--convert hsl to rgb
|
||||||
--all components are 0-1, hue is fraction of a turn rather than degrees or radians
|
--all components are 0-1, hue is fraction of a turn rather than degrees or radians
|
||||||
@ -115,11 +170,10 @@ function colour.rgb_to_hsl(r, g, b)
|
|||||||
else
|
else
|
||||||
h = (r - g) / d + 4
|
h = (r - g) / d + 4
|
||||||
end
|
end
|
||||||
assert(h)
|
|
||||||
return h / 6, s, l
|
return h / 6, s, l
|
||||||
end
|
end
|
||||||
|
|
||||||
--todo: hsv, other colour spaces
|
--todo: hsv
|
||||||
|
|
||||||
--oklab https://bottosson.github.io/posts/oklab/
|
--oklab https://bottosson.github.io/posts/oklab/
|
||||||
function colour.oklab_to_rgb(l, a, b)
|
function colour.oklab_to_rgb(l, a, b)
|
||||||
|
Loading…
Reference in New Issue
Block a user