Modul:Kontrast boja/boje: razlika između inačica

Izvor: Hrvatska internetska enciklopedija
Prijeđi na navigaciju Prijeđi na pretraživanje
Stvorena nova stranica sa sadržajem: »local p = {} local HTMLcolor = mw.loadData( 'Module:Kontrast boja/boje' ) local function sRGB (v) if (v <= 0.03928) then v = v / 12.92 else v = math.pow((v+0.055)/1.055, 2.4) end return v end local function rgbdec2lum(R, G, B) if ( 0 <= R and R < 256 and 0 <= G and G < 256 and 0 <= B and B < 256 ) then return 0.2126 * sRGB(R/255) + 0.7152 * sRGB(G/255) + 0.0722 * sRGB(B/255) else return '' end end local function hsl2lum(h, s, l)...«.
 
mNema sažetka uređivanja
 
Nisu prikazane 3 međuinačice
Redak 1: Redak 1:
local p = {}
-- ČISTI DATA MODUL – SAMO TABLICA, BEZ FUNKCIJA
local HTMLcolor = mw.loadData( 'Module:Kontrast boja/boje' )


local function sRGB (v)
local t = {
if (v <= 0.03928) then
v = v / 12.92
else
v = math.pow((v+0.055)/1.055, 2.4)
end
return v
end


local function rgbdec2lum(R, G, B)
    ["aliceblue"]=0.9726,["antiquewhite"]=0.8469,["aqua"]=0.7874,["aquamarine"]=0.8079,["azure"]=0.9726,
if ( 0 <= R and R < 256 and 0 <= G and G < 256 and 0 <= B and B < 256 ) then
    ["beige"]=0.9131,["bisque"]=0.8079,["black"]=0,["blanchedalmond"]=0.8469,["blue"]=0.0722,
return 0.2126 * sRGB(R/255) + 0.7152 * sRGB(G/255) + 0.0722 * sRGB(B/255)
    ["blueviolet"]=0.1265,["brown"]=0.098,["burlywood"]=0.5686,["cadetblue"]=0.3647,["chartreuse"]=0.7607,
else
    ["chocolate"]=0.1732,["coral"]=0.348,["cornflowerblue"]=0.4817,["cornsilk"]=0.9466,["crimson"]=0.1604,
return ''
    ["cyan"]=0.7874,["darkblue"]=0.018,["darkcyan"]=0.2159,["darkgoldenrod"]=0.2722,["darkgray"]=0.662,
end
    ["darkgreen"]=0.091,["darkgrey"]=0.662,["darkkhaki"]=0.485,["darkmagenta"]=0.0735,["darkolivegreen"]=0.1427,
end
    ["darkorange"]=0.3904,["darkorchid"]=0.1501,["darkred"]=0.0547,["darksalmon"]=0.4052,["darkseagreen"]=0.567,
    ["darkslateblue"]=0.0837,["darkslategray"]=0.067,["darkslategrey"]=0.067,["darkturquoise"]=0.468,["darkviolet"]=0.1096,
    ["deeppink"]=0.2848,["deepskyblue"]=0.5327,["dimgray"]=0.1413,["dimgrey"]=0.1413,["dodgerblue"]=0.4275,
    ["firebrick"]=0.0992,["floralwhite"]=0.9593,["forestgreen"]=0.1893,["fuchsia"]=0.2848,["gainsboro"]=0.7157,
    ["ghostwhite"]=0.9726,["gold"]=0.6981,["goldenrod"]=0.4192,["gray"]=0.2159,["green"]=0.091,
    ["greenyellow"]=0.8062,["grey"]=0.2159,["honeydew"]=0.9726,["hotpink"]=0.468,["indianred"]=0.214,
    ["indigo"]=0.0314,["ivory"]=0.9726,["khaki"]=0.8062,["lavender"]=0.8469,["lavenderblush"]=0.9131,
    ["lawngreen"]=0.7607,["lemonchiffon"]=0.9131,["lightblue"]=0.6514,["lightcoral"]=0.4052,["lightcyan"]=0.9726,
    ["lightgoldenrodyellow"]=0.9466,["lightgray"]=0.7157,["lightgreen"]=0.6514,["lightgrey"]=0.7157,["lightpink"]=0.8079,
    ["lightsalmon"]=0.468,["lightseagreen"]=0.3647,["lightskyblue"]=0.6514,["lightslategray"]=0.3647,["lightslategrey"]=0.3647,
    ["lightsteelblue"]=0.6514,["lightyellow"]=0.9726,["lime"]=0.7152,["limegreen"]=0.3647,["linen"]=0.9131,
    ["magenta"]=0.2848,["maroon"]=0.0547,["mediumaquamarine"]=0.468,["mediumblue"]=0.044,["mediumorchid"]=0.214,
    ["mediumpurple"]=0.3647,["mediumseagreen"]=0.3647,["mediumslateblue"]=0.3647,["mediumspringgreen"]=0.7152,["mediumturquoise"]=0.468,
    ["mediumvioletred"]=0.1265,["midnightblue"]=0.018,["mintcream"]=0.9726,["mistyrose"]=0.9131,["moccasin"]=0.8469,
    ["navajowhite"]=0.8079,["navy"]=0.018,["oldlace"]=0.9466,["olive"]=0.2159,["olivedrab"]=0.2722,
    ["orange"]=0.3904,["orangered"]=0.2126,["orchid"]=0.4052,["palegoldenrod"]=0.8062,["palegreen"]=0.7152,
    ["paleturquoise"]=0.8469,["palevioletred"]=0.214,["papayawhip"]=0.9131,["peachpuff"]=0.8079,["peru"]=0.2722,
    ["pink"]=0.8469,["plum"]=0.5686,["powderblue"]=0.7152,["purple"]=0.0735,["rebeccapurple"]=0.053,
    ["red"]=0.2126,["rosybrown"]=0.3647,["royalblue"]=0.2642,["saddlebrown"]=0.098,["salmon"]=0.348,
    ["sandybrown"]=0.468,["seagreen"]=0.1893,["seashell"]=0.9466,["sienna"]=0.1732,["silver"]=0.5271,
    ["skyblue"]=0.6514,["slateblue"]=0.214,["slategray"]=0.2642,["slategrey"]=0.2642,["snow"]=0.9726,
    ["springgreen"]=0.7152,["steelblue"]=0.2642,["tan"]=0.5686,["teal"]=0.091,["thistle"]=0.6514,
    ["tomato"]=0.2642,["turquoise"]=0.468,["violet"]=0.5686,["wheat"]=0.8062,["white"]=1,
    ["whitesmoke"]=0.9131,["yellow"]=0.9278,["yellowgreen"]=0.485,


local function hsl2lum(h, s, l)
}
if ( 0 <= h and h < 360 and 0 <= s and s <= 1 and 0 <= l and l <= 1 ) then
local c = (1 - math.abs(2*l - 1))*s
local x = c*(1 - math.abs( math.fmod(h/60, 2) - 1) )
local m = l - c/2


