-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[connect] Update background and loading spinner colors (#9670)
* update background and loading indicator colors * lint * fix test * internal ColorUtils
- Loading branch information
1 parent
ef97c7e
commit 4859229
Showing
7 changed files
with
164 additions
and
2 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
connect/src/main/java/com/stripe/android/connect/util/ColorUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.stripe.android.connect.util | ||
|
||
import android.graphics.Color | ||
import androidx.annotation.ColorInt | ||
import androidx.core.graphics.ColorUtils | ||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
/** | ||
* Returns a color for minimum contrast with a given background color | ||
* | ||
* Reference: [WCAG 2.1 Contrast Minimum](https://www.w3.org/WAI/WCAG21/Understanding/contrast-minimum.html#dfn-contrast-ratio) | ||
* | ||
* @param color The background color with which to get minimum contrast | ||
* @param minimumRatio The minimum contrast ratio (defaults to WCAG minimum ratio of 4.5) | ||
* @return The adjusted color that meets the minimum contrast ratio | ||
*/ | ||
@Suppress("MagicNumber", "ComplexCondition") | ||
@ColorInt | ||
internal fun getContrastingColor(@ColorInt color: Int, minimumRatio: Float = 4.5f): Int { | ||
var adjustedColor = color | ||
|
||
val shouldLighten = ColorUtils.calculateLuminance(color) < 0.5 | ||
val hsv = FloatArray(3) | ||
Color.colorToHSV(adjustedColor, hsv) | ||
|
||
while ( | ||
ColorUtils.calculateContrast(adjustedColor, color) < minimumRatio && | ||
((shouldLighten && hsv[2] < 1f) || (!shouldLighten && hsv[2] > 0f)) | ||
) { | ||
if (shouldLighten) { | ||
hsv[2] = min(1f, hsv[2] + HSV_VALUE_STEP_SIZE) | ||
} else { | ||
hsv[2] = max(0f, hsv[2] - HSV_VALUE_STEP_SIZE) | ||
} | ||
adjustedColor = Color.HSVToColor(hsv) | ||
} | ||
|
||
return adjustedColor | ||
} | ||
|
||
private const val HSV_VALUE_STEP_SIZE = 0.1f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
connect/src/test/java/com/stripe/android/connect/util/GetContrastingColorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.stripe.android.connect.util | ||
|
||
import android.graphics.Color | ||
import androidx.core.graphics.ColorUtils | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
|
||
@RunWith(RobolectricTestRunner::class) | ||
class GetContrastingColorTest { | ||
@Test | ||
fun `should not loop infinitely`() { | ||
val midGray = Color.argb(1f, .5f, .5f, .5f) | ||
|
||
// The maximum contrast ratio to mid-gray is 5.28, ensure that this | ||
// returns a color with the maximum contrast ratio that can be achieved | ||
val color = getContrastingColor(midGray, 5.5f) | ||
assertThat(ColorUtils.calculateContrast(color, midGray)).isLessThan(5.5) | ||
assertThat(color).isEqualTo(Color.WHITE) | ||
} | ||
|
||
@Test | ||
fun `should return a contrasting color`() { | ||
listOf( | ||
Color.WHITE, | ||
Color.LTGRAY, | ||
Color.CYAN, | ||
Color.DKGRAY, | ||
Color.BLACK, | ||
).forEach { bgColor -> | ||
val color = getContrastingColor(bgColor, 4.5f) | ||
val contrast = ColorUtils.calculateContrast(color, bgColor) | ||
assertThat(contrast).isGreaterThan(4.5) | ||
assertThat(contrast).isLessThan(6.0) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters