Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Template:Greater color contrast ratio/doc

Template page

{{#ifeq:doc |doc

    | 

{{#ifeq:show |show

         | {{#invoke:Message box|mbox}}
        }}{{#if: |
         |   {{#ifexist:Template:Greater color contrast ratio
                  | [[Category:{{#switch:Template |Template=Template |Module=Module |User=User |#default=Wikipedia}} documentation pages]]
                  | 
                 }}
        }}
    | 
   }}

{{#invoke:High-use|main|1=62535|2=|info=|demo=|form=|expiry=|system=}} {{#invoke:Lua banner|main}}

This template determines the pair of colors with the larger contrast ratio: color1/color2 or color1/color3. This is useful for selecting a foreground/background color pair. For accessibility, WCAG 2.0 AA guidelines require a contrast ratio of 3 or larger for large text, and 4.5 or larger for normal sized text.

In the default mode, color2 and color3 are white and black, and the selected color pair will always have a contrast ratio greater than 4.58.

Parameters[edit]

A color input can be by name ("khaki") or hextriplet with/without #-prefix ("#F0E68C", "F0E68C") <templatestyles src="Legend/styles.css" />{{#if:

        | {{safesubst:#invoke:If empty|main}}| }} 
|{{SAFESUBST:#if:1|1=}} (required): background color
|{{SAFESUBST:#if:2|2=}} first fontcolor. Optional, default = white (#FFFFFF)
|{{SAFESUBST:#if:3|3=}} second fontcolor. Optional, default = black (#000000)
The template returns the fontcolor with the greatest contrast
background is <templatestyles src="Legend/styles.css" />{{#if:
        | {{safesubst:#invoke:If empty|main}}| }} Khaki (#F0E68C): {{Greater color contrast ratio|khaki|white|black}} → {{safesubst:#invoke:Color contrast|greatercontrast}}
background is <templatestyles src="Legend/styles.css" />{{#if:
        | {{safesubst:#invoke:If empty|main}}| }} RoyalBlue (#4169E1): {{Greater color contrast ratio|#4169E1|FFFFFF|000000}} →  {{safesubst:#invoke:Color contrast|greatercontrast}} (=white)
css text
  • |{{SAFESUBST:#if:css|css=}}y makes the template return css-ready text:
<templatestyles src="Legend/styles.css" />{{#if:
        | {{safesubst:#invoke:If empty|main}}| }} Khaki (#F0E68C): {{Greater color contrast ratio|khaki|white|black|css=y}}
{{ safesubst:#if:

|{{ safesubst:#ifeq:{{{demo}}} |no

|<templatestyles src="Mono/styles.css" />
|

}} |{{ safesubst:#ifeq:Template|Template

|<templatestyles src="Mono/styles.css" />
|

}}}}{{safesubst:#invoke:Color contrast|greatercontrast}}

Example 1, <templatestyles src="Legend/styles.css" />{{#if:
        | {{safesubst:#invoke:If empty|main}}| }} Khaki (#F0E68C)
<span style="font-size:110%; {{Greater color contrast ratio|khaki|white|black|css=y}}">Example text on khaki background.</span>
<span style="font-size:110%; background-color:khaki; color:black;">Example text on khaki background.</span>
Example text on khaki background.
Example 2, <templatestyles src="Legend/styles.css" />{{#if:
        | {{safesubst:#invoke:If empty|main}}| }} #4169E1 (RoyalBlue)
<span style="font-size:110%; {{Greater color contrast ratio|#4169E1|#ffffff|#000000|css=y}}">Example text on #4169E1 background.</span>
<span style="font-size:110%; background-color:#4169E1; color:#ffffff;">Example text on #4169E1 background.</span>
Example text on #4169E1 background.
bias
  • |{{SAFESUBST:#if:bias|bias=}}number: reduces contrast-check. A bias of 1.25, reduces the possible minimum to 4.0, which may not meet accessibility standards.
This parameter should be used with caution to ensure accessibility. As stated above, when used without a bias, and with color2 and color3 equal to white and black, the selected color pair will always have a contrast ratio greater than 4.58.

Examples[edit]

Navy
  • <span style="background:navy; color:{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Navy</span>Navy
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Navy</span>Navy
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Navy</span>Navy
Red
  • <span style="background:red; color:{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Red</span>Red
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Red</span>Red
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Red</span>Red
White
  • <span style="background:white; color:{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">White</span>White
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">White</span>White
Black
  • <span style="background:black; color:{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Black</span>Black
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Black</span>Black
#005500
  • <span style="background:#005500; color:{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">#005500</span>#005500
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">#005500</span>#005500
  • <span style="background:#005500; color:#{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">005500</span>005500
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">005500</span>005500
CC5500
  • <span style="background:#CC5500; color:#{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">CC5500</span>CC5500
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">CC5500</span>CC5500
CC5500, |{{SAFESUBST:#if:bias|bias=}}1
  • <span style="background:#CC5500; color:#{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">CC5500</span>CC5500
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">CC5500</span>CC5500
error
  • <span style="background:Not a color; color:#{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Not a color</span>Not a color
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Not a color</span>Not a color
(returns background-color:Not a color; color:;, and so no color is set; defaults to black-on-white.)
  • <span style="{{#Invoke:Template link general|main|nowrap=yes|nolink=yes}}">Not a color</span>Not a color
(returns background-color:Navy; color:;, and so no fontcolor is set; defaults to black. The template did not function.)

See also[edit]



Debug data: