Skip to content

Commit d10a115

Browse files
ffooddolsza
authored andcommitted
Use WCAG contrast algo (twbs#30168)
1 parent 7fece00 commit d10a115

File tree

7 files changed

+155
-116
lines changed

7 files changed

+155
-116
lines changed

scss/_functions.scss

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,43 @@
8888
}
8989

9090
// Color contrast
91-
@function color-yiq($color, $dark: $yiq-text-dark, $light: $yiq-text-light) {
92-
$r: red($color);
93-
$g: green($color);
94-
$b: blue($color);
91+
// See https://github.com/twbs/bootstrap/pull/30168
9592

96-
$yiq: (($r * 299) + ($g * 587) + ($b * 114)) / 1000;
93+
// A list of pre-calculated numbers of pow(($value / 255 + .055) / 1.055, 2.4). (from 0 to 255)
94+
// stylelint-disable-next-line scss/dollar-variable-default, scss/dollar-variable-pattern
95+
$_luminance-list: .0008 .001 .0011 .0013 .0015 .0017 .002 .0022 .0025 .0027 .003 .0033 .0037 .004 .0044 .0048 .0052 .0056 .006 .0065 .007 .0075 .008 .0086 .0091 .0097 .0103 .011 .0116 .0123 .013 .0137 .0144 .0152 .016 .0168 .0176 .0185 .0194 .0203 .0212 .0222 .0232 .0242 .0252 .0262 .0273 .0284 .0296 .0307 .0319 .0331 .0343 .0356 .0369 .0382 .0395 .0409 .0423 .0437 .0452 .0467 .0482 .0497 .0513 .0529 .0545 .0561 .0578 .0595 .0612 .063 .0648 .0666 .0685 .0704 .0723 .0742 .0762 .0782 .0802 .0823 .0844 .0865 .0887 .0908 .0931 .0953 .0976 .0999 .1022 .1046 .107 .1095 .1119 .1144 .117 .1195 .1221 .1248 .1274 .1301 .1329 .1356 .1384 .1413 .1441 .147 .15 .1529 .1559 .159 .162 .1651 .1683 .1714 .1746 .1779 .1812 .1845 .1878 .1912 .1946 .1981 .2016 .2051 .2086 .2122 .2159 .2195 .2232 .227 .2307 .2346 .2384 .2423 .2462 .2502 .2542 .2582 .2623 .2664 .2705 .2747 .2789 .2831 .2874 .2918 .2961 .3005 .305 .3095 .314 .3185 .3231 .3278 .3325 .3372 .3419 .3467 .3515 .3564 .3613 .3663 .3712 .3763 .3813 .3864 .3916 .3968 .402 .4072 .4125 .4179 .4233 .4287 .4342 .4397 .4452 .4508 .4564 .4621 .4678 .4735 .4793 .4851 .491 .4969 .5029 .5089 .5149 .521 .5271 .5333 .5395 .5457 .552 .5583 .5647 .5711 .5776 .5841 .5906 .5972 .6038 .6105 .6172 .624 .6308 .6376 .6445 .6514 .6584 .6654 .6724 .6795 .6867 .6939 .7011 .7084 .7157 .7231 .7305 .7379 .7454 .7529 .7605 .7682 .7758 .7835 .7913 .7991 .807 .8148 .8228 .8308 .8388 .8469 .855 .8632 .8714 .8796 .8879 .8963 .9047 .9131 .9216 .9301 .9387 .9473 .956 .9647 .9734 .9823 .9911 1;
9796

98-
@return if($yiq >= $yiq-contrasted-threshold, $dark, $light);
97+
@function color-contrast($background, $color-contrast-dark: $color-contrast-dark, $color-contrast-light: $color-contrast-light) {
98+
$l1: luminance($background);
99+
$l2: luminance(opaque($background, $color-contrast-light));
100+
101+
$contrast: if($l1 > $l2, ($l1 + .05) / ($l2 + .05), ($l2 + .05) / ($l1 + .05));
102+
103+
@return if($contrast < $min-contrast-ratio, $color-contrast-dark, $color-contrast-light);
104+
}
105+
106+
// Return WCAG2.0 relative luminance
107+
// See https://www.w3.org/WAI/GL/wiki/Relative_luminance
108+
// See https://www.w3.org/TR/WCAG20-TECHS/G17.html#G17-tests
109+
@function luminance($color) {
110+
$rgb: (
111+
"r": red($color),
112+
"g": green($color),
113+
"b": blue($color)
114+
);
115+
116+
@each $name, $value in $rgb {
117+
$value: if($value / 255 < .03928, $value / 255 / 12.92, nth($_luminance-list, $value + 1));
118+
$rgb: map-merge($rgb, ($name: $value));
119+
}
120+
121+
@return (map-get($rgb, "r") * .2126) + (map-get($rgb, "g") * .7152) + (map-get($rgb, "b") * .0722);
122+
}
123+
124+
// Return opaque color
125+
// opaque(#fff, rgba(0, 0, 0, .5)) => #808080
126+
@function opaque($background, $foreground) {
127+
@return mix(rgba($foreground, 1), $background, opacity($foreground) * 100);
99128
}
100129

101130
// Request a color level

scss/_variables.scss

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ $theme-colors: (
7979
) !default;
8080

8181
// Set a specific jump point for requesting color jumps
82-
$theme-color-interval: 8% !default;
82+
$theme-color-interval: 8% !default;
8383

84-
// The yiq lightness value that determines when the lightness of color changes from "dark" to "light". Acceptable values are between 0 and 255.
85-
$yiq-contrasted-threshold: 150 !default;
84+
// The contrast ratio to reach against white, to determine if color changes from "light" to "dark". Acceptable values for WCAG 2.0 are 3, 4.5 and 7.
85+
// See https://www.w3.org/TR/WCAG20/#visual-audio-contrast-contrast
86+
$min-contrast-ratio: 3 !default;
8687

87-
// Customize the light and dark text colors for use in our YIQ color contrast function.
88-
$yiq-text-dark: $gray-900 !default;
89-
$yiq-text-light: $white !default;
88+
// Customize the light and dark text colors for use in our color contrast function.
89+
$color-contrast-dark: $gray-900 !default;
90+
$color-contrast-light: $white !default;
9091

9192
// fusv-disable
9293
$blue-100: tint-color($blue, 8) !default;

scss/mixins/_buttons.scss

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
@mixin button-variant(
77
$background,
88
$border,
9-
$color: color-yiq($background),
9+
$color: color-contrast($background),
1010
$hover-background: darken($background, 7.5%),
1111
$hover-border: darken($border, 10%),
12-
$hover-color: color-yiq($hover-background),
12+
$hover-color: color-contrast($hover-background),
1313
$active-background: darken($background, 10%),
1414
$active-border: darken($border, 12.5%),
15-
$active-color: color-yiq($active-background)
15+
$active-color: color-contrast($active-background)
1616
) {
1717
color: $color;
1818
@include gradient-bg($background);
@@ -69,10 +69,10 @@
6969

7070
@mixin button-outline-variant(
7171
$color,
72-
$color-hover: color-yiq($color),
72+
$color-hover: color-contrast($color),
7373
$active-background: $color,
7474
$active-border: $color,
75-
$active-color: color-yiq($active-background)
75+
$active-color: color-contrast($active-background)
7676
) {
7777
color: $color;
7878
border-color: $color;

scss/mixins/_forms.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
margin-top: .1rem;
3333
@include font-size($form-feedback-tooltip-font-size);
3434
line-height: $form-feedback-tooltip-line-height;
35-
color: color-yiq($color);
35+
color: color-contrast($color);
3636
background-color: rgba($color, $form-feedback-tooltip-opacity);
3737
@include border-radius($form-feedback-tooltip-border-radius);
3838
}

0 commit comments

Comments
 (0)