Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Module:Color contrast

Permanently protected module
From Wikipedia, the free encyclopedia
Module documentation[view] [edit] [history] [purge]
Thismodule is rated asready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned onhelp pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed throughsandbox testing rather than repeated trial-and-error editing.
Page template-protectedThis module is currentlyprotected from editing.
See theprotection policy andprotection log for more details. Please discuss any changes on thetalk page; you maysubmit an edit request to ask anadministrator to make an edit if it isuncontroversial or supported byconsensus. You may alsorequest that this page be unprotected.
WarningThis Lua module is used onapproximately 551,000 pages.
To avoid major disruption and server load, any changes should be tested in the module's/sandbox or/testcases subpages, or in your ownmodule sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on thetalk page before implementing them.
This module depends on the following other modules:

This module is used primarily by

It is also used for tracking within

and for documentation in

The formulas used arestated in WCAG 2.x specifications.WCAG is the main guideline for creating accessible interfaces on the web.

Usage

To use this module, you may use one of the above listed templates or invoke the module directly

  • To compute relative luminescence:
    {{RGBColorToLum|color}} or{{#invoke:Color contrast|lum|color}}
  • To compute a contrast ratio between two colors:
    {{Color contrast ratio|color1|color2|error=?}} or{{#invoke:Color contrast|ratio|color1|color2|error=?}}
  • To determine which of two colors (color2a and color2b) has the greater contrast ratio with a particular color (color1):
    {{Greater color contrast ratio|color1|color2a|color2b}} or{{#invoke:Color contrast|greatercontrast|color1|color2a|color2b}}
  • To compute the contrast ratio between the background and text colors specified in a css style string:
    {{#invoke:Color contrast|styleratio|css style statement string|default background color|default text color}}
The abovedocumentation istranscluded fromModule:Color contrast/doc.(edit |history)
Editors can experiment in this module'ssandbox(edit |diff) and testcases(create) pages.
Subpages of this module.

---- 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
Retrieved from "https://en.wikipedia.org/w/index.php?title=Module:Color_contrast&oldid=1283164428"
Categories:
Hidden category:

[8]ページ先頭

©2009-2025 Movatter.jp