local r, g, b = m, m, m
return t
if( 0 <= h and h < 60 ) then
r = r + c
g = g + x
elseif( 60 <= h and h < 120 ) then
r = r + x
g = g + c
elseif( 120 <= h and h < 180 ) then
g = g + c
b = b + x
elseif( 180 <= h and h < 240 ) then
g = g + x
b = b + c
elseif( 240 <= h and h < 300 ) then
r = r + x
b = b + c
elseif( 300 <= h and h < 360 ) then
r = r + c
b = b + x
end
return rgbdec2lum(255*r, 255*g, 255*b)
else
return ''
end
end
 
local function color2lum(c)
 
if (c == nil) then
return ''
end
 
-- html '#' entity
c = c:gsub("&#35;", "#")
 
-- whitespace
c = c:match( '^%s*(.-)[%s;]*$' )
 
-- unstrip nowiki strip markers
c = mw.text.unstripNoWiki(c)
 
-- lowercase
c = c:lower()
 
-- first try to look it up
local L = HTMLcolor[c]
if (L ~= nil) then
return L
end
 
-- convert from hsl
if mw.ustring.match(c,'^hsl%([%s]*[0-9][0-9%.]*[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*%)$') then
local h, s, l = mw.ustring.match(c,'^hsl%([%s]*([0-9][0-9%.]*)[%s]*,[%s]*([0-9][0-9%.]*)%%[%s]*,[%s]*([0-9][0-9%.]*)%%[%s]*%)$')
return hsl2lum(tonumber(h), tonumber(s)/100, tonumber(l)/100)
end
 
