|
1 | | -@use "sass:color"; |
2 | | -@use "sass:map"; |
3 | | -@use "sass:math"; |
4 | | -@use "math" as *; |
| 1 | +@use "contrast-ratio" as *; |
| 2 | +@use "../variables" as *; |
5 | 3 |
|
6 | 4 | // Color contrast |
7 | 5 | // See https://github.com/twbs/bootstrap/pull/30168 |
8 | 6 |
|
9 | | - |
10 | | -@function color-contrast($background, $color-contrast-dark, $color-contrast-light, $white, $black, $min-contrast-ratio) { |
| 7 | +@function color-contrast($background, $color-contrast-dark: $color-contrast-dark, $color-contrast-light: $color-contrast-light, $min-contrast-ratio: $min-contrast-ratio) { |
11 | 8 | $foregrounds: $color-contrast-light, $color-contrast-dark, $white, $black; |
12 | 9 | $max-ratio: 0; |
13 | 10 | $max-ratio-color: null; |
|
26 | 23 |
|
27 | 24 | @return $max-ratio-color; |
28 | 25 | } |
29 | | - |
30 | | -@function contrast-ratio($background, $foreground) { |
31 | | - $l1: luminance($background); |
32 | | - $l2: luminance(opaque($background, $foreground)); |
33 | | - |
34 | | - @return if($l1 > $l2, divide($l1 + .05, $l2 + .05), divide($l2 + .05, $l1 + .05)); |
35 | | -} |
36 | | - |
37 | | -// Return WCAG2.2 relative luminance |
38 | | -// See https://www.w3.org/TR/WCAG/#dfn-relative-luminance |
39 | | -// See https://www.w3.org/TR/WCAG/#dfn-contrast-ratio |
40 | | -@function luminance($color) { |
41 | | - $rgb: ( |
42 | | - "r": color.channel($color, "red", $space: rgb), // stylelint-disable-line scss/at-function-named-arguments |
43 | | - "g": color.channel($color, "green", $space: rgb), // stylelint-disable-line scss/at-function-named-arguments |
44 | | - "b": color.channel($color, "blue", $space: rgb) // stylelint-disable-line scss/at-function-named-arguments |
45 | | - ); |
46 | | - |
47 | | - @each $name, $value in $rgb { |
48 | | - $value: if(divide($value, 255) < .04045, divide(divide($value, 255), 12.92), math.pow(divide((divide(math.round($value) + 1, 255) + .055), 1.055), 2.4)); |
49 | | - $rgb: map.merge($rgb, ($name: $value)); |
50 | | - } |
51 | | - |
52 | | - @return (map.get($rgb, "r") * .2126) + (map.get($rgb, "g") * .7152) + (map.get($rgb, "b") * .0722); |
53 | | -} |
54 | | - |
55 | | -// Return opaque color |
56 | | -// opaque(#fff, rgba(0, 0, 0, .5)) => #808080 |
57 | | -@function opaque($background, $foreground) { |
58 | | - @return color.mix(rgba($foreground, 1), $background, color.opacity($foreground) * 100%); |
59 | | -} |
0 commit comments