Skip to content

Commit e04bbe9

Browse files
MartijnCuppensffoodd
authored andcommitted
Code simplification
1 parent 5b480fc commit e04bbe9

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

scss/_functions.scss

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,42 +98,29 @@ $_luminance-list: .0008 .001 .0011 .0013 .0015 .0017 .002 .0022 .0025 .0027 .003
9898

9999
@function color-contrast($background, $color-contrast-dark: $color-contrast-dark, $color-contrast-light: $color-contrast-light, $min-contrast-ratio: $min-contrast-ratio) {
100100
$foregrounds: $color-contrast-light, $color-contrast-dark, $white, $black;
101-
$contrast-ratios: ();
101+
$max-ratio: 0;
102+
$max-ratio-color: null;
102103

103104
@each $color in $foregrounds {
104105
$contrast-ratio: contrast-ratio($background, $color);
105106
@if $contrast-ratio > $min-contrast-ratio {
106107
@return $color;
107-
} @else {
108-
$contrast-ratios: map-merge(($contrast-ratio: $color), $contrast-ratios);
108+
} @else if $contrast-ratio > $max-ratio {
109+
$max-ratio: $contrast-ratio;
110+
$max-ratio-color: $color;
109111
}
110112
}
111113

112114
@warn "Found no color leading to #{$min-contrast-ratio}:1 contrast ratio against #{$background}";
113115

114-
@return most-contrasted($contrast-ratios);
116+
@return $max-ratio-color;
115117
}
116118

117119
@function contrast-ratio($background, $foreground: $color-contrast-light) {
118120
$l1: luminance($background);
119121
$l2: luminance(opaque($background, $foreground));
120122

121-
$contrast: if($l1 > $l2, ($l1 + .05) / ($l2 + .05), ($l2 + .05) / ($l1 + .05));
122-
123-
@return $contrast;
124-
}
125-
126-
// Returns the most contrasted color in a map of insufficiently contrasted colors
127-
@function most-contrasted($contrast-ratios) {
128-
$ratios: ();
129-
130-
@each $ratio, $color in $contrast-ratios {
131-
$ratios: append($ratios, $ratio);
132-
}
133-
134-
$highest-ratio: max($ratios...);
135-
136-
@return map-get($contrast-ratios, $highest-ratio);
123+
@return if($l1 > $l2, ($l1 + .05) / ($l2 + .05), ($l2 + .05) / ($l1 + .05));
137124
}
138125

139126
// Return WCAG2.0 relative luminance

0 commit comments

Comments
 (0)