|
17 | 17 | package com.google.android.material.color.utilities; |
18 | 18 |
|
19 | 19 | import static com.google.android.material.color.utilities.ArgbSubject.assertThat; |
| 20 | +import static org.junit.Assert.assertTrue; |
20 | 21 |
|
21 | 22 | import org.junit.Test; |
22 | 23 | import org.junit.runner.RunWith; |
@@ -77,4 +78,87 @@ public void dynamicColor_withOpacity() { |
77 | 78 | final SchemeTonalSpot darkScheme = new SchemeTonalSpot(Hct.fromInt(0xff4285f4), true, 0.0); |
78 | 79 | assertThat(dynamicColor.getArgb(darkScheme)).isSameColorAs(0x33ffffff); |
79 | 80 | } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void respectsContrast() { |
| 84 | + final Hct[] seedColors = |
| 85 | + new Hct[] { |
| 86 | + Hct.fromInt(0xFFFF0000), |
| 87 | + Hct.fromInt(0xFFFFFF00), |
| 88 | + Hct.fromInt(0xFF00FF00), |
| 89 | + Hct.fromInt(0xFF0000FF) |
| 90 | + }; |
| 91 | + |
| 92 | + final double[] contrastLevels = {-1.0, -0.5, 0.0, 0.5, 1.0}; |
| 93 | + |
| 94 | + for (Hct seedColor : seedColors) { |
| 95 | + for (double contrastLevel : contrastLevels) { |
| 96 | + for (boolean isDark : new boolean[] {false, true}) { |
| 97 | + final DynamicScheme[] schemes = |
| 98 | + new DynamicScheme[] { |
| 99 | + new SchemeContent(seedColor, isDark, contrastLevel), |
| 100 | + new SchemeMonochrome(seedColor, isDark, contrastLevel), |
| 101 | + new SchemeTonalSpot(seedColor, isDark, contrastLevel), |
| 102 | + new SchemeFidelity(seedColor, isDark, contrastLevel) |
| 103 | + }; |
| 104 | + for (final DynamicScheme scheme : schemes) { |
| 105 | + assertTrue( |
| 106 | + pairSatisfiesContrast( |
| 107 | + scheme, MaterialDynamicColors.onPrimary, MaterialDynamicColors.primary)); |
| 108 | + assertTrue( |
| 109 | + pairSatisfiesContrast( |
| 110 | + scheme, |
| 111 | + MaterialDynamicColors.onPrimaryContainer, |
| 112 | + MaterialDynamicColors.primaryContainer)); |
| 113 | + assertTrue( |
| 114 | + pairSatisfiesContrast( |
| 115 | + scheme, MaterialDynamicColors.onSecondary, MaterialDynamicColors.secondary)); |
| 116 | + assertTrue( |
| 117 | + pairSatisfiesContrast( |
| 118 | + scheme, |
| 119 | + MaterialDynamicColors.onSecondaryContainer, |
| 120 | + MaterialDynamicColors.secondaryContainer)); |
| 121 | + assertTrue( |
| 122 | + pairSatisfiesContrast( |
| 123 | + scheme, MaterialDynamicColors.onTertiary, MaterialDynamicColors.tertiary)); |
| 124 | + assertTrue( |
| 125 | + pairSatisfiesContrast( |
| 126 | + scheme, |
| 127 | + MaterialDynamicColors.onTertiaryContainer, |
| 128 | + MaterialDynamicColors.tertiaryContainer)); |
| 129 | + assertTrue( |
| 130 | + pairSatisfiesContrast( |
| 131 | + scheme, MaterialDynamicColors.onError, MaterialDynamicColors.error)); |
| 132 | + assertTrue( |
| 133 | + pairSatisfiesContrast( |
| 134 | + scheme, |
| 135 | + MaterialDynamicColors.onErrorContainer, |
| 136 | + MaterialDynamicColors.errorContainer)); |
| 137 | + assertTrue( |
| 138 | + pairSatisfiesContrast( |
| 139 | + scheme, MaterialDynamicColors.onBackground, MaterialDynamicColors.background)); |
| 140 | + assertTrue( |
| 141 | + pairSatisfiesContrast( |
| 142 | + scheme, |
| 143 | + MaterialDynamicColors.onSurfaceVariant, |
| 144 | + MaterialDynamicColors.surfaceVariant)); |
| 145 | + assertTrue( |
| 146 | + pairSatisfiesContrast( |
| 147 | + scheme, |
| 148 | + MaterialDynamicColors.onSurfaceInverse, |
| 149 | + MaterialDynamicColors.surfaceInverse)); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + private boolean pairSatisfiesContrast(DynamicScheme scheme, DynamicColor fg, DynamicColor bg) { |
| 157 | + double fgTone = fg.getHct(scheme).getTone(); |
| 158 | + double bgTone = bg.getHct(scheme).getTone(); |
| 159 | + // TODO(b/270915664) - Fix inconsistencies. |
| 160 | + // TODO(b/270915664) - Minimum requirement should be 4.5 when not reducing contrast. |
| 161 | + double minimumRequirement = 3.0; |
| 162 | + return Contrast.ratioOfTones(fgTone, bgTone) >= minimumRequirement; |
| 163 | + } |
80 | 164 | } |
0 commit comments