Skip to content

Commit bd60db6

Browse files
authored
fix(colors): handle translucent accent when picking action key icon color (#913)
calculateContrast throws for a translucent color, which crashed the color settings when the accent had transparency. Blend a translucent accent over the keyboard background before the check; opaque accents are unchanged. Also return an opaque color from KeyboardView.getCompositeColor for a fully transparent key. Fixes #909 Co-authored-by: LeanBitLab <leanbitlab@users.noreply.github.com>
1 parent fb536d1 commit bd60db6

3 files changed

Lines changed: 63 additions & 9 deletions

File tree

‎app/src/main/java/helium314/keyboard/keyboard/KeyboardView.kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ open class KeyboardView @JvmOverloads constructor(
763763
private fun getCompositeColor(srcColor: Int, dstColor: Int): Int {
764764
val alpha = (srcColor ushr 24) and 0xFF
765765
if (alpha == 0xFF) return srcColor
766-
if (alpha == 0x00) return dstColor
766+
if (alpha == 0x00) return dstColor or 0xFF000000.toInt() // result is used as an opaque contrast background
767767
val fAlpha = alpha / 255f
768768
val srcR = (srcColor ushr 16) and 0xFF; val srcG = (srcColor ushr 8) and 0xFF; val srcB = srcColor and 0xFF
769769
val dstR = (dstColor ushr 16) and 0xFF; val dstG = (dstColor ushr 8) and 0xFF; val dstB = dstColor and 0xFF

‎app/src/main/java/helium314/keyboard/latin/common/Colors.kt‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ class DynamicColors(context: Context, override val themeStyle: String, override
316316
actionKeyIconColorFilter = when {
317317
themeStyle == STYLE_HOLO -> keyTextFilter
318318
// the white icon may not have enough contrast, and can't be adjusted by the user
319-
isLightAccent(accent) -> colorFilter(Color.DKGRAY)
319+
isLightAccent(accent, background) -> colorFilter(Color.DKGRAY)
320320
else -> colorFilter(Color.WHITE)
321321
}
322322
}
@@ -342,7 +342,7 @@ class DynamicColors(context: Context, override val themeStyle: String, override
342342
NAVIGATION_BAR -> navBar
343343
MORE_SUGGESTIONS_HINT, SUGGESTED_WORD, SUGGESTION_TYPED_WORD, SUGGESTION_VALID_WORD -> adjustedKeyText
344344
TOOL_BAR_EXPAND_KEY -> Color.WHITE
345-
ACTION_KEY_ICON -> if (isLightAccent(accent)) Color.DKGRAY else Color.WHITE
345+
ACTION_KEY_ICON -> if (isLightAccent(accent, background)) Color.DKGRAY else Color.WHITE
346346
EDIT_MODE_DELETE_BACKGROUND -> androidx.core.graphics.ColorUtils.blendARGB(accent, functionalKey, 0.4f)
347347
EDIT_MODE_FUNC_BACKGROUND -> androidx.core.graphics.ColorUtils.blendARGB(accent, functionalKey, 0.2f)
348348
EDIT_MODE_ALPHA_BACKGROUND -> androidx.core.graphics.ColorUtils.blendARGB(accent, functionalKey, 0.6f)
@@ -549,7 +549,7 @@ class DefaultColors (
549549
actionKeyIconColorFilter = when {
550550
themeStyle == STYLE_HOLO -> keyTextFilter
551551
// the white icon may not have enough contrast, and can't be adjusted by the user
552-
isLightAccent(accent) -> colorFilter(Color.DKGRAY)
552+
isLightAccent(accent, background) -> colorFilter(Color.DKGRAY)
553553
else -> colorFilter(Color.WHITE)
554554
}
555555
}
@@ -575,7 +575,7 @@ class DefaultColors (
575575
SUGGESTION_AUTO_CORRECT, EMOJI_CATEGORY, TOOL_BAR_EXPAND_KEY, ONE_HANDED_MODE_BUTTON -> suggestionText
576576
TOOL_BAR_KEY -> toolbarKeyOverride ?: suggestionText
577577
MORE_SUGGESTIONS_HINT, SUGGESTED_WORD, SUGGESTION_TYPED_WORD, SUGGESTION_VALID_WORD -> adjustedSuggestionText
578-
ACTION_KEY_ICON -> if (isLightAccent(accent)) Color.DKGRAY else Color.WHITE
578+
ACTION_KEY_ICON -> if (isLightAccent(accent, background)) Color.DKGRAY else Color.WHITE
579579
EDIT_MODE_DELETE_BACKGROUND -> androidx.core.graphics.ColorUtils.blendARGB(accent, functionalKey, 0.4f)
580580
EDIT_MODE_FUNC_BACKGROUND -> androidx.core.graphics.ColorUtils.blendARGB(accent, functionalKey, 0.2f)
581581
EDIT_MODE_ALPHA_BACKGROUND -> androidx.core.graphics.ColorUtils.blendARGB(accent, functionalKey, 0.6f)
@@ -678,7 +678,7 @@ class AllColors(private val colorMap: EnumMap<ColorType, Int>, override val them
678678
override fun get(color: ColorType): Int {
679679
if (color == ColorType.ACTION_KEY_ICON && !colorMap.containsKey(ColorType.ACTION_KEY_ICON)) {
680680
val accent = get(ColorType.ACTION_KEY_BACKGROUND)
681-
return if (isLightAccent(accent)) Color.DKGRAY else Color.WHITE
681+
return if (isLightAccent(accent, get(ColorType.MAIN_BACKGROUND))) Color.DKGRAY else Color.WHITE
682682
}
683683
return colorMap[color] ?: color.default()
684684
}
@@ -751,7 +751,7 @@ class AllColors(private val colorMap: EnumMap<ColorType, Int>, override val them
751751
val c = get(color)
752752
if (color == ColorType.ACTION_KEY_ICON && !colorMap.containsKey(ColorType.ACTION_KEY_ICON)) {
753753
val accent = get(ColorType.ACTION_KEY_BACKGROUND)
754-
if (isLightAccent(accent)) colorFilter(Color.DKGRAY) else colorFilter(Color.WHITE)
754+
if (isLightAccent(accent, get(ColorType.MAIN_BACKGROUND))) colorFilter(Color.DKGRAY) else colorFilter(Color.WHITE)
755755
} else {
756756
colorFilter(c)
757757
}
@@ -771,8 +771,16 @@ class AllColors(private val colorMap: EnumMap<ColorType, Int>, override val them
771771
}
772772
}
773773

774-
private fun isLightAccent(accent: Int): Boolean =
775-
isBrightColor(accent) || ColorUtils.calculateContrast(Color.DKGRAY, accent) > ColorUtils.calculateContrast(Color.WHITE, accent)
774+
/**
775+
* Whether the action key icon needs to be dark on [accent]. A translucent accent is judged by the color it shows
776+
* on the keyboard [background] (calculateContrast throws for a translucent color, #909). Opaque accents are unchanged.
777+
*/
778+
internal fun isLightAccent(accent: Int, background: Int): Boolean {
779+
val opaqueAccent = if (Color.alpha(accent) == 255) accent
780+
else ColorUtils.compositeColors(accent, ColorUtils.compositeColors(background, Color.BLACK))
781+
return isBrightColor(opaqueAccent) ||
782+
ColorUtils.calculateContrast(Color.DKGRAY, opaqueAccent) > ColorUtils.calculateContrast(Color.WHITE, opaqueAccent)
783+
}
776784

777785
private fun colorFilter(color: Int, mode: BlendModeCompat = BlendModeCompat.MODULATE): ColorFilter {
778786
// null is only returned for unsupported blend modes, which are not used here
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: GPL-3.0-only
2+
package helium314.keyboard.latin.common
3+
4+
import android.graphics.Color
5+
import androidx.core.graphics.ColorUtils
6+
import helium314.keyboard.latin.utils.isBrightColor
7+
import org.junit.runner.RunWith
8+
import org.robolectric.RobolectricTestRunner
9+
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
import kotlin.test.assertFalse
12+
import kotlin.test.assertTrue
13+
14+
@RunWith(RobolectricTestRunner::class)
15+
class ColorsTest {
16+
private val darkBackground = 0xff1b1b1f.toInt()
17+
private val lightBackground = 0xfff2f0f4.toInt()
18+
19+
@Test fun `translucent accent does not crash (#909)`() {
20+
// accent colors from the #909 crash reports
21+
for (accent in listOf(0x7379a5ff.toInt(), 0x9d79a5ff.toInt())) {
22+
isLightAccent(accent, darkBackground)
23+
isLightAccent(accent, lightBackground)
24+
isLightAccent(accent, 0x80000000.toInt()) // translucent background
25+
isLightAccent(accent, Color.TRANSPARENT)
26+
}
27+
isLightAccent(Color.TRANSPARENT, Color.TRANSPARENT)
28+
}
29+
30+
@Test fun `opaque accent gives the same result as before`() {
31+
val accents = listOf(Color.WHITE, Color.BLACK, Color.YELLOW, Color.BLUE, Color.RED, 0xff79a5ff.toInt(),
32+
0xff5e9e92.toInt(), 0xffd0bcff.toInt(), 0xff6750a4.toInt(), 0xffb3b3b3.toInt(), 0xff777777.toInt())
33+
for (accent in accents) {
34+
val before = isBrightColor(accent) ||
35+
ColorUtils.calculateContrast(Color.DKGRAY, accent) > ColorUtils.calculateContrast(Color.WHITE, accent)
36+
assertEquals(before, isLightAccent(accent, darkBackground), "accent ${Integer.toHexString(accent)}")
37+
assertEquals(before, isLightAccent(accent, lightBackground), "accent ${Integer.toHexString(accent)}")
38+
}
39+
}
40+
41+
@Test fun `translucent accent is judged by the color shown on the background`() {
42+
val faintWhite = 0x20ffffff
43+
assertFalse(isLightAccent(faintWhite, darkBackground)) // looks dark: white icon
44+
assertTrue(isLightAccent(faintWhite, lightBackground)) // looks light: dark icon
45+
}
46+
}

0 commit comments

Comments
 (0)