Module:Color2dec: Difference between revisions
From Neodyland Wiki
More actions
m +color |
m +function |
||
| Line 8: | Line 8: | ||
local p = {} | local p = {} | ||
-- | -- local / global function: convert h → d: #rrggbb or #rgb to dec_table {rr, gg, bb} | ||
local function convert ( arg ) | local function convert ( arg ) | ||
local hex = arg or '#000'; | local hex = arg or '#000'; | ||
if mw.ustring.sub ( hex, 1, 1) ~= '#' then | if mw.ustring.sub ( hex, 1, 1) ~= '#' then | ||
error ('value "' .. hex .. '" cannot be converted to decimal') | error ('value "' .. hex .. '" cannot be converted to decimal') | ||
end | end | ||
local dec = {}; | local dec = {}; | ||
if #hex == | 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 | |||
end | |||
return dec; | return dec; | ||
end | end -- function convert | ||
-- global=args[1], local=frame | -- global=args[1], local=frame | ||
| Line 116: | Line 100: | ||
end | end | ||
---- Global functions -------------- | |||
-- | |||
-- global with 2 parms | -- global with 2 parms | ||
function p.tbcgen ( frame ) | function p.tbcgen ( frame ) | ||
| Line 140: | Line 117: | ||
end | end | ||
-- | -- for template: ColorString | ||
-- two different possibilities for parent parameter input: | -- two different possibilities for parent parameter input: | ||
-- first parameter has the format aaa/bbb/ccc... (also with html-reserved characters!) | -- first parameter has the format aaa/bbb/ccc... (also with html-reserved characters!) | ||
-- second parameter: | -- second parameter: the name of the template to be performed (default: Igen/cbx) | ||
-- or | -- or | ||
-- parameters in the format aaa|bbb|ccc... (allows to specify items like URLs with slashes) | -- 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... | -- 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 pparms[2] 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 | |||
function p. | -- global function colortst (1 param) | ||
local args | function p.colorts ( frame ) | ||
local | local args = frame.args; | ||
local | local hcod = p.tincture ( args[1] ); | ||
local dtab = convert ( hcod ) | |||
if | 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 | |||
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; | return p; | ||
Revision as of 08:25, 7 September 2020
This documentation is transcluded from Module:Color2dec/doc.
--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 pparms[2] 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;