|
16 | 16 | import { |
17 | 17 | BaseException, |
18 | 18 | FeatureTest, |
19 | | - MathClamp, |
20 | 19 | shadow, |
21 | 20 | Util, |
22 | 21 | warn, |
@@ -931,18 +930,36 @@ function findContrastColor(baseColor, fixedColor) { |
931 | 930 | // Use the contrast ratio requirements from WCAG 2.1. |
932 | 931 | // https://www.w3.org/TR/WCAG21/#contrast-minimum |
933 | 932 | // https://www.w3.org/TR/WCAG21/#contrast-enhanced |
934 | | - const minContrast = isFixedColorDark ? 7 : 4.5; |
| 933 | + const minContrast = isFixedColorDark ? 12 : 4.5; |
935 | 934 |
|
936 | 935 | baseHSL[2] = isFixedColorDark |
937 | 936 | ? Math.sqrt(baseHSL[2]) |
938 | 937 | : 1 - Math.sqrt(1 - baseHSL[2]); |
939 | | - const increment = isFixedColorDark ? 0.01 : -0.01; |
940 | | - let contrast = contrastRatio(baseHSL, fixedHSL, output); |
941 | | - while (baseHSL[2] >= 0 && baseHSL[2] <= 1 && contrast < minContrast) { |
942 | | - baseHSL[2] += increment; |
943 | | - contrast = contrastRatio(baseHSL, fixedHSL, output); |
| 938 | + |
| 939 | + if (contrastRatio(baseHSL, fixedHSL, output) < minContrast) { |
| 940 | + let start, end; |
| 941 | + if (isFixedColorDark) { |
| 942 | + start = baseHSL[2]; |
| 943 | + end = 1; |
| 944 | + } else { |
| 945 | + start = 0; |
| 946 | + end = baseHSL[2]; |
| 947 | + } |
| 948 | + const PRECISION = 0.005; |
| 949 | + while (end - start > PRECISION) { |
| 950 | + const mid = (baseHSL[2] = (start + end) / 2); |
| 951 | + if ( |
| 952 | + isFixedColorDark === |
| 953 | + contrastRatio(baseHSL, fixedHSL, output) < minContrast |
| 954 | + ) { |
| 955 | + start = mid; |
| 956 | + } else { |
| 957 | + end = mid; |
| 958 | + } |
| 959 | + } |
| 960 | + baseHSL[2] = isFixedColorDark ? end : start; |
944 | 961 | } |
945 | | - baseHSL[2] = MathClamp(baseHSL[2], 0, 1); |
| 962 | + |
946 | 963 | HSLToRGB(baseHSL, output); |
947 | 964 | cachedValue = Util.makeHexColor( |
948 | 965 | Math.round(output[0] * 255), |
|
0 commit comments