Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ATTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,10 @@ Generated-in-code art is original and has no file to list: the UI icon set (`cra
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_appearance_settings_appearance_light_200.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_darkChosenOnLightPhone_settings_appearance_forced_dark_on_light_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_SettingsScreenshotTest_lightChosenOnDarkPhone_settings_appearance_forced_light_on_dark_100.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of Android Settings › Appearance (#1997) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_DarkRecolourScreenshotTest_boxedReply_recolour_boxed_dark.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android reading view's dark recolouring (MR8) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_DarkRecolourScreenshotTest_boxedReply_recolour_boxed_light.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android reading view's dark recolouring (MR8) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_DarkRecolourScreenshotTest_brandNewsletter_recolour_brand_dark.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android reading view's dark recolouring (MR8) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_DarkRecolourScreenshotTest_brandNewsletter_recolour_brand_light.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android reading view's dark recolouring (MR8) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_DarkRecolourScreenshotTest_darkBody_recolour_dark_body_dark.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android reading view's dark recolouring (MR8) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_DarkRecolourScreenshotTest_darkBody_recolour_dark_body_light.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android reading view's dark recolouring (MR8) |
| `apps/mailcraft-android/app/src/test/snapshots/images/work.mailcraft.android_DarkRecolourScreenshotTest_ownDarkDesign_recolour_own_dark_design_dark.png` | MailCraft contributors | Drawn by Paparazzi (`cargo xtask mobile`, `recordPaparazziDebug`) from the app's own screens with invented example.org mail | MIT OR Apache-2.0 | Golden screenshot of the Android reading view's dark recolouring (MR8) |
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private fun SingleBlock(block: BlockView, env: BodyEnv, paper: Paper, width: Dp)
when (val kind = block.kind) {
is BlockKindView.Table -> TableBlock(kind.table, block.align, env, paper, room)
is BlockKindView.Image -> PictureBlock(block, kind, env, paper, room)
is BlockKindView.Rule -> RuleBlock(block, env)
is BlockKindView.Rule -> RuleBlock(block, env, paper)
is BlockKindView.ListItem -> ListItem(block, kind, env, paper)
is BlockKindView.Heading -> TextBlock(block, env, paper, heading = kind.level.toInt())
is BlockKindView.Preformatted -> TextBlock(block, env, paper, mono = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ internal fun SidesView.hasAny() = top || right || bottom || left

private const val DASH = 6f
private const val DOT = 2f
private const val INVISIBLE_CONTRAST = 1.2
private const val MAX_LINE = 4
private const val TOP = 0
private const val RIGHT = 1
Expand All @@ -40,7 +39,7 @@ internal fun Modifier.frame(lines: List<LineView>, sides: SidesView?, fallback:

private fun DrawScope.drawSide(side: Int, line: LineView?, fallback: Color, paper: Paper) {
val thickness = (line?.width?.toInt() ?: 0).coerceIn(1, MAX_LINE).dp.toPx()
val seen = line?.color?.rgb()?.takeIf { Readability.contrast(it, paper.rgb) >= INVISIBLE_CONTRAST }
val seen = line?.color?.rgb()?.let(paper::line)
val effect = when (line?.style) {
LineStyleView.DASHED -> PathEffect.dashPathEffect(floatArrayOf(DASH * thickness, DASH * thickness))
LineStyleView.DOTTED -> PathEffect.dashPathEffect(floatArrayOf(DOT * thickness, DOT * thickness))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private const val PERCENT = 100
@Composable
internal fun TableBlock(table: TableView, align: AlignView, env: BodyEnv, paper: Paper, room: Dp) {
val width = tableWidth(table, room)
val own = table.background?.let { Paper.of(it.rgb(), paper.link) } ?: paper
val own = table.background?.let { paper.inner(it.rgb()) } ?: paper
val shape = RoundedCornerShape(table.radius.toInt().dp)
val outline = if (table.outline) Modifier.frame(table.lines, null, env.rule, own) else Modifier
val box = Modifier
Expand Down Expand Up @@ -140,7 +140,7 @@ private fun Modifier.alignIn(row: RowScope, v: VAlignView?): Modifier = with(row
/** One cell: its background, lines and padding, and its blocks in the room that is left. */
@Composable
internal fun CellBox(table: TableView, cell: CellView, env: BodyEnv, paper: Paper, width: Dp, modifier: Modifier) {
val own = cell.background?.let { Paper.of(it.rgb(), paper.link) } ?: paper
val own = cell.background?.let { paper.inner(it.rgb()) } ?: paper
val pad = cell.padding
// A cell of a data table that says nothing of its padding has the grid's own.
val roomy = !table.layout && pad == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,36 @@ import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.sp
import uniffi.mailcraft_mobile.SpanView

/** The paper a piece of a message is drawn on and the ink that reads on it, as `0xRRGGBB`. */
internal data class Paper(val rgb: Int, val ink: Int, val link: Int) {
/**
* The paper a piece of a message is drawn on and the ink that reads on it, as `0xRRGGBB`. [turn] is the
* reader's dark paper when the message's own colours are recoloured for a dark reading (#1966), else null.
*/
internal data class Paper(val rgb: Int, val ink: Int, val link: Int, val turn: Int? = null) {
/** A background the message asks for, as it is painted here. */
fun back(asked: Int): Int = turn?.let { Readability.darkBackground(asked, it) } ?: asked

/** A line the message asks for, as it is painted on this paper; null when it would not be seen. */
fun line(asked: Int): Int? = back(asked).takeIf { Readability.contrast(it, rgb) >= INVISIBLE_CONTRAST }

/** A horizontal rule the message colours: as written unless recoloured, then as [line]. */
fun rule(asked: Int): Int? = if (turn == null) asked else line(asked)

/** The paper of a box of the message's own inside this one. */
fun inner(asked: Int): Paper = of(back(asked), link).copy(turn = turn)

/** A text colour the message asks for, on [behind] (painted here), as it is drawn. */
fun inkOn(asked: Int, behind: Int): Int =
if (turn != null) Readability.darkInk(asked, behind) else Readability.adjust(asked, behind)

companion object {
/** A paper of [rgb] of the message's own, with the default ink for it. */
fun of(rgb: Int, link: Int): Paper = Paper(rgb, Readability.plainInk(rgb), Readability.adjust(link, rgb))
}
}

/** The least contrast at which a line the message colours is drawn in its own colour. */
private const val INVISIBLE_CONTRAST = 1.2

/** A link's place in a text. */
internal class LinkRange(val start: Int, val end: Int, val link: LinkInfo)

Expand Down Expand Up @@ -74,10 +96,10 @@ internal fun familyOf(font: String?, code: Boolean): FontFamily? {

/** The colour of a span's text on [paper]: its own, adjusted to read, else the paper's ink (or the link colour). */
private fun inkOf(span: SpanView, paper: Paper, linked: Boolean): Int {
val behind = span.background?.rgb() ?: paper.rgb
val behind = span.background?.rgb()?.let(paper::back) ?: paper.rgb
val own = span.color?.rgb()
return when {
own != null -> Readability.adjust(own, behind)
own != null -> paper.inkOn(own, behind)
linked -> Readability.adjust(paper.link, behind)
span.background != null -> Readability.plainInk(behind)
else -> paper.ink
Expand All @@ -98,7 +120,7 @@ internal fun sizeOf(span: SpanView): Int? = span.size?.toInt()?.coerceIn(1, MAX_
/** The style of one span on [paper]: what it asks for, with the colours adjusted to read. */
internal fun styleOf(span: SpanView, paper: Paper, linked: Boolean): SpanStyle = SpanStyle(
color = composeColor(inkOf(span, paper, linked)),
background = span.background?.let { composeColor(it.rgb()) } ?: Color.Unspecified,
background = span.background?.let { composeColor(paper.back(it.rgb())) } ?: Color.Unspecified,
fontWeight = if (span.bold) FontWeight.Bold else null,
fontStyle = if (span.italic) FontStyle.Italic else null,
textDecoration = decorationOf(span, linked),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ internal fun ListItem(block: BlockView, kind: BlockKindView.ListItem, env: BodyE

/** A horizontal rule, as thick and as coloured as the message says (a line of 1 to 4 pixels). */
@Composable
internal fun RuleBlock(block: BlockView, env: BodyEnv) {
internal fun RuleBlock(block: BlockView, env: BodyEnv, paper: Paper) {
val line = block.line
val thickness = line.width.toInt().coerceIn(1, MAX_RULE)
val color = line.color?.let { composeColor(it.rgb()) } ?: env.rule
val color = line.color?.rgb()?.let(paper::rule)?.let(::composeColor) ?: env.rule
Spacer(
modifier = Modifier
.padding(vertical = RULE_MARGIN.dp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import kotlin.math.pow
* The colours of a message, adjusted to stay readable on the paper they are drawn on. A message asks
* for colours as `0xRRGGBB`; a pair whose contrast falls short is changed (the text only, towards
* black or white, whichever reads better) and a pair that reads well is left as the sender wrote it.
*
* In a dark reading of a message written for a white page (the dark theme, its light page off, #1966)
* the message's colours are recoloured by the desktop's rule (`crates/ui-egui/src/reading/colors.rs`):
* a light background has its lightness turned over, keeping its hue, muted ([darkBackground]); a dark
* or strong one stays; and a text colour that then falls short has its lightness turned over too
* before it is moved towards black or white ([darkInk]). Pictures are never recoloured.
*/
internal object Readability {
/** The contrast ratio a text colour must have against its paper (WCAG's AA for body text). */
Expand Down Expand Up @@ -87,4 +93,79 @@ internal object Readability {
}
return (part(RED_SHIFT) shl RED_SHIFT) or (part(GREEN_SHIFT) shl GREEN_SHIFT) or part(0)
}

private const val RGB = 0xFFFFFF
private const val MAX_FLOOR = 0.4
private const val MUTE = 0.6

/**
* [rgb] as a background in a dark reading: one lighter than a half has its lightness turned
* over, white landing on the lightness of [surface] (the reader's dark paper) and a half
* staying a half, its hue kept and muted a little; one a half or darker (black, a navy footer,
* a saturated red) stays as the sender wrote it.
*/
fun darkBackground(rgb: Int, surface: Int): Int {
val (h, s, l) = hsl(rgb)
if (l <= MIDDLE) return rgb and RGB
val floor = hsl(surface).third.coerceIn(0.0, MAX_FLOOR)
return fromHsl(h, s * MUTE, floor + (1.0 - l) * (1.0 - TWO * floor))
}

/**
* [ink] on [paper] in a dark reading: itself when it reads, else its lightness turned over (a
* dark blue a light blue), else [adjust]ed.
*/
fun darkInk(ink: Int, paper: Int): Int {
val own = ink and RGB
if (contrast(own, paper) >= MIN_CONTRAST) return own
val (h, s, l) = hsl(own)
val turned = fromHsl(h, s, 1.0 - l)
return adjust(if (contrast(turned, paper) > contrast(own, paper)) turned else own, paper)
}
}

private const val HSL_BYTE = 255
private const val HSL_RED = 16
private const val HSL_GREEN = 8
private const val HSL_ROUND = 0.5
private const val HUE_SIXTHS = 6.0
private const val TWO = 2.0
private const val THREE = 3
private const val FOUR = 4
private const val TINY = 1e-6

/** Hue (0 to 6), saturation and lightness, as the desktop reckons them. */
internal fun hsl(rgb: Int): Triple<Double, Double, Double> {
val r = (rgb shr HSL_RED and HSL_BYTE) / HSL_BYTE.toDouble()
val g = (rgb shr HSL_GREEN and HSL_BYTE) / HSL_BYTE.toDouble()
val b = (rgb and HSL_BYTE) / HSL_BYTE.toDouble()
val max = maxOf(r, g, b)
val min = minOf(r, g, b)
val l = (max + min) / TWO
val d = max - min
if (d < TINY) return Triple(0.0, 0.0, l)
val s = d / (1.0 - kotlin.math.abs(TWO * l - 1.0)).coerceAtLeast(TINY)
val h = when (max) {
r -> ((g - b) / d).mod(HUE_SIXTHS)
g -> (b - r) / d + TWO
else -> (r - g) / d + FOUR
}
return Triple(h, s.coerceIn(0.0, 1.0), l)
}

/** The colour of hue [h] (0 to 6), saturation [s] and lightness [l]. */
internal fun fromHsl(h: Double, s: Double, l: Double): Int {
val c = (1.0 - kotlin.math.abs(TWO * l - 1.0)) * s
val x = c * (1.0 - kotlin.math.abs(h.mod(TWO) - 1.0))
val (r, g, b) = when (h.mod(HUE_SIXTHS).toInt()) {
0 -> Triple(c, x, 0.0)
1 -> Triple(x, c, 0.0)
2 -> Triple(0.0, c, x)
THREE -> Triple(0.0, x, c)
FOUR -> Triple(x, 0.0, c)
else -> Triple(c, 0.0, x)
}
val m = l - c / TWO
fun byte(v: Double): Int = ((v + m) * HSL_BYTE + HSL_ROUND).toInt().coerceIn(0, HSL_BYTE)
return (byte(r) shl HSL_RED) or (byte(g) shl HSL_GREEN) or byte(b)
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ internal fun MessagePage(
)
}
val page = if (light) colors.surfaceContainerLowest else colors.surface
// A dark page recolours a message written for a white one (#1966); one with its own dark design keeps its colours.
val turn = (page.toArgb() and RGB_MASK).takeIf { appIsDark() && !light && !opened.view.darkDesign }
val paper = Paper.of(page.toArgb() and RGB_MASK, colors.primary.toArgb() and RGB_MASK)
.copy(ink = colors.onSurface.toArgb() and RGB_MASK)
.copy(ink = colors.onSurface.toArgb() and RGB_MASK, turn = turn)
Column(modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.surface)) {
val title = opened.view.subject.ifBlank { shortName(opened.view.from) }
ReadingBar(texts, tools, title, onMenu)
Expand Down
Loading
Loading