Module:Color contrast: Difference between revisions

refactor to allow this to be called from another module
m>Frietjes
(add shorthand)
m>Frietjes
(refactor to allow this to be called from another module)
Line 120: Line 120:
-- failure, return blank
-- failure, return blank
return ''
return ''
end
local function colors2ratio(c1,c2)
local v1 = color2lum(c1)
local v2 = color2lum(c2)
if (type(v1) == 'number' and type(v2) == 'number') then
return (v2 + 0.05)/(v1 + 0.05)
else
return ''
end
end
end


Line 127: Line 137:


function p.ratio(frame)
function p.ratio(frame)
local v1 = color2lum(frame.args[1] or frame:getParent().args[1])
local r = colors2ratio(
local v2 = color2lum(frame.args[2] or frame:getParent().args[2])
frame.args[1] or frame:getParent().args[1],
if (type(v1) == 'number' and type(v2) == 'number') then
frame.args[2] or frame:getParent().args[2]
return (v2 + 0.05)/(v1 + 0.05)
)
if (r ~= '') then
return r
else
else
return frame.args['error'] or frame:getParent().args['error'] or '?'
return frame.args['error'] or frame:getParent().args['error'] or '?'
Anonymous user