1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-02-23 00:39:54 +00:00

Code simplification

This commit is contained in:
Martijn Cuppens 2020-04-14 18:51:21 +02:00 committed by Gaël Poupard
parent 5b480fcc80
commit e04bbe99ea

View File

@ -98,42 +98,29 @@ $_luminance-list: .0008 .001 .0011 .0013 .0015 .0017 .002 .0022 .0025 .0027 .003
@function color-contrast($background, $color-contrast-dark: $color-contrast-dark, $color-contrast-light: $color-contrast-light, $min-contrast-ratio: $min-contrast-ratio) { @function color-contrast($background, $color-contrast-dark: $color-contrast-dark, $color-contrast-light: $color-contrast-light, $min-contrast-ratio: $min-contrast-ratio) {
$foregrounds: $color-contrast-light, $color-contrast-dark, $white, $black; $foregrounds: $color-contrast-light, $color-contrast-dark, $white, $black;
$contrast-ratios: (); $max-ratio: 0;
$max-ratio-color: null;
@each $color in $foregrounds { @each $color in $foregrounds {
$contrast-ratio: contrast-ratio($background, $color); $contrast-ratio: contrast-ratio($background, $color);
@if $contrast-ratio > $min-contrast-ratio { @if $contrast-ratio > $min-contrast-ratio {
@return $color; @return $color;
} @else { } @else if $contrast-ratio > $max-ratio {
$contrast-ratios: map-merge(($contrast-ratio: $color), $contrast-ratios); $max-ratio: $contrast-ratio;
$max-ratio-color: $color;
} }
} }
@warn "Found no color leading to #{$min-contrast-ratio}:1 contrast ratio against #{$background}"; @warn "Found no color leading to #{$min-contrast-ratio}:1 contrast ratio against #{$background}";
@return most-contrasted($contrast-ratios); @return $max-ratio-color;
} }
@function contrast-ratio($background, $foreground: $color-contrast-light) { @function contrast-ratio($background, $foreground: $color-contrast-light) {
$l1: luminance($background); $l1: luminance($background);
$l2: luminance(opaque($background, $foreground)); $l2: luminance(opaque($background, $foreground));
$contrast: if($l1 > $l2, ($l1 + .05) / ($l2 + .05), ($l2 + .05) / ($l1 + .05)); @return if($l1 > $l2, ($l1 + .05) / ($l2 + .05), ($l2 + .05) / ($l1 + .05));
@return $contrast;
}
// Returns the most contrasted color in a map of insufficiently contrasted colors
@function most-contrasted($contrast-ratios) {
$ratios: ();
@each $ratio, $color in $contrast-ratios {
$ratios: append($ratios, $ratio);
}
$highest-ratio: max($ratios...);
@return map-get($contrast-ratios, $highest-ratio);
} }
// Return WCAG2.0 relative luminance // Return WCAG2.0 relative luminance