Skip to content

Commit 3aa424a

Browse files
david-allisonmikehardy
authored andcommitted
fix(card-browser): crashed in Dark Mode
We had an assertion that the pressed color != the color, and this failed if the color was black In this case, we lighten the color by 25% Fixes 17731
1 parent 3077325 commit 3aa424a

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

AnkiDroid/src/main/java/com/ichi2/anki/browser/BrowserMultiColumnAdapter.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ import com.ichi2.anki.AnkiDroidApp.Companion.sharedPrefs
3838
import com.ichi2.anki.Flag
3939
import com.ichi2.anki.R
4040
import com.ichi2.anki.utils.android.darkenColor
41+
import com.ichi2.anki.utils.android.lightenColorAbsolute
4142
import com.ichi2.anki.utils.ext.findViewById
43+
import com.ichi2.annotations.NeedsTest
4244
import net.ankiweb.rsdroid.BackendException
4345
import timber.log.Timber
4446
import kotlin.math.abs
@@ -119,10 +121,19 @@ class BrowserMultiColumnAdapter(
119121
checkBoxView.isChecked = value
120122
}
121123

124+
@NeedsTest("17731 - maybe check all activities load in dark mode, at least check this code")
122125
fun setColor(
123126
@ColorInt color: Int,
124127
) {
125-
val pressedColor = darkenColor(color, 0.85f)
128+
var pressedColor = darkenColor(color, 0.85f)
129+
130+
if (pressedColor == color) {
131+
// if the color is black, we can't darken it.
132+
// A non-black background looks unusual, so the 'press' should lighten the color
133+
134+
// 25% was determined by visual inspection
135+
pressedColor = lightenColorAbsolute(pressedColor, 0.25f)
136+
}
126137

127138
require(pressedColor != color)
128139
val rippleDrawable =

common/src/main/java/com/ichi2/anki/utils/android/ColorUtils.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@ fun darkenColor(
3939
return Color.HSVToColor(hsv)
4040
}
4141

42+
/**
43+
* Lightens the provided ARGB color by a provided [amount]
44+
*
45+
* @param argb The ARGB color to transform
46+
* @param amount Amount to lighten, between 0.0f (no change) and 1.0f (100% brightness)
47+
* @return The lightened color in ARGB
48+
*/
49+
@ColorInt
50+
fun lightenColorAbsolute(
51+
@ColorInt argb: Int,
52+
amount: Float,
53+
): Int {
54+
val hsv = argb.toHSV()
55+
// https://en.wikipedia.org/wiki/HSL_and_HSV
56+
// The third component is the 'value', or 'lightness/darkness'
57+
hsv[2] = (hsv[2] + amount).clamp(0f, 1f)
58+
return Color.HSVToColor(hsv)
59+
}
60+
4261
/**
4362
* Converts an ARGB color to an array of its HSV components
4463
*

0 commit comments

Comments
 (0)