Module:Color contrast: Difference between revisions
better error checking
m>Frietjes (typos) |
m>Frietjes (better error checking) |
||
Line 153: | Line 153: | ||
local style = (args[1] or ''):lower() | local style = (args[1] or ''):lower() | ||
local bg, fg = (args[2] or 'white'), (args[3] or 'black') | local bg, fg = (args[2] or 'white'), (args[3] or 'black') | ||
local lum_bg, lum_fg = 1, 0 | |||
local slist = mw.text.split(style or '', ';') | local slist = mw.text.split(style or '', ';') | ||
Line 161: | Line 162: | ||
v = v or '' | v = v or '' | ||
if (k:match('^[%s]*(background)[%s]*$') or k:match('^[%s]*(background-color)[%s]*$')) then | if (k:match('^[%s]*(background)[%s]*$') or k:match('^[%s]*(background-color)[%s]*$')) then | ||
bg = v | local lum = color2lum(v) | ||
if( lum ~= '' ) then bg, lum_bg = v, lum end | |||
elseif (k:match('^[%s]*(color)[%s]*$')) then | elseif (k:match('^[%s]*(color)[%s]*$')) then | ||
local lum = color2lum(v) | |||
if( lum ~= '' ) then bg, lum_fg = v, lum end | |||
end | end | ||
end | end | ||
if lum_bg > lum_fg then | |||
return | return (lum_bg + 0.05)/(lum_fg + 0.05) | ||
else | |||
return (lum_fg + 0.05)/(lum_bg + 0.05) | |||
end | |||
end | end | ||