Jump to content

Module:Color contrast/sandbox

From Wikipedia, the free encyclopedia
---- This module implements-- {{Color contrast ratio}}-- {{Greater color contrast ratio}}-- {{ColorToLum}}-- {{RGBColorToLum}}--localp={}localHTMLcolor=mw.loadData('Module:Color contrast/colors')localfunctionsRGB(v)if(v<=0.03928)thenv=v/12.92elsev=math.pow((v+0.055)/1.055,2.4)endreturnvendlocalfunctionrgbdec2lum(R,G,B)if(0<=RandR<256and0<=GandG<256and0<=BandB<256)thenreturn0.2126*sRGB(R/255)+0.7152*sRGB(G/255)+0.0722*sRGB(B/255)elsereturn''endendlocalfunctionhsl2lum(h,s,l)if(0<=handh<360and0<=sands<=1and0<=landl<=1)thenlocalc=(1-math.abs(2*l-1))*slocalx=c*(1-math.abs(math.fmod(h/60,2)-1))localm=l-c/2localr,g,b=m,m,mif(0<=handh<60)thenr=r+cg=g+xelseif(60<=handh<120)thenr=r+xg=g+celseif(120<=handh<180)theng=g+cb=b+xelseif(180<=handh<240)theng=g+xb=b+celseif(240<=handh<300)thenr=r+xb=b+celseif(300<=handh<360)thenr=r+cb=b+xendreturnrgbdec2lum(255*r,255*g,255*b)elsereturn''endendlocalfunctioncolor2lum(c)if(c==nil)thenreturn''end-- html '#' entityc=c:gsub("&#35;","#")-- whitespacec=c:match('^%s*(.-)[%s;]*$')-- unstrip nowiki strip markersc=mw.text.unstripNoWiki(c)-- lowercasec=c:lower()-- first try to look it uplocalL=HTMLcolor[c]if(L~=nil)thenreturnLend-- convert from hslifmw.ustring.match(c,'^hsl%([%s]*[0-9][0-9%.]*[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*%)$')thenlocalh,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]*%)$')returnhsl2lum(tonumber(h),tonumber(s)/100,tonumber(l)/100)end-- convert from rgbifmw.ustring.match(c,'^rgb%([%s]*[0-9][0-9]*[%s]*,[%s]*[0-9][0-9]*[%s]*,[%s]*[0-9][0-9]*[%s]*%)$')thenlocalR,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]*%)$')returnrgbdec2lum(tonumber(R),tonumber(G),tonumber(B))end-- convert from rgb percentifmw.ustring.match(c,'^rgb%([%s]*[0-9][0-9%.]*%%[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*,[%s]*[0-9][0-9%.]*%%[%s]*%)$')thenlocalR,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]*%)$')returnrgbdec2lum(255*tonumber(R)/100,255*tonumber(G)/100,255*tonumber(B)/100)end-- remove leading # (if there is one) and whitespacec=mw.ustring.match(c,'^[%s#]*([a-f0-9]*)[%s]*$')-- split into rgblocalcs=mw.text.split(cor'','')if(#cs==6)thenlocalR=16*tonumber('0x'..cs[1])+tonumber('0x'..cs[2])localG=16*tonumber('0x'..cs[3])+tonumber('0x'..cs[4])localB=16*tonumber('0x'..cs[5])+tonumber('0x'..cs[6])returnrgbdec2lum(R,G,B)elseif(#cs==3)thenlocalR=16*tonumber('0x'..cs[1])+tonumber('0x'..cs[1])localG=16*tonumber('0x'..cs[2])+tonumber('0x'..cs[2])localB=16*tonumber('0x'..cs[3])+tonumber('0x'..cs[3])returnrgbdec2lum(R,G,B)end-- failure, return blankreturn''end-- This exports the function for use in other modules.-- The colour is passed as a string.functionp._lum(color)returncolor2lum(color)endfunctionp._greatercontrast(args)localbias=tonumber(args['bias']or'0')or0localcss=(args['css']andargs['css']~='')andtrueorfalselocalv1=color2lum(args[1]or'')localc2=args[2]or'white'localv2=color2lum(c2)localc3=args[3]or'black'localv3=color2lum(c3)localratio1=-1;localratio2=-1;if(type(v1)=='number'andtype(v2)=='number')thenratio1=(v2+0.05)/(v1+0.05)ratio1=(ratio1<1)and1/ratio1orratio1endif(type(v1)=='number'andtype(v3)=='number')thenratio2=(v3+0.05)/(v1+0.05)ratio2=(ratio2<1)and1/ratio2orratio2endifcssthenlocalc1=args[1]or''ifmw.ustring.match(c1,'^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$')ormw.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]$')thenc1='#'..c1endifmw.ustring.match(c2,'^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$')ormw.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]$')thenc2='#'..c2endifmw.ustring.match(v3,'^[A-Fa-f0-9][A-Fa-f0-9][A-Fa-f0-9]$')ormw.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]$')thenc3='#'..c3endreturn'background-color:'..c1..'; color:'..((ratio1>0)and(ratio2>0)and((ratio1+bias>ratio2)andc2orc3)or'')..';'endreturn(ratio1>0)and(ratio2>0)and((ratio1+bias>ratio2)andc2orc3)or''endfunctionp._ratio(args)localv1=color2lum(args[1])localv2=color2lum(args[2])if(type(v1)=='number'andtype(v2)=='number')then-- v1 should be the brighter of the two.ifv2>v1thenv1,v2=v2,v1endreturn(v1+0.05)/(v2+0.05)elsereturnargs['error']or'?'endendfunctionp._styleratio(args)localstyle=(args[1]or''):lower()localbg,fg='white','black'locallum_bg,lum_fg=1,0ifargs[2]thenlocallum=color2lum(args[2])iflum~=''thenbg,lum_bg=args[2],lumendendifargs[3]thenlocallum=color2lum(args[3])iflum~=''thenfg,lum_fg=args[3],lumendendlocalslist=mw.text.split(mw.ustring.gsub(mw.ustring.gsub(styleor'','&#[Xx]23;','#'),'&#35;','#'),';')fork=1,#slistdolocals=slist[k]localk,v=s:match('^[%s]*([^:]-):([^:]-)[%s;]*$')k=kor''v=vor''if(k:match('^[%s]*(background)[%s]*$')ork:match('^[%s]*(background%-color)[%s]*$'))thenlocallum=color2lum(v)if(lum~='')thenbg,lum_bg=v,lumendelseif(k:match('^[%s]*(color)[%s]*$'))thenlocallum=color2lum(v)if(lum~='')thenbg,lum_fg=v,lumendendendiflum_bg>lum_fgthenreturn(lum_bg+0.05)/(lum_fg+0.05)elsereturn(lum_fg+0.05)/(lum_bg+0.05)endend--[[Use {{#invoke:Color contrast|somecolor}} directly or{{#invoke:Color contrast}} from a wrapper template.Parameters: -- |1= — required; A color to check.--]]functionp.lum(frame)localcolor=frame.args[1]orframe:getParent().args[1]returnp._lum(color)endfunctionp.ratio(frame)localargs=frame.args[1]andframe.argsorframe:getParent().argsreturnp._ratio(args)endfunctionp.styleratio(frame)localargs=frame.args[1]andframe.argsorframe:getParent().argsreturnp._styleratio(args)endfunctionp.greatercontrast(frame)localargs=frame.args[1]andframe.argsorframe:getParent().argsreturnp._greatercontrast(args)endreturnp
close