1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 11:40:29 +00:00

Split colours.rgb8 into colours.packRGB and colours.unpackRGB

This commit is contained in:
Lignum 2017-09-04 16:25:40 +02:00 committed by SquidDev
parent 070fd1f2ff
commit af01b9514b
2 changed files with 29 additions and 15 deletions

View File

@ -48,22 +48,36 @@ function test( colors, color )
if type( color ) ~= "number" then if type( color ) ~= "number" then
error( "bad argument #2 (expected number, got " .. type( color ) .. ")", 2 ) error( "bad argument #2 (expected number, got " .. type( color ) .. ")", 2 )
end end
return ((bit32.band(colors, color)) == color) return bit32.band(colors, color) == color
end
function packRGB( r, g, b )
if type( r ) ~= "number" then
error( "bad argument #1 (expected number, got " .. type( r ) .. ")", 2 )
end
if type( g ) ~= "number" then
error( "bad argument #2 (expected number, got " .. type( g ) .. ")", 2 )
end
if type( b ) ~= "number" then
error( "bad argument #3 (expected number, got " .. type( b ) .. ")", 2 )
end
return
bit32.band( r, 0xFF ) * 2^16 +
bit32.band( g, 0xFF ) * 2^8 +
bit32.band( b, 0xFF )
end
function unpackRGB( rgb )
if type( rgb ) ~= "number" then
error( "bad argument #1 (expected number, got " .. type( rgb ) .. ")", 2 )
end
return bit32.band( bit32.rshift( rgb, 16 ), 0xFF ), bit32.band( bit32.rshift( rgb, 8 ), 0xFF ), bit32.band( rgb, 0xFF )
end end
function rgb8( r, g, b ) function rgb8( r, g, b )
if type( r ) ~= "number" then if g == nil and b == nil then
error( "bad argument #1 (expected number, got " .. type( r ) .. ")", 2 ) return unpackRGB( r )
elseif type(r) == "number" and g == nil and b == nil then else
return bit32.band( bit32.rshift( r, 16 ), 0xFF ) / 255, bit32.band( bit32.rshift( r, 8 ), 0xFF ) / 255, bit32.band( r, 0xFF ) / 255 return packRGB( r, g, b )
elseif type(r) == "number" and type(g) == "number" and type(b) == "number" then
return
bit32.lshift( bit32.band(r * 255, 0xFF), 16 ) +
bit32.lshift( bit32.band(g * 255, 0xFF), 8 ) +
bit32.band(b * 255, 0xFF)
elseif type( g ) ~= "number" then
error( "bad argument #2 (expected number, got " .. type( g ) .. ")", 2 )
elseif type( b ) ~= "number" then
error( "bad argument #3 (expected number, got " .. type( b ) .. ")", 2 )
end end
end end

View File

@ -298,7 +298,7 @@ function create( parent, nX, nY, nWidth, nHeight, bStartVisible )
local tCol local tCol
if type(r) == "number" and g == nil and b == nil then if type(r) == "number" and g == nil and b == nil then
tCol = { colours.rgb8( r ) } tCol = { colours.unpackRGB( r ) }
tPalette[ colour ] = tCol tPalette[ colour ] = tCol
else else
if type( r ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( r ) .. ")", 2 ) end if type( r ) ~= "number" then error( "bad argument #2 (expected number, got " .. type( r ) .. ")", 2 ) end