Skip to content

Commit

Permalink
fix(card-browser): crashed in Dark Mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
david-allison authored and mikehardy committed Jan 6, 2025
1 parent 3077325 commit 3aa424a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ import com.ichi2.anki.AnkiDroidApp.Companion.sharedPrefs
import com.ichi2.anki.Flag
import com.ichi2.anki.R
import com.ichi2.anki.utils.android.darkenColor
import com.ichi2.anki.utils.android.lightenColorAbsolute
import com.ichi2.anki.utils.ext.findViewById
import com.ichi2.annotations.NeedsTest
import net.ankiweb.rsdroid.BackendException
import timber.log.Timber
import kotlin.math.abs
Expand Down Expand Up @@ -119,10 +121,19 @@ class BrowserMultiColumnAdapter(
checkBoxView.isChecked = value
}

@NeedsTest("17731 - maybe check all activities load in dark mode, at least check this code")
fun setColor(
@ColorInt color: Int,
) {
val pressedColor = darkenColor(color, 0.85f)
var pressedColor = darkenColor(color, 0.85f)

if (pressedColor == color) {
// if the color is black, we can't darken it.
// A non-black background looks unusual, so the 'press' should lighten the color

// 25% was determined by visual inspection
pressedColor = lightenColorAbsolute(pressedColor, 0.25f)
}

require(pressedColor != color)
val rippleDrawable =
Expand Down
19 changes: 19 additions & 0 deletions common/src/main/java/com/ichi2/anki/utils/android/ColorUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,25 @@ fun darkenColor(
return Color.HSVToColor(hsv)
}

/**
* Lightens the provided ARGB color by a provided [amount]
*
* @param argb The ARGB color to transform
* @param amount Amount to lighten, between 0.0f (no change) and 1.0f (100% brightness)
* @return The lightened color in ARGB
*/
@ColorInt
fun lightenColorAbsolute(
@ColorInt argb: Int,
amount: Float,
): Int {
val hsv = argb.toHSV()
// https://en.wikipedia.org/wiki/HSL_and_HSV
// The third component is the 'value', or 'lightness/darkness'
hsv[2] = (hsv[2] + amount).clamp(0f, 1f)
return Color.HSVToColor(hsv)
}

/**
* Converts an ARGB color to an array of its HSV components
*
Expand Down

0 comments on commit 3aa424a

Please sign in to comment.