Module:Color contrast: Difference between revisions
Content added Content deleted
(better error checking) |
(rudimentary CSS parser) |
||
Line 202: | Line 202: | ||
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._greatercontrast(args) |
return p._greatercontrast(args) |
||
end |
|||
-- warning: crude! may be fooled by sufficientry tricky CSS |
|||
function p.extract_css_colors(css, e) |
|||
local fgcolor, bgcolor |
|||
css = css:gsub("/%*.-%*/", "") |
|||
for name, value in string.gmatch(css, "([a-z-]*)%s*:%s*([^;]*)") do |
|||
value = mw.text.trim(value) |
|||
if name == 'color' then |
|||
fgcolor = value |
|||
elseif name == 'background-color' then |
|||
bgcolor = value |
|||
elseif name == 'background' then |
|||
value = value:gsub("url%((['\"]?).-%1%)", "") |
|||
bgcolor = value:match("#[0-9A-Fa-f]*") or value:match("rgba?%([0-9.%, ]*%)") or value:match("hsla?%([0-9.%, ]*%)") |
|||
if not bgcolor then |
|||
value = value:gsub("[0-9.][0-9.]*[%%a-z][a-z]?[a-z]?", "") |
|||
value = value:gsub("auto", ""):gsub("cover", ""):gsub("contain", "") |
|||
value = value:gsub("scroll", ""):gsub("fixed", ""):gsub("local", "") |
|||
value = value:gsub("left", ""):gsub("right", ""):gsub("top", ""):gsub("bottom", ""):gsub("center", "") |
|||
value = value:gsub("no%-repeat", ""):gsub("repeat", ""):gsub("repeat%-[xy]", ""):gsub("space", ""):gsub("round", "") |
|||
value = value:gsub("border%-box", ""):gsub("padding%-box", ""):gsub("content%-box", "") |
|||
bgcolor = mw.text.trim(value) |
|||
end |
|||
end |
|||
end |
|||
return fgcolor, bgcolor |
|||
end |
end |
||