-- convert from rgb
if mw.ustring.match(c,'^rgb%([%s]*[0-9][0-9]*[%s]*,[%s]*[0-9][0-9]*[%s]*,[%s]*[0-9][0-9]*[%s]*%)$') then
local R, G, B = mw.ustring.match(c,'^rgb%([%s]*([0-9][0-9]*)[%s]*,[%s]*([0-9][0-9]*)[%s]*,[%s]*([0-9][0-9]*)[%s]*%)$')
return rgbdec2lum(tonumber(R), tonumber(G), tonumber(B))
end
 
-- convert from rgb percent
if mw.ustring.match(c,'^rgb%([%s]*[0-9][0-9%.]*%%[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*%)$') then
local R, G, B = mw.ustring.match(c,'^rgb%([%s]*([0-9][0-9%.]*)%%[%s]*,[%s]*([0-9][0-9%.]*)%%[%s]*,[%s]*([0-9][0-9%.]*)%%[%s]*%)$')
return rgbdec2lum(255*tonumber(R)/100, 255*tonumber(G)/100, 255*tonumber(B)/100)
end
 
-- remove leading # (if there is one) and whitespace
c = mw.ustring.match(c, '^[%s#]*([a-f0-9]*)[%s]*$')
 
-- split into rgb
local cs = mw.text.split(c or '', '')
if( #cs == 6 ) then
local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[2])
local G = 16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[4])
local B = 16*tonumber('0x' .. cs[5]) + tonumber('0x' .. cs[6])
 
return rgbdec2lum(R, G, B)
elseif ( #cs == 3 ) then
local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[1])
local G = 16*tonumber('0x' .. cs[2]) + tonumber('0x' .. cs[2])
local B = 16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[3])
 
return rgbdec2lum(R, G, B)
end
 
-- failure, return blank
return ''
end
 
-- This exports the function for use in other modules.
-- The colour is passed as a string.
function p._lum(color)
return color2lum(color)
end
 
function p._jacikontrast(args)
local bias = tonumber(args['bias'] or '0') or 0
local css = (args['css'] and args['css'] ~= '') and true or false
local v1 = color2lum(args[1] or '')
local c2 = args[2] or '#FFFFFF'
local v2 = color2lum(c2)
local c3 = args[3] or '#000000'
local v3 = color2lum(c3)
local ratio1 = -1;
local ratio2 = -1;
if (type(v1) == 'number' and type(v2) == 'number') then
ratio1 = (v2 + 0.05)/(v1 + 0.05)
ratio1 = (ratio1 < 1) and 1/ratio1 or ratio1
end
if (type(v1) == 'number' and type(v3) == 'number') then
ratio2 = (v3 + 0.05)/(v1 + 0.05)
ratio2 = (ratio2 < 1) and 1/ratio2 or ratio2
end
 
