minesweeper: fixed light/dark number palette with distinct colours (#ux-audit)
- Commit
8339163ec72e293c5ccabca01c6b2f8b3ca20969- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 01:22 UTC (23 days ago)
- Parents
4b781de
Symptom: in the dark theme the neighbour counts were hard to tell apart - 1, 2 and 4 came out as similar pastel tones, and 5 through 8 all shared one colour - so players had to read the digit shape to distinguish counts the colour is supposed to signal at a glance. Root cause: countColor() mapped counts onto theme roles (primary, tertiary, error, secondary, onSurface for everything above 4). Those roles are not designed to be mutually distinct, and in dark schemes (and dynamic Material You palettes) several converge. Fix: countColor(count, dark) returns from a fixed palette of eight classic hues - blue, green, red, purple, amber, cyan, near-black, grey - with a light variant and a dark variant, each colour at least 4:1 against the revealed-cell background of its variant. The variant is chosen from the luminance of the revealed-cell background (surfaceContainerLowest) rather than the system dark-mode flag, so an in-app theme override is respected. Counts outside 1..8 clamp. User-visible effect: every count 1-8 has its own colour in both themes; light-theme colours stay close to the classic look. Verification: new CountPaletteTest asserts eight distinct colours per variant, >= 4:1 contrast against M3 light and dark container backgrounds, and clamping. :games:minesweeper:testDevUnitTest in the toolchain container.
Diff
games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt
diff --git a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.ktindex 05d320d2a8..11a405136a 100644--- a/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt+++ b/games/minesweeper/src/main/java/com/vayunmathur/games/minesweeper/ui/components/MineFieldGrid.kt@@ -14,6 +14,7 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.luminance import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.platform.LocalDensity import androidx.compose.ui.platform.LocalHapticFeedback@@ -34,7 +35,6 @@ import androidx.compose.ui.unit.sp import com.vayunmathur.games.minesweeper.R import com.vayunmathur.games.minesweeper.data.GameOutcome import com.vayunmathur.games.minesweeper.data.MinesweeperGameState-import com.vayunmathur.library.ui.ColorScheme import com.vayunmathur.library.ui.IconFlag import com.vayunmathur.library.ui.IconMine import com.vayunmathur.library.ui.MaterialTheme@@ -169,7 +169,7 @@ private fun MineCell( revealed && count > 0 -> Text( text = count.toString(),- color = countColor(count, scheme),+ color = countColor(count, dark = scheme.surfaceContainerLowest.luminance() < HALF), fontWeight = FontWeight.Bold, // 15sp follows the font setting only until the digit fills the cell: a Large // field's ~27dp cells overflowed at 200% font, and the number spilled out.@@ -183,6 +183,9 @@ private fun MineCell( /** Gap between cells, so the field reads as a grid without needing drawn lines. */ private val CellGap = 1.dp +/** Luminance below which the revealed-cell background counts as dark. */+private const val HALF = 0.5f+ /** A count never renders taller than this share of its cell. */ private const val COUNT_CELL_FRACTION = 0.7f @@ -197,14 +200,24 @@ private fun Density.fitSp(preferred: TextUnit, cap: Dp): TextUnit = if (preferred.toPx() > cap.toPx()) cap.toSp() else preferred /**- * The traditional per-number colours, mapped onto theme roles so they still work in both light and- * dark and against a dynamic palette. Beyond four the numbers are rare enough that one shared colour- * is fine, and the count is bold enough to read regardless.+ * The traditional per-number colours, as a fixed palette with a light and a dark variant.+ *+ * They used to be theme roles (primary, tertiary, error, secondary, then onSurface for 5-8). In the+ * dark scheme several of those land on near-identical pastel tones, so 1, 2 and 4 were hard to tell+ * apart, and 5-8 were all one colour. A fixed palette keeps the classic hues - blue, green, red,+ * purple, amber, cyan, near-black, grey - eight distinct colours, each at least 4:1 against the+ * revealed-cell background of its variant. The variant follows the cell background rather than the+ * system setting, so an app-level theme override is respected. */-private fun countColor(count: Int, scheme: ColorScheme): Color = when (count) {- 1 -> scheme.primary- 2 -> scheme.tertiary- 3 -> scheme.error- 4 -> scheme.secondary- else -> scheme.onSurface-}+internal fun countColor(count: Int, dark: Boolean): Color =+ (if (dark) DarkCounts else LightCounts)[(count - 1).coerceIn(0, LightCounts.lastIndex)]++private val LightCounts = listOf(+ Color(0xFF1565C0), Color(0xFF2E7D32), Color(0xFFC62828), Color(0xFF6A1B9A),+ Color(0xFFA15C00), Color(0xFF00838F), Color(0xFF212121), Color(0xFF757575),+)++private val DarkCounts = listOf(+ Color(0xFF82B1FF), Color(0xFF81C784), Color(0xFFFF8A80), Color(0xFFCE93D8),+ Color(0xFFFFB74D), Color(0xFF4DD0E1), Color(0xFFF5F5F5), Color(0xFF9E9E9E),+)
games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/components/CountPaletteTest.kt
diff --git a/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/components/CountPaletteTest.kt b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/components/CountPaletteTest.ktnew file mode 100644index 0000000000..ded5a8fb2b--- /dev/null+++ b/games/minesweeper/src/test/java/com/vayunmathur/games/minesweeper/ui/components/CountPaletteTest.kt@@ -0,0 +1,41 @@+package com.vayunmathur.games.minesweeper.ui.components++import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.luminance+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertTrue++/** The neighbour-count palette: eight distinct, readable colours in both variants. */+class CountPaletteTest {++ private fun contrast(a: Color, b: Color): Float {+ val (hi, lo) = listOf(a.luminance(), b.luminance()).sortedDescending()+ return (hi + 0.05f) / (lo + 0.05f)+ }++ @Test+ fun eachVariantHasEightDistinctColours() {+ for (dark in listOf(false, true)) {+ val colours = (1..8).map { countColor(it, dark) }+ assertEquals(8, colours.toSet().size, "dark=$dark")+ }+ }++ @Test+ fun coloursContrastWithTheRevealedCellBackground() {+ // Material 3 baseline surfaceContainerLowest (light) and a dark surface container.+ val lightBg = Color(0xFFF7F2FA)+ val darkBg = Color(0xFF1D1B20)+ for (count in 1..8) {+ assertTrue(contrast(countColor(count, dark = false), lightBg) >= 4f, "light $count")+ assertTrue(contrast(countColor(count, dark = true), darkBg) >= 4f, "dark $count")+ }+ }++ @Test+ fun outOfRangeCountsClamp() {+ assertEquals(countColor(1, dark = true), countColor(0, dark = true))+ assertEquals(countColor(8, dark = false), countColor(9, dark = false))+ }+}