forked from Blendman974/faceclaw
Better rendering of color emojis
This commit is contained in:
1 parent
c6fa3647ae
commit
d1f9f76abb
8 files changed
+336
-32
No files matched your search
@@ -13,7 +13,7 @@ local `@faceclaw/kotlin` plugin. Generated binaries and Gradle caches are ignore
|
||||
primitives, sha256/clock/log/file expects), `g2protocol/FirmwareImage`,
|
||||
`audio/` (Lc3PacketFramer, AudioSegmentation, VoicePrint, WavPcmReader),
|
||||
`graphics/` (GrayPacket, PreviewPalette, StatusIconArt, NotificationIconArt,
|
||||
GlyphPacket, OpenTypeNames, SvgIconSource), `net/` (HttpIdentity, SseStream,
|
||||
ColorGlyphArt, GlyphPacket, OpenTypeNames, SvgIconSource), `net/` (HttpIdentity, SseStream,
|
||||
ResumableDownload, RemoteInputSession), `evenhub/EvenHubAssetServer`,
|
||||
`alarms/` (AlarmSchedule, AlarmRingingPolicy), `settings/SettingsChangeHub`,
|
||||
the stock-firmware flows over a `StockLink` GATT port (`g2protocol/StockLinkSession`,
|
||||
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.jvm.JvmStatic
|
||||
import kotlin.math.max
|
||||
import kotlin.math.pow
|
||||
import kotlin.math.roundToInt
|
||||
|
||||
/**
|
||||
* Turns a color glyph (an emoji from a color font the text stack fell back to) into the
|
||||
* lit-on-black coverage the glyph pipeline carries, so it keeps its internal detail instead of
|
||||
* showing as its alpha silhouette (a face as a plain disc, ✅ as a plain square). Platform
|
||||
* rasterizers draw the glyph in white and pass unpremultiplied ARGB; this object owns the
|
||||
* pixel math so Android and iOS agree.
|
||||
*
|
||||
* Shading is by OKLab lightness relative to the glyph's body, its median lightness:
|
||||
* - the body lights fully, so a one-color emoji (❤️, ⭐, 🖤) reads as its shape at full
|
||||
* brightness whatever its color, the way the alpha did, rather than dim for being dark;
|
||||
* - darker detail (eyes, mouths, the digits on 📅) falls toward [DARKEST_LIT], never off, so
|
||||
* the silhouette stays whole;
|
||||
* - when a fair share of the glyph is clearly lighter than the body (the check on ✅, the
|
||||
* circle on 🎱, the eyes on 💩) the body dims toward [DIMMEST_BODY] to leave that detail
|
||||
* room above it. Small highlights (❤️'s glint) don't count.
|
||||
* Lit values go through the notification icons' tone curve ([NotificationIconArt.GAMMA]),
|
||||
* then scale by the pixel's alpha, which stays the glyph's coverage.
|
||||
*/
|
||||
object ColorGlyphArt {
|
||||
/** A pixel this opaque that is not white means the glyph was not drawn in the paint's white. */
|
||||
private const val MIN_COLOR_ALPHA = 32
|
||||
|
||||
/** How far below 255 a channel may sit and still count as white (unpremultiply rounding). */
|
||||
private const val WHITE_TOLERANCE = 24
|
||||
|
||||
/** Lit level of the glyph's darkest detail, before the tone curve. */
|
||||
private const val DARKEST_LIT = 0.3
|
||||
|
||||
/** Lit level the body dims to when the glyph has plenty of detail lighter than it. */
|
||||
private const val DIMMEST_BODY = 0.6
|
||||
|
||||
/** Lightness quantile that must be clearly above the body for the body to dim: about the lightest 10%. */
|
||||
private const val LIGHT_DETAIL_QUANTILE = 0.9
|
||||
|
||||
/** OKLab lightness above the body where light detail starts to dim the body, and the span to fully dimmed. */
|
||||
private const val LIGHT_DETAIL_START = 0.05
|
||||
private const val LIGHT_DETAIL_SPAN = 0.2
|
||||
|
||||
/**
|
||||
* The smallest lightness spans the ramps above and below the body stretch over, so faint
|
||||
* shading on a nearly one-color glyph stays faint instead of stretching to the full range.
|
||||
*/
|
||||
private const val MIN_LIGHT_SPAN = 0.15
|
||||
private const val MIN_DARK_SPAN = 0.3
|
||||
|
||||
/**
|
||||
* Whether a glyph drawn in white came out in other colors, i.e. came from a color font.
|
||||
* Ordinary glyphs are white wherever they have coverage.
|
||||
*/
|
||||
@JvmStatic
|
||||
fun isColor(argb: IntArray): Boolean {
|
||||
val threshold = 255 - WHITE_TOLERANCE
|
||||
for (p in argb) {
|
||||
if (alpha(p) < MIN_COLOR_ALPHA) continue
|
||||
if (((p shr 16) and 0xff) < threshold || ((p shr 8) and 0xff) < threshold || (p and 0xff) < threshold) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Coverage (one byte per pixel) of a glyph drawn in white as unpremultiplied ARGB: its alpha,
|
||||
* or for a color glyph its [shade].
|
||||
*/
|
||||
@JvmStatic
|
||||
fun coverage(argb: IntArray): ByteArray =
|
||||
if (isColor(argb)) shade(argb) else ByteArray(argb.size) { alpha(argb[it]).toByte() }
|
||||
|
||||
/** Lit-on-black shade (one byte per pixel) of a color glyph as unpremultiplied ARGB. */
|
||||
@JvmStatic
|
||||
fun shade(argb: IntArray): ByteArray {
|
||||
var peak = 0
|
||||
for (p in argb) peak = max(peak, alpha(p))
|
||||
val out = ByteArray(argb.size)
|
||||
if (peak == 0) return out
|
||||
|
||||
// Statistics over the solid part, so antialiased edges don't skew the body.
|
||||
val cutoff = max(1, (peak + 1) / 2)
|
||||
var solid = 0
|
||||
for (p in argb) if (alpha(p) >= cutoff) solid += 1
|
||||
val sorted = DoubleArray(solid)
|
||||
var next = 0
|
||||
for (p in argb) if (alpha(p) >= cutoff) sorted[next++] = Oklab.lightness(p)
|
||||
sorted.sort()
|
||||
val body = quantile(sorted, 0.5)
|
||||
val darkest = quantile(sorted, 0.05)
|
||||
val lightest = quantile(sorted, 0.95)
|
||||
val lightDetail = quantile(sorted, LIGHT_DETAIL_QUANTILE) - body
|
||||
val dimming = ((lightDetail - LIGHT_DETAIL_START) / LIGHT_DETAIL_SPAN).coerceIn(0.0, 1.0)
|
||||
val bodyLit = 1.0 - (1.0 - DIMMEST_BODY) * dimming
|
||||
val lightSpan = max(lightest - body, MIN_LIGHT_SPAN)
|
||||
val darkSpan = max(body - darkest, MIN_DARK_SPAN)
|
||||
|
||||
for (i in argb.indices) {
|
||||
val p = argb[i]
|
||||
val a = alpha(p)
|
||||
if (a == 0) continue
|
||||
val l = Oklab.lightness(p)
|
||||
val lit = if (l >= body) {
|
||||
bodyLit + (1.0 - bodyLit) * ((l - body) / lightSpan).coerceAtMost(1.0)
|
||||
} else {
|
||||
bodyLit - (bodyLit - DARKEST_LIT) * ((body - l) / darkSpan).coerceAtMost(1.0)
|
||||
}
|
||||
out[i] = (a * lit.pow(NotificationIconArt.GAMMA)).roundToInt().coerceIn(0, 255).toByte()
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
private fun quantile(sorted: DoubleArray, q: Double): Double = sorted[((sorted.size - 1) * q).roundToInt()]
|
||||
|
||||
private fun alpha(p: Int): Int = (p ushr 24) and 0xff
|
||||
}
|
||||
+1
-20
@@ -2,7 +2,6 @@ package com.faceclaw.app
|
||||
|
||||
import kotlin.jvm.JvmStatic
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.cbrt
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
import kotlin.math.pow
|
||||
@@ -72,12 +71,6 @@ object NotificationIconArt {
|
||||
|
||||
class Art(val kind: Kind, val gray: ByteArray)
|
||||
|
||||
/** sRGB byte value to linear light. */
|
||||
private val SRGB_TO_LINEAR = DoubleArray(256) { value ->
|
||||
val c = value / 255.0
|
||||
if (c <= 0.04045) c / 12.92 else ((c + 0.055) / 1.055).pow(2.4)
|
||||
}
|
||||
|
||||
/**
|
||||
* Classify and convert an icon rendered at (outputSize * ANALYSIS_SCALE) square, row-major
|
||||
* ARGB (not premultiplied).
|
||||
@@ -232,7 +225,7 @@ object NotificationIconArt {
|
||||
private fun readTile(argb: IntArray, mask: BooleanArray, size: Int, maxAlpha: Int, outputSize: Int): Art {
|
||||
val lab = DoubleArray(argb.size * 3)
|
||||
for (i in argb.indices) {
|
||||
if (mask[i]) toOklab(argb[i], lab, i * 3)
|
||||
if (mask[i]) Oklab.toOklab(argb[i], lab, i * 3)
|
||||
}
|
||||
|
||||
// The background is the color of the tile's rim, about one output pixel deep.
|
||||
@@ -347,18 +340,6 @@ object NotificationIconArt {
|
||||
return gray
|
||||
}
|
||||
|
||||
private fun toOklab(pixel: Int, out: DoubleArray, offset: Int) {
|
||||
val r = SRGB_TO_LINEAR[(pixel shr 16) and 0xff]
|
||||
val g = SRGB_TO_LINEAR[(pixel shr 8) and 0xff]
|
||||
val b = SRGB_TO_LINEAR[pixel and 0xff]
|
||||
val l = cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)
|
||||
val m = cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)
|
||||
val s = cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)
|
||||
out[offset] = 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s
|
||||
out[offset + 1] = 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s
|
||||
out[offset + 2] = 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s
|
||||
}
|
||||
|
||||
private fun distance(lab: DoubleArray, index: Int, background: DoubleArray): Double {
|
||||
val dl = lab[index * 3] - background[0]
|
||||
val da = lab[index * 3 + 1] - background[1]
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.faceclaw.app
|
||||
|
||||
import kotlin.math.cbrt
|
||||
import kotlin.math.pow
|
||||
|
||||
/**
|
||||
* OKLab, a perceptual color space: L is lightness (0 = black, 1 = white) and equal distances
|
||||
* look about equally different. The color analysis of icons and color glyphs works in it.
|
||||
*/
|
||||
internal object Oklab {
|
||||
/** sRGB byte value to linear light. */
|
||||
private val SRGB_TO_LINEAR = DoubleArray(256) { value ->
|
||||
val c = value / 255.0
|
||||
if (c <= 0.04045) c / 12.92 else ((c + 0.055) / 1.055).pow(2.4)
|
||||
}
|
||||
|
||||
/** Writes the (L, a, b) of an ARGB pixel's color (its alpha ignored) at out[offset]. */
|
||||
fun toOklab(pixel: Int, out: DoubleArray, offset: Int) {
|
||||
val r = SRGB_TO_LINEAR[(pixel shr 16) and 0xff]
|
||||
val g = SRGB_TO_LINEAR[(pixel shr 8) and 0xff]
|
||||
val b = SRGB_TO_LINEAR[pixel and 0xff]
|
||||
val l = cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)
|
||||
val m = cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)
|
||||
val s = cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)
|
||||
out[offset] = 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s
|
||||
out[offset + 1] = 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s
|
||||
out[offset + 2] = 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s
|
||||
}
|
||||
|
||||
/** The OKLab lightness of an ARGB pixel's color (its alpha ignored). */
|
||||
fun lightness(pixel: Int): Double {
|
||||
val r = SRGB_TO_LINEAR[(pixel shr 16) and 0xff]
|
||||
val g = SRGB_TO_LINEAR[(pixel shr 8) and 0xff]
|
||||
val b = SRGB_TO_LINEAR[pixel and 0xff]
|
||||
val l = cbrt(0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b)
|
||||
val m = cbrt(0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b)
|
||||
val s = cbrt(0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b)
|
||||
return 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
|
||||
|
||||
package com.faceclaw.app
|
||||
|
||||
import platform.Foundation.NSData
|
||||
|
||||
/** ColorGlyphArt for FaceclawFontRenderer.m, which draws color glyphs with CoreGraphics. */
|
||||
object IosColorGlyph {
|
||||
/**
|
||||
* The shade (one byte per pixel) of a color glyph drawn as premultiplied RGBA bytes, as a
|
||||
* kCGImageAlphaPremultipliedLast context lays them out. Empty data for a malformed buffer
|
||||
* rather than an exception, which would terminate the app on its way into Objective-C.
|
||||
*/
|
||||
fun shade(rgba: NSData): NSData {
|
||||
val bytes = rgba.byteArray()
|
||||
if (bytes.isEmpty() || bytes.size % 4 != 0) return NSData()
|
||||
val argb = IntArray(bytes.size / 4) { i ->
|
||||
val a = bytes[i * 4 + 3].toInt() and 0xff
|
||||
fun channel(offset: Int): Int {
|
||||
if (a == 0) return 0
|
||||
val premultiplied = bytes[i * 4 + offset].toInt() and 0xff
|
||||
return ((premultiplied * 255 + a / 2) / a).coerceAtMost(255)
|
||||
}
|
||||
(a shl 24) or (channel(0) shl 16) or (channel(1) shl 8) or channel(2)
|
||||
}
|
||||
return ColorGlyphArt.shade(argb).data()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user