Module:Color2dec: Difference between revisions
From Neodyland Wiki
More actions
m test |
m corr |
||
| Line 1: | Line 1: | ||
-- | --The function "tincture" converts valid tincture names to hexadecimal colorcode | ||
-- e.g. 'argent' will give '#FFF' | |||
--The function "convert" converts a valid hexadecimal colorcode into three decimal numbers: | |||
-- #bbccdd gives 187|187|204, while #FF0 will give 255|255|0 | -- #bbccdd gives 187|187|204, while #FF0 will give 255|255|0 | ||
--The function "tbcgen" generates a complete 5-parameter string | --The function "tbcgen" generates a complete 5-parameter string | ||
| Line 38: | Line 40: | ||
end | end | ||
-- global=args[1], local=frame | |||
-- | |||
function p.tincture ( frame ) | function p.tincture ( frame ) | ||
local args = frame.args | |||
local inp = '' | |||
if args then | |||
inp = args[1] or 'glo' | |||
elseif frame then | |||
inp = frame or 'loc' | |||
-- else inp = 'Tinct wo' | |||
end | |||
if mw.ustring.sub ( inp, 1, 1 ) == '#' then | if mw.ustring.sub ( inp, 1, 1 ) == '#' then | ||
return inp; | return inp; | ||
end | end | ||
local low = ( string.lower ( inp ) ); | local low = ( string.lower ( inp ) ); | ||
local out = ''; | local out = ''; | ||
| Line 79: | Line 67: | ||
elseif low == 'tawny' then out = '#9D5333' | elseif low == 'tawny' then out = '#9D5333' | ||
elseif low == 'vert' then out = '#078930' | elseif low == 'vert' then out = '#078930' | ||
else out = inp | else out = '#abcdef' .. inp | ||
end | end | ||
return out; | return out; | ||
end | end | ||
-- | -- global=args[1], local=frame | ||
function p.decode ( frame ) | function p.decode ( frame ) | ||
local args = frame.args | |||
local str = '' | |||
if args then | |||
str = args[1] or 'glo' | |||
elseif frame then | |||
str = frame or 'loc' | |||
-- else str = 'decode wo' | |||
end | |||
if mw.ustring.sub ( str, 1, 1 ) == '#' then | if mw.ustring.sub ( str, 1, 1 ) == '#' then | ||
return '#' .. mw.ustring.sub( str, 2, #str ); | return '#' .. mw.ustring.sub( str, 2, #str ); | ||
| Line 96: | Line 90: | ||
elseif mw.ustring.sub ( str, 1, 1 ) == ';' then | elseif mw.ustring.sub ( str, 1, 1 ) == ';' then | ||
return ';' .. mw.ustring.sub( str, 2, #str ); | return ';' .. mw.ustring.sub( str, 2, #str ); | ||
else | else | ||
return str; | return str; | ||
| Line 104: | Line 96: | ||
-- | -- global with 2 parms | ||
function p.tbcgen ( frame ) | function p.tbcgen ( frame ) | ||
local args = frame.args; | local args = frame.args; | ||
local arg1 = args[1] or '#000000'; | |||
local tbctab = {}; | local tbctab = {}; | ||
local hexcod = ''; | |||
hexcod = p.tincture ( arg1 ); | |||
decval = convert ( hexcod ); | decval = convert ( hexcod ); | ||
tbctab [1] = '0'; | tbctab [1] = '0'; | ||
| Line 115: | Line 108: | ||
tbctab [3] = decval [2]; | tbctab [3] = decval [2]; | ||
tbctab [4] = decval [3]; | tbctab [4] = decval [3]; | ||
tbctab [5] = args[2] or 'couleur'; | tbctab [5] = p.decode ( args[2] or 'couleur' ); | ||
return mw.getCurrentFrame():expandTemplate{ title = 'Tbc', args = tbctab }; | return mw.getCurrentFrame():expandTemplate{ title = 'Tbc', args = tbctab }; | ||
end | end | ||
return p; | return p; | ||
Revision as of 11:11, 28 August 2019
This documentation is transcluded from Module:Color2dec/doc.
--The function "tincture" converts valid tincture names to hexadecimal colorcode
-- e.g. 'argent' will give '#FFF'
--The function "convert" converts a valid hexadecimal colorcode into three decimal numbers:
-- #bbccdd gives 187|187|204, while #FF0 will give 255|255|0
--The function "tbcgen" generates a complete 5-parameter string
--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 = tonumber (dig)
end
return dec;
end
--
local function convert ( arg )
local hex = arg or '#000';
local dec = {};
if #hex == 7 then
dec [1] = hexdec ( string.byte (hex, 2)) * 16 + hexdec (string.byte (hex, 3))
dec [2] = hexdec ( string.byte (hex, 4)) * 16 + hexdec (string.byte (hex, 5))
dec [3] = hexdec ( string.byte (hex, 6)) * 16 + hexdec (string.byte (hex, 7))
else
dec [1] = hexdec ( string.byte (hex, 2)) * 16 + hexdec (string.byte (hex, 2))
dec [2] = hexdec ( string.byte (hex, 3)) * 16 + hexdec (string.byte (hex, 3))
dec [3] = hexdec ( string.byte (hex, 4)) * 16 + hexdec (string.byte (hex, 4))
end
return dec;
end
-- global=args[1], local=frame
function p.tincture ( frame )
local args = frame.args
local inp = ''
if args then
inp = args[1] or 'glo'
elseif frame then
inp = frame or 'loc'
-- else inp = 'Tinct wo'
end
if mw.ustring.sub ( inp, 1, 1 ) == '#' then
return inp;
end
local low = ( string.lower ( inp ) );
local out = '';
if low == 'argent' then out = '#FFF'
elseif low == 'argent-d' then out = '#E7E7E7'
elseif low == 'azure' then out = '#0F47AF'
elseif low == 'carnation' then out = '#F2A772'
elseif low == 'celeste' then out = '#89C5E3'
elseif low == 'cendrée' then out = '#999'
elseif low == 'gules' then out = '#DA121A'
elseif low == 'or' then out = '#FCDD09'
elseif low == 'purpure' then out = '#9116A1'
elseif low == 'sable' then out = '#000'
elseif low == 'tawny' then out = '#9D5333'
elseif low == 'vert' then out = '#078930'
else out = '#abcdef' .. inp
end
return out;
end
-- global=args[1], local=frame
function p.decode ( frame )
local args = frame.args
local str = ''
if args then
str = args[1] or 'glo'
elseif frame then
str = frame or 'loc'
-- else str = 'decode wo'
end
if mw.ustring.sub ( str, 1, 1 ) == '#' then
return '#' .. mw.ustring.sub( str, 2, #str );
elseif mw.ustring.sub ( str, 1, 1 ) == '*' then
return '*' .. mw.ustring.sub( str, 2, #str );
elseif mw.ustring.sub ( str, 1, 1 ) == ':' then
return ':' .. mw.ustring.sub( str, 2, #str );
elseif mw.ustring.sub ( str, 1, 1 ) == ';' then
return ';' .. mw.ustring.sub( str, 2, #str );
else
return str;
end
end
-- global with 2 parms
function p.tbcgen ( frame )
local args = frame.args;
local arg1 = args[1] or '#000000';
local tbctab = {};
local hexcod = '';
hexcod = p.tincture ( arg1 );
decval = convert ( hexcod );
tbctab [1] = '0';
tbctab [2] = decval [1];
tbctab [3] = decval [2];
tbctab [4] = decval [3];
tbctab [5] = p.decode ( args[2] or 'couleur' );
return mw.getCurrentFrame():expandTemplate{ title = 'Tbc', args = tbctab };
end
return p;