-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path_contrast.scss
53 lines (41 loc) · 1.55 KB
/
_contrast.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
@function ch-color-luminance($color) {
// Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
// Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
$rgba: red($color), green($color), blue($color);
$rgba2: ();
@for $i from 1 through 3 {
$rgb: nth($rgba, $i);
$rgb: $rgb / 255;
$rgb: if($rgb < .03928, $rgb / 12.92, ch-pow(($rgb + .055) / 1.055, 2.4, 16));
$rgba2: append($rgba2, $rgb);
}
@return .2126 * nth($rgba2, 1) + .7152 * nth($rgba2, 2) + 0.0722 * nth($rgba2, 3);
}
@function ch-color-contrast($color1, $color2) {
// Adapted from: https://github.com/LeaVerou/contrast-ratio/blob/gh-pages/color.js
// Formula: http://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef
$luminance1: ch-color-luminance($color1) + .05;
$luminance2: ch-color-luminance($color2) + .05;
$ratio: $luminance1 / $luminance2;
@if $luminance2 > $luminance1 {
$ratio: 1 / $ratio;
}
$ratio: round($ratio * 10) / 10;
@return $ratio;
}
@function ch-best-color-contrast($base, $colors: (#fff, #000), $tolerance: 0) {
$best: nth($colors, 1);
$contrast: ch-color-contrast($base, $best);
@for $i from 2 through length($colors) {
$currentColor: nth($colors, $i);
$currentContrast: ch-color-contrast($base, $currentColor);
@if ($currentContrast - $contrast > $tolerance) {
$best: $currentColor;
$contrast: $currentContrast;
}
}
@if ($contrast < 3) {
@warn "Contrast ratio of #{$best} on #{$base} is pretty bad, just #{$contrast}";
}
@return $best;
}