Title: Module:Color contrast - freem URL Source: https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast Markdown Content: [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#) - [x] * [Home](https://freemwiki.com/wiki/Main_Page) * [Random](https://freemwiki.com/wiki/Special:Random) * [Log in](https://freemwiki.com/index.php?title=Special:UserLogin&returnto=Module%3AColor+contrast&returntoquery=mobileaction%3Dtoggle_view_mobile) * [Settings](https://freemwiki.com/index.php?title=Special:MobileOptions&returnto=Module%3AColor+contrast) * [About](https://freemwiki.com/wiki/About) * [Terms of Service](https://freemwiki.com/wiki/Terms_of_Service) [freem](https://freemwiki.com/wiki/Main_Page) Search # Module:Color contrast * [Language](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast "Language") * [Watch](https://freemwiki.com/index.php?title=Special:UserLogin&returnto=Module%3AColor+contrast) * [Edit](https://freemwiki.com/index.php?title=Module:Color_contrast&action=edit) _Documentation for this module may be created at [Module:Color contrast/doc](https://freemwiki.com/index.php?title=Module:Color\_contrast/doc&action=edit&redlink=1 "Module:Color contrast/doc (page does not exist)")_ [](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-1)--[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-2)-- This module implements[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-3)-- {{[Color contrast ratio](https://freemwiki.com/wiki/Template:Color_contrast_ratio "Template:Color contrast ratio")}}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-4)-- {{[Greater color contrast ratio](https://freemwiki.com/wiki/Template:Greater_color_contrast_ratio "Template:Greater color contrast ratio")}}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-5)-- {{[ColorToLum](https://freemwiki.com/wiki/Template:ColorToLum "Template:ColorToLum")}}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-6)-- {{[RGBColorToLum](https://freemwiki.com/wiki/Template:RGBColorToLum "Template:RGBColorToLum")}}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-7)--[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-8)local p = {}[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-9)local HTMLcolor = mw.loadData( ['Module:Color contrast/colors'](https://freemwiki.com/wiki/Module:Color_contrast/colors) )[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-10)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-11)local function sRGB (v)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-12)if (v <= 0.03928) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-13)v = v / 12.92[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-14)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-15)v = math.pow((v+0.055)/1.055, 2.4)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-16)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-17)return v[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-18)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-19)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-20)local function rgbdec2lum(R, G, B)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-21)if ( 0 <= R and R < 256 and 0 <= G and G < 256 and 0 <= B and B < 256 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-22)return 0.2126 * sRGB(R/255) + 0.7152 * sRGB(G/255) + 0.0722 * sRGB(B/255)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-23)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-24)return ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-25)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-26)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-27)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-28)local function hsl2lum(h, s, l)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-29)if ( 0 <= h and h < 360 and 0 <= s and s <= 1 and 0 <= l and l <= 1 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-30)local c = (1 - math.abs(2*l - 1))*s[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-31)local x = c*(1 - math.abs( math.fmod(h/60, 2) - 1) )[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-32)local m = l - c/2[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-33)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-34)local r, g, b = m, m, m[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-35)if( 0 <= h and h < 60 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-36)r = r + c[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-37)g = g + x[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-38)elseif( 60 <= h and h < 120 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-39)r = r + x[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-40)g = g + c[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-41)elseif( 120 <= h and h < 180 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-42)g = g + c[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-43)b = b + x[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-44)elseif( 180 <= h and h < 240 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-45)g = g + x[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-46)b = b + c[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-47)elseif( 240 <= h and h < 300 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-48)r = r + x[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-49)b = b + c[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-50)elseif( 300 <= h and h < 360 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-51)r = r + c[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-52)b = b + x[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-53)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-54)return rgbdec2lum(255*r, 255*g, 255*b)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-55)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-56)return ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-57)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-58)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-59)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-60)local function color2lum(c)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-61)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-62)if (c == nil) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-63)return ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-64)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-65)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-66)-- html '#' entity[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-67)c = c:gsub("#", "#")[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-68)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-69)-- whitespace[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-70)c = c:match( '^%s*(.-)[%s;]*$' )[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-71)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-72)-- unstrip nowiki strip markers[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-73)c = mw.text.unstripNoWiki(c)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-74)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-75)-- lowercase[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-76)c = c:lower()[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-77)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-78)-- first try to look it up[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-79)local L = HTMLcolor[c][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-80)if (L ~= nil) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-81)return L[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-82)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-83)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-84)-- convert from hsl[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-85)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[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-86)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]*%)$')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-87)return hsl2lum(tonumber(h), tonumber(s)/100, tonumber(l)/100)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-88)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-89)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-90)-- convert from rgb[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-91)if mw.ustring.match(c,'^rgb%([%s]*[0-9][0-9]*[%s]*,[%s]*[0-9][0-9]*[%s]*,[%s]*[0-9][0-9]*[%s]*%)$') then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-92)local R, G, B = mw.ustring.match(c,'^rgb%([%s]*([0-9][0-9]*)[%s]*,[%s]*([0-9][0-9]*)[%s]*,[%s]*([0-9][0-9]*)[%s]*%)$')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-93)return rgbdec2lum(tonumber(R), tonumber(G), tonumber(B))[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-94)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-95)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-96)-- convert from rgb percent[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-97)if mw.ustring.match(c,'^rgb%([%s]*[0-9][0-9%.]*%%[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*%)$') then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-98)local R, G, B = mw.ustring.match(c,'^rgb%([%s]*([0-9][0-9%.]*)%%[%s]*,[%s]*([0-9][0-9%.]*)%%[%s]*,[%s]*([0-9][0-9%.]*)%%[%s]*%)$')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-99)return rgbdec2lum(255*tonumber(R)/100, 255*tonumber(G)/100, 255*tonumber(B)/100)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-100)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-101)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-102)-- remove leading # (if there is one) and whitespace[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-103)c = mw.ustring.match(c, '^[%s#]*([a-f0-9]*)[%s]*$')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-104)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-105)-- split into rgb[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-106)local cs = mw.text.split(c or '', '')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-107)if( #cs == 6 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-108)local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[2])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-109)local G = 16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[4])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-110)local B = 16*tonumber('0x' .. cs[5]) + tonumber('0x' .. cs[6])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-111)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-112)return rgbdec2lum(R, G, B)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-113)elseif ( #cs == 3 ) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-114)local R = 16*tonumber('0x' .. cs[1]) + tonumber('0x' .. cs[1])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-115)local G = 16*tonumber('0x' .. cs[2]) + tonumber('0x' .. cs[2])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-116)local B = 16*tonumber('0x' .. cs[3]) + tonumber('0x' .. cs[3])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-117)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-118)return rgbdec2lum(R, G, B)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-119)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-120)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-121)-- failure, return blank[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-122)return ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-123)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-124)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-125)-- This exports the function for use in other modules.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-126)-- The colour is passed as a string.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-127)function p._lum(color)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-128)return color2lum(color)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-129)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-130)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-131)function p._greatercontrast(args)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-132)local bias = tonumber(args['bias'] or '0') or 0[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-133)local css = (args['css'] and args['css'] ~= '') and true or false[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-134)local v1 = color2lum(args[1] or '')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-135)local c2 = args[2] or 'white'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-136)local v2 = color2lum(c2)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-137)local c3 = args[3] or 'black'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-138)local v3 = color2lum(c3)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-139)local ratio1 = -1;[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-140)local ratio2 = -1;[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-141)if (type(v1) == 'number' and type(v2) == 'number') then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-142)ratio1 = (v2 + 0.05)/(v1 + 0.05)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-143)ratio1 = (ratio1 < 1) and 1/ratio1 or ratio1[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-144)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-145)if (type(v1) == 'number' and type(v3) == 'number') then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-146)ratio2 = (v3 + 0.05)/(v1 + 0.05)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-147)ratio2 = (ratio2 < 1) and 1/ratio2 or ratio2[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-148)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-149)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-150)if css then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-151)local c1 = args[1] or ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-152)if mw.ustring.match(c1, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') or[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-153)mw.ustring.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]$') then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-154)c1 = '#' .. c1[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-155)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-156)if mw.ustring.match(c2, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') or[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-157)mw.ustring.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]$') then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-158)c2 = '#' .. c2[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-159)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-160)if mw.ustring.match(v3, '^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$') or[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-161)mw.ustring.match(v3, '^[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[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-162)c3 = '#' .. c3[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-163)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-164)return 'background-color:' .. c1 .. '; color:' .. ((ratio1 > 0) and (ratio2 > 0) and ((ratio1 + bias > ratio2) and c2 or c3) or '') .. ';'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-165)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-166)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-167)return (ratio1 > 0) and (ratio2 > 0) and ((ratio1 + bias > ratio2) and c2 or c3) or ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-168)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-169)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-170)function p._ratio(args)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-171)local v1 = color2lum(args[1])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-172)local v2 = color2lum(args[2])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-173)if (type(v1) == 'number' and type(v2) == 'number') then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-174)-- v1 should be the brighter of the two.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-175)if v2 > v1 then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-176)v1, v2 = v2, v1[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-177)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-178)return (v1 + 0.05)/(v2 + 0.05)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-179)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-180)return args['error'] or '?'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-181)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-182)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-183)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-184)function p._styleratio(args)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-185)local style = (args[1] or ''):lower()[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-186)local bg, fg = 'white', 'black'[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-187)local lum_bg, lum_fg = 1, 0[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-188)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-189)if args[2] then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-190)local lum = color2lum(args[2])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-191)if lum ~= '' then bg, lum_bg = args[2], lum end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-192)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-193)if args[3] then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-194)local lum = color2lum(args[3])[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-195)if lum ~= '' then fg, lum_fg = args[3], lum end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-196)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-197)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-198)local slist = mw.text.split(mw.ustring.gsub(mw.ustring.gsub(style or '', '&#[Xx]23;', '#'), '#', '#'), ';')[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-199)for k = 1,#slist do[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-200)local s = slist[k][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-201)local k,v = s:match( '^[%s]*([^:]-):([^:]-)[%s;]*$' )[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-202)k = k or ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-203)v = v or ''[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-204)if (k:match('^[%s]*(background)[%s]*$') or k:match('^[%s]*(background%-color)[%s]*$')) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-205)local lum = color2lum(v)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-206)if( lum ~= '' ) then bg, lum_bg = v, lum end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-207)elseif (k:match('^[%s]*(color)[%s]*$')) then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-208)local lum = color2lum(v)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-209)if( lum ~= '' ) then bg, lum_fg = v, lum end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-210)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-211)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-212)if lum_bg > lum_fg then[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-213)return (lum_bg + 0.05)/(lum_fg + 0.05)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-214)else[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-215)return (lum_fg + 0.05)/(lum_bg + 0.05)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-216)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-217)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-218)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-219)--[[[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-220)Use {{#invoke:Color contrast|somecolor}} directly or[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-221){{#invoke:Color contrast}} from a wrapper template.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-222)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-223)Parameters:[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-224) -- |1= — required; A color to check.[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-225)--]][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-226)function p.lum(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-227)local color = frame.args[1] or frame:getParent().args[1][](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-228)return p._lum(color)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-229)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-230)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-231)function p.ratio(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-232)local args = frame.args[1] and frame.args or frame:getParent().args[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-233)return p._ratio(args)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-234)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-235)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-236)function p.styleratio(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-237)local args = frame.args[1] and frame.args or frame:getParent().args[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-238)return p._styleratio(args)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-239)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-240)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-241)function p.greatercontrast(frame)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-242)local args = frame.args[1] and frame.args or frame:getParent().args[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-243)return p._greatercontrast(args)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-244)end[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-245)[](https://freemwiki.com/index.php?mobileaction=toggle_view_mobile&title=Module%3AColor_contrast#L-246)return p Retrieved from "[https://freemwiki.com/index.php?title=Module:Color_contrast&oldid=579812](https://freemwiki.com/index.php?title=Module:Color_contrast&oldid=579812)" [**Last edited 9 months ago** by Lukegao](https://freemwiki.com/index.php?title=Module:Color_contrast&action=history) #### Languages This page is not available in other languages. freem * This page was last edited on 21 October 2025, at 06:18. * Content is available under [Creative Commons Attribution-ShareAlike 4.0](https://creativecommons.org/licenses/by-sa/4.0/) unless otherwise noted. FreemWiki is operated by **FINGERPOWER DIGITAL TECHNOLOGY LLC** (Colorado, Entity ID 20221978727). * [Privacy Policy](https://freemwiki.com/wiki/Privacy_Policy) * [About](https://freemwiki.com/wiki/About) * [Terms of Service](https://freemwiki.com/wiki/Terms_of_Service) * [Desktop](https://freemwiki.com/index.php?title=Module:Color_contrast&mobileaction=toggle_view_desktop)