Module:Color2dec: Difference between revisions
From Neodyland Wiki
More actions
m test |
m -link |
||
| Line 8: | Line 8: | ||
-- | -- | ||
local function hexdec ( arg ) | local function hexdec ( arg ) | ||
local dig = ( string.upper ( string.char ( arg ) ) ); | local dig = ( string.upper ( string.char ( arg ) ) ); | ||
| Line 44: | Line 27: | ||
end | end | ||
return dec; | return dec; | ||
end | end | ||
Revision as of 09:11, 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 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.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;