Module:Color2dec: Difference between revisions
From Neodyland Wiki
More actions
m test |
m test |
||
| Line 7: | Line 7: | ||
local p = {} | local p = {} | ||
-- | |||
local function getParameters( frame_args, arg_list ) | local function getParameters( frame_args, arg_list ) | ||
local get_args = {}; | local get_args = {}; | ||
| Line 64: | Line 64: | ||
end | end | ||
-- | -- | ||
function p.tbcgen ( | function p.tbcgen ( frame ) | ||
local args = frame.args | |||
local tbctab = {}; | local tbctab = {}; | ||
tbctab [1] = '0'; | tbctab [1] = '0'; | ||
tbctab [5] = | tbctab [5] = args[2] or 'colour'; | ||
local hexcod = | local hexcod = args[1] or '#000000'; | ||
if #hexcod == 7 then | if #hexcod == 7 then | ||
tbctab [2] = hexdec( string.byte (hexcod, 2)) * 16 + hexdec (string.byte (hexcod, 3)) | tbctab [2] = hexdec( string.byte (hexcod, 2)) * 16 + hexdec (string.byte (hexcod, 3)) | ||
Revision as of 08:52, 25 August 2019
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 ( frame )
local args = frame.args
local hexcod = args[1] 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 ( frame )
local args = frame.args
local tbctab = {};
tbctab [1] = '0';
tbctab [5] = args[2] or 'colour';
local hexcod = args[1] or '#000000';
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;