Module:Color contrast: Difference between revisions
add support for hsl
m>Mr. Stradivarius m (Protected Module:Color contrast: High-risk Lua module ([Edit=Allow only autoconfirmed users] (indefinite) [Move=Allow only autoconfirmed users] (indefinite))) |
m>Frietjes (add support for hsl) |
||
Line 16: | Line 16: | ||
end | end | ||
return v | return v | ||
end | |||
local function hsl2lum( h, s, l ) | |||
local lum = '' | |||
if ( 0 <= h and h < 360 and 0 <= s and s <= 1 and 0 <= l and l <= 1 ) then | |||
local c = (1 - abs(2*l - 1))*s | |||
local x = c*(1 - abs( mod(h/60, 2) - 1) ) | |||
local m = l - c/2 | |||
local R, G, B = m, m, m | |||
if( 0 <= h and h < 60 ) then | |||
R = R + c | |||
G = G + x | |||
elseif( 60 <= h and h < 120 ) then | |||
R = R + x | |||
G = G + c | |||
elseif( 120 <= h and h < 180 ) then | |||
G = R + c | |||
B = G + x | |||
elseif( 180 <= h and h < 240 ) then | |||
G = R + x | |||
B = G + c | |||
elseif( 240 <= h and h < 300 ) then | |||
R = R + x | |||
B = G + c | |||
elseif( 300 <= h and h < 360 ) then | |||
R = R + c | |||
B = G + x | |||
end | |||
lum = 0.2126 * sRGB(R) + 0.7152 * sRGB(G) + 0.0722 * sRGB(B) | |||
end | |||
return lum | |||
end | end | ||
Line 35: | Line 67: | ||
end | end | ||
-- remove leading # (if there is one) and whitespace | -- remove leading # (if there is one) and whitespace | ||
c = mw.ustring.match(c, '^[%s#]*([a-f0-9]*)[%s]*$') | c = mw.ustring.match(c, '^[%s#]*([a-f0-9]*)[%s;]*$') | ||
-- convert from hsl | |||
if mw.ustring.match(c,'^hsl%([%s]*[0-9][0-9]*[%s]*,[%s]*[0-9][0-9]*%%[%s]*,[%s]*[0-9][0-9]*%%[%s]*%)$') then | |||
local h = mw.ustring.match(c,'^hsl%[%s]*([0-9][0-9]*)[%s]*,[%s]*[0-9][0-9]*%%[%s]*,[%s]*[0-9][0-9]*%%[%s]*%$') | |||
local s = mw.ustring.match(c,'^hsl%[%s]*[0-9][0-9]*[%s]*,[%s]*([0-9][0-9]*)%%[%s]*,[%s]*[0-9][0-9]*%%[%s]*%$') | |||
local l = mw.ustring.match(c,'^hsl%[%s]*[0-9][0-9]*[%s]*,[%s]*[0-9][0-9]*%%[%s]*,[%s]*([0-9][0-9]*)%%[%s]*%$') | |||
return hsl2rgb(tonumber(h), tonumber(s)/100, tonumber(l)/100) | |||
end | |||
-- split into rgb | -- split into rgb |