if css then
local c1 = args[1] or ''
if mw.ustring.match(c1, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') or
mw.ustring.match(c1, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') then
c1 = '#' .. c1
end
if mw.ustring.match(c2, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') or
mw.ustring.match(c2, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') then
c2 = '#' .. c2
end
if mw.ustring.match(v3, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') or
mw.ustring.match(v3, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') then
c3 = '#' .. c3
end
return 'background-color:' .. c1 .. '; color:' .. ((ratio1 > 0) and (ratio2 > 0) and ((ratio1 + bias > ratio2) and c2 or c3) or '') .. ';'
end
 
return (ratio1 > 0) and (ratio2 > 0) and ((ratio1 + bias > ratio2) and c2 or c3) or ''
end
 
function p._ratio(args)
local v1 = color2lum(args[1])
local v2 = color2lum(args[2])
if (type(v1) == 'number' and type(v2) == 'number') then
-- v1 should be the brighter of the two.
if v2 > v1 then
v1, v2 = v2, v1
end
return (v1 + 0.05)/(v2 + 0.05)
else
return args['error'] or '?'
end
end
 
function p._styleratio(args)
local style = (args[1] or ''):lower()
local bg, fg = 'white', 'black'
local lum_bg, lum_fg = 1, 0
 
if args[2] then
local lum = color2lum(args[2])
if lum ~= '' then bg, lum_bg = args[2], lum end
end
if args[3] then
local lum = color2lum(args[3])
if lum ~= '' then fg, lum_fg = args[3], lum end
end
 
local slist = mw.text.split(mw.ustring.gsub(mw.ustring.gsub(style or '', '&#[Xx]23;', '#'), '&#35;', '#'), ';')
for k = 1,#slist do
local s = slist[k]
local k,v = s:match( '^[%s]*([^:]-):([^:]-)[%s;]*$' )
k = k or ''
v = v or ''
if (k:match('^[%s]*(background)[%s]*$') or k:match('^[%s]*(background%-color)[%s]*$')) then
local lum = color2lum(v)
if( lum ~= '' ) then bg, lum_bg = v, lum end
elseif (k:match('^[%s]*(color)[%s]*$')) then
local lum = color2lum(v)
if( lum ~= '' ) then bg, lum_fg = v, lum end
end
end
if lum_bg > lum_fg then
return (lum_bg + 0.05)/(lum_fg + 0.05)
else
return (lum_fg + 0.05)/(lum_bg + 0.05)
end
end
 
--[[
Use {{#invoke:Kontrast boja|somecolor}} directly or
{{#invoke:Kontrast boja}} from a wrapper template.
 
Parameters:
-- |1= — required; A color to check.
--]]
function p.lum(frame)
local color = frame.args[1] or frame:getParent().args[1]
return p._lum(color)
end
 
function p.ratio(frame)
local args = frame.args[1] and frame.args or frame:getParent().args
return p._ratio(args)
end
 
function p.styleratio(frame)
local args = frame.args[1] and frame.args or frame:getParent().args
return p._styleratio(args)
end
 
function p.jacikontrast(frame)
local args = frame.args[1] and frame.args or frame:getParent().args
return p._jacikontrast(args)
end
 
return p

Posljednja izmjena od 15. travanj 2026. u 09:56

Dokumentacija modula
This module contains the relative luminance of HTML colors. It is used in Module:Kontrast boja.


-- ČISTI DATA MODUL – SAMO TABLICA, BEZ FUNKCIJA

local t = {

    ["aliceblue"]=0.9726,["antiquewhite"]=0.8469,["aqua"]=0.7874,["aquamarine"]=0.8079,["azure"]=0.9726,
    ["beige"]=0.9131,["bisque"]=0.8079,["black"]=0,["blanchedalmond"]=0.8469,["blue"]=0.0722,
    ["blueviolet"]=0.1265,["brown"]=0.098,["burlywood"]=0.5686,["cadetblue"]=0.3647,["chartreuse"]=0.7607,
    ["chocolate"]=0.1732,["coral"]=0.348,["cornflowerblue"]=0.4817,["cornsilk"]=0.9466,["crimson"]=0.1604,
    ["cyan"]=0.7874,["darkblue"]=0.018,["darkcyan"]=0.2159,["darkgoldenrod"]=0.2722,["darkgray"]=0.662,
    ["darkgreen"]=0.091,["darkgrey"]=0.662,["darkkhaki"]=0.485,["darkmagenta"]=0.0735,["darkolivegreen"]=0.1427,
    ["darkorange"]=0.3904,["darkorchid"]=0.1501,["darkred"]=0.0547,["darksalmon"]=0.4052,["darkseagreen"]=0.567,
    ["darkslateblue"]=0.0837,["darkslategray"]=0.067,["darkslategrey"]=0.067,["darkturquoise"]=0.468,["darkviolet"]=0.1096,
    ["deeppink"]=0.2848,["deepskyblue"]=0.5327,["dimgray"]=0.1413,["dimgrey"]=0.1413,["dodgerblue"]=0.4275,
    ["firebrick"]=0.0992,["floralwhite"]=0.9593,["forestgreen"]=0.1893,["fuchsia"]=0.2848,["gainsboro"]=0.7157,
    ["ghostwhite"]=0.9726,["gold"]=0.6981,["goldenrod"]=0.4192,["gray"]=0.2159,["green"]=0.091,
    ["greenyellow"]=0.8062,["grey"]=0.2159,["honeydew"]=0.9726,["hotpink"]=0.468,["indianred"]=0.214,
    ["indigo"]=0.0314,["ivory"]=0.9726,["khaki"]=0.8062,["lavender"]=0.8469,["lavenderblush"]=0.9131,
    ["lawngreen"]=0.7607,["lemonchiffon"]=0.9131,["lightblue"]=0.6514,["lightcoral"]=0.4052,["lightcyan"]=0.9726,
    ["lightgoldenrodyellow"]=0.9466,["lightgray"]=0.7157,["lightgreen"]=0.6514,["lightgrey"]=0.7157,["lightpink"]=0.8079,
    ["lightsalmon"]=0.468,["lightseagreen"]=0.3647,["lightskyblue"]=0.6514,["lightslategray"]=0.3647,["lightslategrey"]=0.3647,
    ["lightsteelblue"]=0.6514,["lightyellow"]=0.9726,["lime"]=0.7152,["limegreen"]=0.3647,["linen"]=0.9131,
    ["magenta"]=0.2848,["maroon"]=0.0547,["mediumaquamarine"]=0.468,["mediumblue"]=0.044,["mediumorchid"]=0.214,
    ["mediumpurple"]=0.3647,["mediumseagreen"]=0.3647,["mediumslateblue"]=0.3647,["mediumspringgreen"]=0.7152,["mediumturquoise"]=0.468,
    ["mediumvioletred"]=0.1265,["midnightblue"]=0.018,["mintcream"]=0.9726,["mistyrose"]=0.9131,["moccasin"]=0.8469,
    ["navajowhite"]=0.8079,["navy"]=0.018,["oldlace"]=0.9466,["olive"]=0.2159,["olivedrab"]=0.2722,
    ["orange"]=0.3904,["orangered"]=0.2126,["orchid"]=0.4052,["palegoldenrod"]=0.8062,["palegreen"]=0.7152,
    ["paleturquoise"]=0.8469,["palevioletred"]=0.214,["papayawhip"]=0.9131,["peachpuff"]=0.8079,["peru"]=0.2722,
    ["pink"]=0.8469,["plum"]=0.5686,["powderblue"]=0.7152,["purple"]=0.0735,["rebeccapurple"]=0.053,
    ["red"]=0.2126,["rosybrown"]=0.3647,["royalblue"]=0.2642,["saddlebrown"]=0.098,["salmon"]=0.348,
    ["sandybrown"]=0.468,["seagreen"]=0.1893,["seashell"]=0.9466,["sienna"]=0.1732,["silver"]=0.5271,
    ["skyblue"]=0.6514,["slateblue"]=0.214,["slategray"]=0.2642,["slategrey"]=0.2642,["snow"]=0.9726,
    ["springgreen"]=0.7152,["steelblue"]=0.2642,["tan"]=0.5686,["teal"]=0.091,["thistle"]=0.6514,
    ["tomato"]=0.2642,["turquoise"]=0.468,["violet"]=0.5686,["wheat"]=0.8062,["white"]=1,
    ["whitesmoke"]=0.9131,["yellow"]=0.9278,["yellowgreen"]=0.485,

}

return t