Module:Color contrast: Difference between revisions
imported>Jonesey95 Undid revision 1366336997 by Aidan9382 (talk). Appears to have broken the layout of Template:basketballbox. See Template:Basketballbox/testcases |
imported>Axolitl Implement edit request, for real this time |
||
| Line 1: | Line 1: | ||
-- | -- | ||
-- This module implements | -- This module implements | ||
-- | -- {{Color contrast ratio}} | ||
-- | -- {{Greater color contrast ratio}} | ||
-- | -- {{ColorToLum}} | ||
-- | -- {{RGBColorToLum}} | ||
-- | -- | ||
local p = {} | local p = {} | ||
| Line 82: | Line 82: | ||
end | end | ||
-- convert from hsl | -- convert from hsl / hsla (accepts comma or space delimiters) | ||
if mw.ustring.match(c,'^ | if mw.ustring.match(c,'^hsla?%([%s]*[0-9][0-9%.]*[%s]*[,%s][%s]*[0-9][0-9%.]*%%[%s]*[,%s][%s]*[0-9][0-9%.]*%%.*%)$') then | ||
local h, s, l = mw.ustring.match(c,'^ | local h, s, l = mw.ustring.match(c,'^hsla?%([%s]*([0-9][0-9%.]*)[%s]*[,%s][%s]*([0-9][0-9%.]*)%%[%s]*[,%s][%s]*([0-9][0-9%.]*)%%.*%)$') | ||
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 / rgba (numeric values 0 to 255) | ||
if mw.ustring.match(c,'^ | if mw.ustring.match(c,'^rgba?%([%s]*[0-9][0-9]*[%s]*[,%s][%s]*[0-9][0-9]*[%s]*[,%s][%s]*[0-9][0-9]*.*%)$') then | ||
local R, G, B = mw.ustring.match(c,'^ | local R, G, B = mw.ustring.match(c,'^rgba?%([%s]*([0-9][0-9]*)[%s]*[,%s][%s]*([0-9][0-9]*)[%s]*[,%s][%s]*([0-9][0-9]*).*%)$') | ||
return rgbdec2lum(tonumber(R), tonumber(G), tonumber(B)) | return rgbdec2lum(tonumber(R), tonumber(G), tonumber(B)) | ||
end | end | ||
-- convert from rgb percent | -- convert from rgb / rgba percent | ||
if mw.ustring.match(c,'^ | if mw.ustring.match(c,'^rgba?%([%s]*[0-9][0-9%.]*%%[%s]*[,%s][%s]*[0-9][0-9%.]*%%[%s]*[,%s][%s]*[0-9][0-9%.]*%%.*%)$') then | ||
local R, G, B = mw.ustring.match(c,'^ | local R, G, B = mw.ustring.match(c,'^rgba?%([%s]*([0-9][0-9%.]*)%%[%s]*[,%s][%s]*([0-9][0-9%.]*)%%[%s]*[,%s][%s]*([0-9][0-9%.]*)%%.*%)$') | ||
return rgbdec2lum(255*tonumber(R)/100, 255*tonumber(G)/100, 255*tonumber(B)/100) | return rgbdec2lum(255*tonumber(R)/100, 255*tonumber(G)/100, 255*tonumber(B)/100) | ||
end | end | ||
| Line 105: | Line 105: | ||
-- split into rgb | -- split into rgb | ||
local cs = mw.text.split(c or '', '') | local cs = mw.text.split(c or '', '') | ||
if( #cs == 6 ) then | if( #cs == 6 or #cs == 8 ) then -- Accepts 6 digits (#RRGGBB) or 8 digits (#RRGGBBAA) | ||
local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[2]) | local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[2]) | ||
local G = 16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[4]) | local G = 16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[4]) | ||
| Line 111: | Line 111: | ||
return rgbdec2lum(R, G, B) | return rgbdec2lum(R, G, B) | ||
elseif ( #cs == 3 ) then | elseif ( #cs == 3 or #cs == 4 ) then -- Accepts 3 digits (#RGB) or 4 digits (#RGBA) | ||
local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[1]) | local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[1]) | ||
local G = 16*tonumber('0x' .. cs[2]) + tonumber('0x' .. cs[2]) | local G = 16*tonumber('0x' .. cs[2]) + tonumber('0x' .. cs[2]) | ||
| Line 150: | Line 150: | ||
if css then | if css then | ||
local c1 = args[1] or '' | local c1 = args[1] or '' | ||
if | if string.match(c1, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]?$') or | ||
string.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][A-Fa-f0-9]?[A-Fa-f0-9]?$') then | |||
c1 = '#' .. c1 | c1 = '#' .. c1 | ||
end | end | ||
if | if string.match(c2, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]?$') or | ||
string.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][A-Fa-f0-9]?[A-Fa-f0-9]?$') then | |||
c2 = '#' .. c2 | c2 = '#' .. c2 | ||
end | end | ||
if | if string.match(c3, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]?$') or | ||
string.match(c3, '^[A-Fa-f0-9][A-Fa-f0-9][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 | c3 = '#' .. c3 | ||
end | end | ||
| Line 184: | Line 184: | ||
function p._styleratio(args) | function p._styleratio(args) | ||
local style = (args[1] or ''):lower() | local style = (args[1] or ''):lower() | ||
local lum_bg, lum_fg = 1, 0 | local lum_bg, lum_fg = 1, 0 | ||
if args[2] then | if args[2] then | ||
local lum = color2lum(args[2]) | local lum = color2lum(args[2]) | ||
if lum ~= '' then | if lum ~= '' then lum_bg = lum end | ||
end | end | ||
if args[3] then | if args[3] then | ||
local lum = color2lum(args[3]) | local lum = color2lum(args[3]) | ||
if lum ~= '' then | if lum ~= '' then lum_fg = lum end | ||
end | end | ||
| Line 199: | Line 198: | ||
for k = 1,#slist do | for k = 1,#slist do | ||
local s = slist[k] | local s = slist[k] | ||
local | local prop, val = s:match( '^[%s]*([^:]-):([^:]-)[%s;]*$' ) | ||
prop = prop or '' | |||
val = val or '' | |||
if ( | if (prop:match('^[%s]*(background)[%s]*$') or prop:match('^[%s]*(background%-color)[%s]*$')) then | ||
local lum = color2lum( | local lum = color2lum(val) | ||
if( lum ~= '' ) then | if( lum ~= '' ) then lum_bg = lum end | ||
elseif ( | elseif (prop:match('^[%s]*(color)[%s]*$')) then | ||
local lum = color2lum( | local lum = color2lum(val) | ||
if( lum ~= '' ) then | if( lum ~= '' ) then lum_fg = lum end | ||
end | end | ||
end | end | ||
| Line 222: | Line 221: | ||
Parameters: | Parameters: | ||
-- |1= — required; A color to check. | -- |1= — required; A color to check. | ||
--]] | --]] | ||
function p.lum(frame) | function p.lum(frame) | ||