Jump to content

Module:Color contrast: Difference between revisions

add support for rgb(x,y,z)
m>Frietjes
(combine)
m>Frietjes
(add support for rgb(x,y,z))
Line 19: Line 19:


local function rgbdec2lum( R, G, B )
local function rgbdec2lum( R, G, B )
return 0.2126 * sRGB(R/255) + 0.7152 * sRGB(G/255) + 0.0722 * sRGB(B/255)
if ( 0 <= R and R < 256 and 0 <= G and G < 256 and 0 <= B and B < 256 ) then
return 0.2126 * sRGB(R/255) + 0.7152 * sRGB(G/255) + 0.0722 * sRGB(B/255)
else
return ''
end
end
end


local function hsl2lum( h, s, l )
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
if ( 0 <= h and h < 360 and 0 <= s and s <= 1 and 0 <= l and l <= 1 ) then
local c = (1 - math.abs(2*l - 1))*s
local c = (1 - math.abs(2*l - 1))*s
Line 49: Line 52:
b = b + x
b = b + x
end
end
lum = rgbdec2lum(255*r, 255*g, 255*b)
return rgbdec2lum(255*r, 255*g, 255*b)
else
return ''
end
end
return lum
end
end


Line 72: Line 76:


   -- convert from hsl
   -- 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
   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,'^hsl%([%s]*([0-9][0-9]*)[%s]*,[%s]*([0-9][0-9]*)%%[%s]*,[%s]*([0-9][0-9]*)%%[%s]*%)$')
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
  -- convert from rgb
  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,'^[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))
   end
   end


Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.