Module:Color2dec
From Neodyland Wiki
More actions
This documentation is transcluded from Module:Color2dec/doc.
--This function converts a valid hexadecimal colorcode into three decimal numbers:
-- #bbccdd gives 187|187|204, while #FF0 will give 255|255|0
--Invalid color codes will give invalid or unpredictable results, or LUA errors.
local p = {}
local function getParameters( frame_args, arg_list )
local get_args = {};
local index = 1;
local value;
for i,arg in ipairs( arg_list ) do
value = frame_args[arg]
if value == nil then
value = frame_args[index];
index = index + 1;
end
get_args[arg] = value;
end
return get_args;
end
local function hexdec ( arg )
local dig = ( string.upper ( string.char ( arg ) ) );
local dec = 0;
if dig == 'F' then
dec = 0xF
elseif dig == 'E' then
dec = 0xE
elseif dig == 'D' then
dec = 0xD
elseif dig == 'C' then
dec = 0xC
elseif dig == 'B' then
dec = 0xB
elseif dig == 'A' then
dec = 0xA
else
dec = dig
end
return dec;
end
--
function p.hexval ( inp )
local color = getParameters( inp.args, { 'colcod' } );
local hexcod = color['colcod'] or '#000000';
local decval = {};
if #hexcod == 7 then
decval [1] = hexdec( string.byte (hexcod, 2)) * 16 + hexdec (string.byte (hexcod, 3))
decval [2] = hexdec( string.byte (hexcod, 4)) * 16 + hexdec (string.byte (hexcod, 5))
decval [3] = hexdec( string.byte (hexcod, 6)) * 16 + hexdec (string.byte (hexcod, 7))
else
decval [1] = hexdec( string.byte (hexcod, 2)) * 16 + hexdec (string.byte (hexcod, 2))
decval [2] = hexdec( string.byte (hexcod, 3)) * 16 + hexdec (string.byte (hexcod, 3))
decval [3] = hexdec( string.byte (hexcod, 4)) * 16 + hexdec (string.byte (hexcod, 4))
end
return decval;
end
--
function p.tbcgen ( inp )
local param = getParameters( inp.args, { 'colcod', 'descrp' } );
local tbctab = {};
tbctab [1] = '0';
tbctab [5] = param['descrp'] or 'colour';
local hexcod = param['colcod'] or '#000000';
local decval = {};
if #hexcod == 7 then
tbctab [2] = hexdec( string.byte (hexcod, 2)) * 16 + hexdec (string.byte (hexcod, 3))
tbctab [3] = hexdec( string.byte (hexcod, 4)) * 16 + hexdec (string.byte (hexcod, 5))
tbctab [4] = hexdec( string.byte (hexcod, 6)) * 16 + hexdec (string.byte (hexcod, 7))
else
tbctab [2] = hexdec( string.byte (hexcod, 2)) * 16 + hexdec (string.byte (hexcod, 2))
tbctab [3] = hexdec( string.byte (hexcod, 3)) * 16 + hexdec (string.byte (hexcod, 3))
tbctab [4] = hexdec( string.byte (hexcod, 4)) * 16 + hexdec (string.byte (hexcod, 4))
end
return mw.getCurrentFrame():expandTemplate{ title = 'Tbc', args = tbctab };
end
return p;