Module:Color2dec
Written a bit complicated, due to my poor LUA knowledge.
This module contains global functions, some functions which can be used either global or local, and local functions.
Global function "tbcgen"
accepts & needs two parameters, a color code and a descriptive text.
- the color code may be a valid hexadecimal color triplet, as e.g."#ABCDEF" or "#80c";
otherwise it is treated with the local/global function "tincture";
- it may also be a valid tincture name, e.g. "céleste" (see examples)
- from the many other color names just the set of CSS colors is valid for conversion;
- when a string not starting with "#" cannot find its hexadecimal representation, an error is raised.
- when the descriptive text starts with e.g. "#" that becomes replaced by its decimal numeric character reference
by the local/global function "decode".
Function "convert"
Converts a hexadecimal string (#rrggbb or #rgb) to a table of three decimal values (see also the dec·hex function of template:D2x).
Function "tincture"
Used also globally by template Igen/cbox
Converts the standard tinctures names to their hexadecimal color codes;
Color codes of six hexadecimal numbers #aabbcc are shortened to #ABC when possible (see also template:D2s).
- when not successful, the conversion of CSS names is tried;
- other values are passed without check as-they-are, which may cause an error
Function "decode"
Used also globally by template Igen/cbox and by other templates (same code as in module Char)
Avoids the wiki problem with # * : ; at the first position
- Global functions:
Global function "tbcgen"
"tbcgen" calls the template Tbc with the 5 parameters needed by that template.
Global function "cstring"
Global init for e.g. ColorString, can split slash-separated params
Global function "colorts"
Testfunction for drawing boxes, used at Template talk:ColorString.
Global function "colortab"
Generates a 16 × 16 × 16 tabla, shown at Module talk:Color2dec
--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 / global function: convert h → d: #rrggbb or #rgb to dec_table {rr, gg, bb}
local function convert ( arg )
local hex = arg or '#000';
if mw.ustring.sub ( hex, 1, 1) ~= '#' then
error ('value "' .. hex .. '" cannot be converted to decimal')
end
local dec = {};
for i = 1, 3 do
if #hex == 4 then
dec [i] = tonumber ( string.sub (hex, i+1, i+1)..string.sub (hex, i+1, i+1), 16 )
else
dec [i] = tonumber ( mw.ustring.sub (hex, 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] or 'glo'
elseif frame then
inp = frame or 'loc'
-- else inp = 'Tinct wo'
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 functions --------------
-- 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
-- for template: ColorString
-- two different possibilities for parent parameter input:
-- first parameter has the format aaa/bbb/ccc... (also with html-reserved characters!)
-- second parameter: the name of the template to be performed (default: Igen/cbx)
-- or
-- parameters in the format aaa|bbb|ccc... (allows to specify items like URLs with slashes)
-- In any case the template to be performed can be specified with "t="
-- returns [to the template] the parameters in the format aaa|bbb|ccc...
-- parent-global parms "ppar" for ColorString
function p.cstring ( frame )
local pparms = mw.getCurrentFrame(): getParent().args;
local argstr = mw.text.trim( pparms[1] ) or 'aa/bb/cc';
local argtit = mw.text.trim( pparms.t or 'Igen/cbx' );
local argtab = mw.text.split( argstr, "/" );
if argtab[1] == pparms[1] then
argtab = pparms
end
return mw.getCurrentFrame(): expandTemplate { title = argtit, args = argtab };
end -- function cstring
-- global function colortst (1 param)
function p.colorts ( frame )
local args = frame.args;
local hcod = p.tincture ( args[1] );
local dtab = convert ( hcod )
local xsum = dtab [1] + dtab [2] + dtab [3]
local par1 = 'b'
local tsum = tostring ( xsum )
if #tsum < 3 then tsum = '0' .. tsum end
if #tsum < 3 then tsum = '0' .. tsum end
tsum = ' '..tsum..' '
if xsum < 408 then par1 = 'w' end
local otab = {}
table.insert(otab, frame:expandTemplate{ title = 'Color', args = { par1, tsum , hcod, hcod } });
return table.concat (otab) -- args[1]
end -- function colorts
--------------------------------------------------------------------------------
-- function of Ud:PerfektesChaos (layout: Ud:Sarang),
-- generates a table of 16 × 16 × 16 = 4096 elements
function p.colortab ()
local iR, iG, iB, hR, hG, hB, h, s, g
local r = "___NOTOC__"
for iR = 0, 15 do
hR = string.format( "%X", iR )
r = r .. "\n=== " .. hR .. "xx"
r = r .. " ===\n<table width='100%' border='0' style='text-align:center'>"
for iG = 0, 15 do
hG = string.format( "%X", iG )
r = r .. "\n<tr>"
for iB = 0, 15 do
hB = string.format( "%X", iB )
h = hR .. hG .. hB
-- s = contrast( '#'..h )
-- s = '#' .. string.format( "%X", 15 - iR ) .. string.format( "%X", 15 - iG ) .. string.format( "%X", 15 - iB )
-- if iR * 2 + iG * 7 + iB < 75
-- if iR * 3 + iG * 10 < 98
if iR + iG + iB < 24
then s = "FFF" else s = "000" end
g = h
if mw.ustring.find("0369CF", hR) ~= nil and
mw.ustring.find("0369CF", hG) ~= nil and
mw.ustring.find("0369CF", hB) ~= nil then
g = "'''" .. g .. "'''"
end
r = r .. string.format( "\n<td title='#%s' style='background:#%s;color:#%s'>%s</td>", h, h, s, g )
end -- for iB
r = r .. "\n</tr>"
end -- for iG
r = r .. "\n</table>"
end -- for iR
return r
end -- function colortab
return p;