Editing Module:Color contrast
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 63: | Line 63: | ||
return '' | return '' | ||
end | end | ||
-- whitespace | -- whitespace | ||
c = c:match( '^%s*(.-)[%s;]*$' ) | c = c:match( '^%s*(.-)[%s;]*$' ) | ||
-- lowercase | -- lowercase | ||
Line 83: | Line 76: | ||
-- convert from hsl | -- convert from hsl | ||
if mw.ustring.match(c,'^ | if mw.ustring.match(c,'^[Hh][Ss][Ll]%([%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,'^ | local h, s, l = mw.ustring.match(c,'^[Hh][Ss][Ll]%([%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) | return hsl2lum(tonumber(h), tonumber(s)/100, tonumber(l)/100) | ||
end | end | ||
-- convert from rgb | -- convert from rgb | ||
if mw.ustring.match(c,'^ | if mw.ustring.match(c,'^[Rr][Gg][Bb]%([%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,'^ | local R, G, B = mw.ustring.match(c,'^[Rr][Gg][Bb]%([%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)) | return rgbdec2lum(tonumber(R), tonumber(G), tonumber(B)) | ||
end | end | ||
Line 125: | Line 112: | ||
function p._greatercontrast(args) | function p._greatercontrast(args) | ||
local bias = tonumber(args['bias'] or '0') or 0 | local bias = tonumber(args['bias'] or '0') or 0 | ||
local v1 = color2lum(args[1] or '') | local v1 = color2lum(args[1] or '') | ||
local c2 = args[2] or '#FFFFFF' | local c2 = args[2] or '#FFFFFF' | ||
Line 131: | Line 117: | ||
local c3 = args[3] or '#000000' | local c3 = args[3] or '#000000' | ||
local v3 = color2lum(c3) | local v3 = color2lum(c3) | ||
local ratio1 = | local ratio1 = 0; | ||
local ratio2 = | local ratio2 = 0; | ||
if (type(v1) == 'number' and type(v2) == 'number') then | if (type(v1) == 'number' and type(v2) == 'number') then | ||
ratio1 = (v2 + 0.05)/(v1 + 0.05) | ratio1 = (v2 + 0.05)/(v1 + 0.05) | ||
Line 141: | Line 127: | ||
ratio2 = (ratio2 < 1) and 1/ratio2 or ratio2 | ratio2 = (ratio2 < 1) and 1/ratio2 or ratio2 | ||
end | end | ||
return (ratio1 + bias > ratio2) and c2 or c3 | |||
return | |||
end | end | ||
Line 173: | Line 141: | ||
else | else | ||
return args['error'] or '?' | return args['error'] or '?' | ||
end | end | ||
end | end | ||
Line 218: | Line 151: | ||
local args = frame.args[1] and frame.args or frame:getParent().args | local args = frame.args[1] and frame.args or frame:getParent().args | ||
return p._ratio(args) | return p._ratio(args) | ||
end | end | ||