Module:Color2dec/sandbox
Jump to navigation
Jump to search
CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules
Lua
Documentation for this module may be created at Module:Color2dec/sandbox/doc
Code
--The function "tincture" converts valid tincture names to hexadecimal colorcodes
-- e.g. 'argent' will give '#FFF'; when not found it tries afterwards with module:convCSS
--The function "convert" converts a valid hexadecimal colorcode into three decimal numbers:
-- #bbccdd gives 187|204|221, 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: converts h → d: #rrggbb or #rgb to table {rr, gg, bb}
function convert ( frame )
local hexv = mw.text.trim( frame ) or '000';
if mw.ustring.sub ( hexv, 1, 1 ) ~= '#' then
error ('value "' .. hex .. '" cannot be converted to decimal')
end
local dec = {};
for i = 1, 3 do
if #hexv == 4 then
dec [i] = tonumber ( mw.ustring.sub (hexv, i+1, i+1)..mw.ustring.sub (hexv, i+1, i+1), 16 )
else
dec [i] = tonumber ( mw.ustring.sub (hexv, 2*i, 2*i + 1), 16 )
end
end
return dec;
end -- function convert
-- global=args[1], local=frame
function p.tincture ( frame )
local args = frame.args
local inp = ''
if args then
inp = args[1] -- 'glo'
elseif frame then
inp = frame -- 'loc'
end
local low = ( string.lower ( mw.text.trim ( inp ) ) );
if mw.ustring.sub ( inp, 1, 1 ) == '#' then
if #low == 4 then
return inp;
elseif mw.ustring.sub ( low, 2, 2 ) == mw.ustring.sub ( low, 3, 3 )
and mw.ustring.sub ( low, 4, 4 ) == mw.ustring.sub ( low, 5, 5 )
and mw.ustring.sub ( low, 6, 6 ) == mw.ustring.sub ( low, 7, 7 ) then
local low = ( string.upper ( inp ) );
return '#' .. mw.ustring.sub ( low, 2, 2 )
.. mw.ustring.sub ( low, 4, 4 )
.. mw.ustring.sub ( low, 6, 6 );
else
return inp;
end
end
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 == 'céleste' then out = '#89C5E3'
elseif low == 'celeste' then out = '#89C5E3'
elseif low == 'cendrée' then out = '#999'
elseif low == 'gules' then out = '#DA121A'
elseif low == 'naranja' then out = '#EB7711'
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'
-- colours less common:
elseif low == 'brunâtre' then out = '#3F1FFF'
elseif low == 'sanguine' then out = '#BC3F4A'
elseif low == 'murrey' then out = '#C54B8C'
elseif low == 'orange_t' then out = '#FC7A11'
else
local convc = require( "Module:convCSS" )
out = convc.main ( low )
end
return out;
end
-- global=args[1], local=frame
function p.decode ( frame )
local args = frame.args
local str = ''
if args then
str = mw.text.trim ( args [1], charset )
elseif frame then
str = mw.text.trim ( frame, charset )
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 1 parm
function p.tindec ( frame )
local args = frame.args;
local hcod = p.tincture ( args[1] );
return p.decode ( hcod );
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
--global init for e.g. ColorString
-- two different possibilities for parent parameter input:
-- first parameter has the format aaa/bbb/ccc... (also with html-reserved characters!)
-- second parameter: a template name
-- or
-- parameters in the format aaa|bbb|ccc... (allows to specify items like URLs with slashes)
-- returns [to the template] the parameters in the format aaa|bbb|ccc...
function p.ppar ( frame )
local args = mw.getCurrentFrame(): getParent().args;
local argstr = mw.text.trim( args[1] ) or 'aa/bb/cc';
local tmplte = mw.text.trim( args[2] or 'Igen/cbx' );
local argt = mw.text.split( argstr, "/" );
return mw.getCurrentFrame(): expandTemplate { title = tmplte, args = argt };
end
return p;