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
@@ -167,6 +167,7 @@ class FontFileRenderer private constructor() {
|
|||||||
* [width u16][height u16]
|
* [width u16][height u16]
|
||||||
* [width*height coverage bytes, gamma-mapped]
|
* [width*height coverage bytes, gamma-mapped]
|
||||||
* An ink-free glyph (space) has width = height = 0 but a valid advance.
|
* An ink-free glyph (space) has width = height = 0 but a valid advance.
|
||||||
|
* A color glyph (emoji) carries its ColorGlyphArt shade as its coverage.
|
||||||
* Empty array when the font cannot be loaded or the render fails.
|
* Empty array when the font cannot be loaded or the render fails.
|
||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
@@ -196,11 +197,16 @@ class FontFileRenderer private constructor() {
|
|||||||
}
|
}
|
||||||
val penX = pad - bounds.left
|
val penX = pad - bounds.left
|
||||||
val baselineY = pad - bounds.top
|
val baselineY = pad - bounds.top
|
||||||
val bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ALPHA_8)
|
// Drawn in color: an emoji the system fallback supplies from a color font
|
||||||
|
// would keep only its silhouette in an alpha-only bitmap. ColorGlyphArt shades
|
||||||
|
// color glyphs and passes white ones through as their alpha.
|
||||||
|
val bitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888)
|
||||||
val canvas = Canvas(bitmap)
|
val canvas = Canvas(bitmap)
|
||||||
canvas.drawText(text, penX.toFloat(), baselineY.toFloat(), paint)
|
canvas.drawText(text, penX.toFloat(), baselineY.toFloat(), paint)
|
||||||
val pixels = alpha8Pixels(bitmap, bw, bh)
|
val argb = IntArray(bw * bh)
|
||||||
|
bitmap.getPixels(argb, 0, bw, 0, 0, bw, bh)
|
||||||
bitmap.recycle()
|
bitmap.recycle()
|
||||||
|
val pixels = ColorGlyphArt.coverage(argb)
|
||||||
|
|
||||||
// Trim to the tight ink box; bearing/inkTop become pen- and
|
// Trim to the tight ink box; bearing/inkTop become pen- and
|
||||||
// line-relative inside the shared packer.
|
// line-relative inside the shared packer.
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#import "FaceclawFontRenderer.h"
|
#import "FaceclawFontRenderer.h"
|
||||||
#import <CoreText/CoreText.h>
|
#import <CoreText/CoreText.h>
|
||||||
#import <CoreGraphics/CoreGraphics.h>
|
#import <CoreGraphics/CoreGraphics.h>
|
||||||
|
#import <FaceclawKit/FaceclawKit.h>
|
||||||
#import <math.h>
|
#import <math.h>
|
||||||
|
|
||||||
// Immutable font descriptors may be shared; layout objects never cross threads.
|
// Immutable font descriptors may be shared; layout objects never cross threads.
|
||||||
@@ -42,8 +43,9 @@ static BOOL HasColorRun(CTLineRef line) {
|
|||||||
}
|
}
|
||||||
// iOS falls back to Apple Color Emoji for default-text symbols the face lacks
|
// iOS falls back to Apple Color Emoji for default-text symbols the face lacks
|
||||||
// (▶ U+25B6 in Roboto), where Android's minikin picks a text font; the
|
// (▶ U+25B6 in Roboto), where Android's minikin picks a text font; the
|
||||||
// alpha-only raster keeps just the emoji's silhouette (▶ becomes a solid
|
// emoji is a tile (▶ becomes a rounded square: a silhouette in the alpha-only
|
||||||
// rounded square). Request text presentation (U+FE0E) for each lone codepoint
|
// line rasters, a shaded tile in glyph cells), which reads worse than the
|
||||||
|
// text symbol. Request text presentation (U+FE0E) for each lone codepoint
|
||||||
// that fell back to a color font, wherever a text font can supply it. Returns
|
// that fell back to a color font, wherever a text font can supply it. Returns
|
||||||
// `text` itself when nothing changes.
|
// `text` itself when nothing changes.
|
||||||
static NSString *PreferTextPresentation(CTFontRef font, NSString *text, CTLineRef line, BOOL glyphLayout) {
|
static NSString *PreferTextPresentation(CTFontRef font, NSString *text, CTLineRef line, BOOL glyphLayout) {
|
||||||
@@ -78,18 +80,34 @@ static BOOL ValidText(NSString *text) { return text != nil && text.length <= 100
|
|||||||
static BOOL ValidRaster(NSInteger width, NSInteger height) {
|
static BOOL ValidRaster(NSInteger width, NSInteger height) {
|
||||||
return width > 0 && height > 0 && width <= 65535 && height <= 65535 && width * height <= 4000000;
|
return width > 0 && height > 0 && width <= 65535 && height <= 65535 && width * height <= 4000000;
|
||||||
}
|
}
|
||||||
|
static void ConfigureTextContext(CGContextRef ctx) {
|
||||||
|
CGContextSetShouldAntialias(ctx, true);
|
||||||
|
CGContextSetShouldSmoothFonts(ctx, false); // Grayscale coverage, never LCD subpixel color.
|
||||||
|
CGContextSetShouldSubpixelPositionFonts(ctx, true);
|
||||||
|
CGContextSetShouldSubpixelQuantizeFonts(ctx, false);
|
||||||
|
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
|
||||||
|
}
|
||||||
static CGContextRef CreateContext(NSMutableData *pixels, NSInteger width, NSInteger height) CF_RETURNS_RETAINED {
|
static CGContextRef CreateContext(NSMutableData *pixels, NSInteger width, NSInteger height) CF_RETURNS_RETAINED {
|
||||||
CGContextRef ctx = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
|
CGContextRef ctx = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width, NULL, (CGBitmapInfo)kCGImageAlphaOnly);
|
||||||
if (ctx) {
|
if (ctx) {
|
||||||
CGContextSetShouldAntialias(ctx, true);
|
ConfigureTextContext(ctx);
|
||||||
CGContextSetShouldSmoothFonts(ctx, false); // Grayscale coverage, never LCD subpixel color.
|
|
||||||
CGContextSetShouldSubpixelPositionFonts(ctx, true);
|
|
||||||
CGContextSetShouldSubpixelQuantizeFonts(ctx, false);
|
|
||||||
CGContextSetTextMatrix(ctx, CGAffineTransformIdentity);
|
|
||||||
CGContextSetGrayFillColor(ctx, 1, 1);
|
CGContextSetGrayFillColor(ctx, 1, 1);
|
||||||
}
|
}
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
// Premultiplied RGBA bytes, for a line with a color (emoji) run: an alpha-only raster keeps
|
||||||
|
// just the emoji's silhouette, so its colors go to ColorGlyphArt to be shaded instead.
|
||||||
|
static CGContextRef CreateColorContext(NSMutableData *pixels, NSInteger width, NSInteger height) CF_RETURNS_RETAINED {
|
||||||
|
CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
|
||||||
|
CGContextRef ctx = CGBitmapContextCreate(pixels.mutableBytes, width, height, 8, width * 4, space,
|
||||||
|
(CGBitmapInfo)kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
||||||
|
CGColorSpaceRelease(space);
|
||||||
|
if (ctx) {
|
||||||
|
ConfigureTextContext(ctx);
|
||||||
|
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
|
||||||
|
}
|
||||||
|
return ctx;
|
||||||
|
}
|
||||||
static void U16(uint8_t *out, NSInteger value) { out[0] = value & 255; out[1] = (value >> 8) & 255; }
|
static void U16(uint8_t *out, NSInteger value) { out[0] = value & 255; out[1] = (value >> 8) & 255; }
|
||||||
static void GammaCopy(uint8_t *dst, const uint8_t *src, NSInteger count, double gamma) {
|
static void GammaCopy(uint8_t *dst, const uint8_t *src, NSInteger count, double gamma) {
|
||||||
if (!isfinite(gamma) || gamma <= 0) gamma = 1;
|
if (!isfinite(gamma) || gamma <= 0) gamma = 1;
|
||||||
@@ -150,10 +168,13 @@ static NSData *ImagePacket(NSData *pixels, NSInteger width, NSInteger height, do
|
|||||||
NSInteger bottom = floor(CGRectGetMinY(bounds)) - 2;
|
NSInteger bottom = floor(CGRectGetMinY(bounds)) - 2;
|
||||||
NSInteger width = ceil(CGRectGetMaxX(bounds)) + 2 - left, height = top - bottom;
|
NSInteger width = ceil(CGRectGetMaxX(bounds)) + 2 - left, height = top - bottom;
|
||||||
if (!ValidRaster(width, height)) { CFRelease(line); CFRelease(font); return [NSData data]; }
|
if (!ValidRaster(width, height)) { CFRelease(line); CFRelease(font); return [NSData data]; }
|
||||||
NSMutableData *pixels = [NSMutableData dataWithLength:width * height];
|
BOOL color = HasColorRun(line);
|
||||||
CGContextRef ctx = CreateContext(pixels, width, height);
|
NSMutableData *raster = [NSMutableData dataWithLength:width * height * (color ? 4 : 1)];
|
||||||
|
CGContextRef ctx = color ? CreateColorContext(raster, width, height) : CreateContext(raster, width, height);
|
||||||
if (!ctx) { CFRelease(line); CFRelease(font); return [NSData data]; }
|
if (!ctx) { CFRelease(line); CFRelease(font); return [NSData data]; }
|
||||||
CGContextSetTextPosition(ctx, -left, -bottom); CTLineDraw(line, ctx); CGContextRelease(ctx);
|
CGContextSetTextPosition(ctx, -left, -bottom); CTLineDraw(line, ctx); CGContextRelease(ctx);
|
||||||
|
NSData *pixels = color ? [[FaceclawKitIosColorGlyph shared] shadeRgba:raster] : raster;
|
||||||
|
if ((NSInteger)pixels.length != width * height) { CFRelease(line); CFRelease(font); return [NSData data]; }
|
||||||
const uint8_t *src = pixels.bytes;
|
const uint8_t *src = pixels.bytes;
|
||||||
NSInteger x0 = width, y0 = height, x1 = -1, y1 = -1;
|
NSInteger x0 = width, y0 = height, x1 = -1, y1 = -1;
|
||||||
for (NSInteger y = 0; y < height; y++) for (NSInteger x = 0; x < width; x++) if (src[y * width + x]) {
|
for (NSInteger y = 0; y < height; y++) for (NSInteger x = 0; x < width; x++) if (src[y * width + x]) {
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ local `@faceclaw/kotlin` plugin. Generated binaries and Gradle caches are ignore
|
|||||||
primitives, sha256/clock/log/file expects), `g2protocol/FirmwareImage`,
|
primitives, sha256/clock/log/file expects), `g2protocol/FirmwareImage`,
|
||||||
`audio/` (Lc3PacketFramer, AudioSegmentation, VoicePrint, WavPcmReader),
|
`audio/` (Lc3PacketFramer, AudioSegmentation, VoicePrint, WavPcmReader),
|
||||||
`graphics/` (GrayPacket, PreviewPalette, StatusIconArt, NotificationIconArt,
|
`graphics/` (GrayPacket, PreviewPalette, StatusIconArt, NotificationIconArt,
|
||||||
GlyphPacket, OpenTypeNames, SvgIconSource), `net/` (HttpIdentity, SseStream,
|
ColorGlyphArt, GlyphPacket, OpenTypeNames, SvgIconSource), `net/` (HttpIdentity, SseStream,
|
||||||
ResumableDownload, RemoteInputSession), `evenhub/EvenHubAssetServer`,
|
ResumableDownload, RemoteInputSession), `evenhub/EvenHubAssetServer`,
|
||||||
`alarms/` (AlarmSchedule, AlarmRingingPolicy), `settings/SettingsChangeHub`,
|
`alarms/` (AlarmSchedule, AlarmRingingPolicy), `settings/SettingsChangeHub`,
|
||||||
the stock-firmware flows over a `StockLink` GATT port (`g2protocol/StockLinkSession`,
|
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.jvm.JvmStatic
|
||||||
import kotlin.math.abs
|
import kotlin.math.abs
|
||||||
import kotlin.math.cbrt
|
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
@@ -72,12 +71,6 @@ object NotificationIconArt {
|
|||||||
|
|
||||||
class Art(val kind: Kind, val gray: ByteArray)
|
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
|
* Classify and convert an icon rendered at (outputSize * ANALYSIS_SCALE) square, row-major
|
||||||
* ARGB (not premultiplied).
|
* ARGB (not premultiplied).
|
||||||
@@ -232,7 +225,7 @@ object NotificationIconArt {
|
|||||||
private fun readTile(argb: IntArray, mask: BooleanArray, size: Int, maxAlpha: Int, outputSize: Int): Art {
|
private fun readTile(argb: IntArray, mask: BooleanArray, size: Int, maxAlpha: Int, outputSize: Int): Art {
|
||||||
val lab = DoubleArray(argb.size * 3)
|
val lab = DoubleArray(argb.size * 3)
|
||||||
for (i in argb.indices) {
|
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.
|
// The background is the color of the tile's rim, about one output pixel deep.
|
||||||
@@ -347,18 +340,6 @@ object NotificationIconArt {
|
|||||||
return gray
|
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 {
|
private fun distance(lab: DoubleArray, index: Int, background: DoubleArray): Double {
|
||||||
val dl = lab[index * 3] - background[0]
|
val dl = lab[index * 3] - background[0]
|
||||||
val da = lab[index * 3 + 1] - background[1]
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package com.faceclaw.app
|
||||||
|
|
||||||
|
import kotlin.math.pow
|
||||||
|
import kotlin.math.roundToInt
|
||||||
|
import kotlin.test.*
|
||||||
|
|
||||||
|
class ColorGlyphArtTest {
|
||||||
|
private val size = 16
|
||||||
|
|
||||||
|
private val clear = 0
|
||||||
|
private val white = argb(255, 255, 255, 255)
|
||||||
|
private val yellow = argb(255, 255, 204, 50)
|
||||||
|
private val brown = argb(255, 90, 50, 20)
|
||||||
|
private val darkGray = argb(255, 60, 60, 60)
|
||||||
|
private val green = argb(255, 120, 170, 60)
|
||||||
|
private val red = argb(255, 230, 60, 50)
|
||||||
|
private val lightRed = argb(255, 250, 140, 130)
|
||||||
|
|
||||||
|
private fun argb(a: Int, r: Int, g: Int, b: Int): Int = (a shl 24) or (r shl 16) or (g shl 8) or b
|
||||||
|
|
||||||
|
private fun glyph(pixel: (x: Int, y: Int) -> Int): IntArray = IntArray(size * size) { pixel(it % size, it / size) }
|
||||||
|
|
||||||
|
private fun inDisc(x: Int, y: Int): Boolean {
|
||||||
|
val dx = x + 0.5 - size / 2.0
|
||||||
|
val dy = y + 0.5 - size / 2.0
|
||||||
|
return dx * dx + dy * dy <= 7.0 * 7.0
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun at(shade: ByteArray, x: Int, y: Int): Int = shade[y * size + x].toInt() and 0xff
|
||||||
|
|
||||||
|
private fun lit(level: Double): Int = (255 * level.pow(NotificationIconArt.GAMMA)).roundToInt()
|
||||||
|
|
||||||
|
@Test fun whiteGlyphIsItsAlpha() {
|
||||||
|
// An ordinary glyph drawn in white: antialiased edges, no color anywhere.
|
||||||
|
val pixels = glyph { x, y -> if (x in 4..11) argb(if (x == 4 || x == 11) 90 else 255, 255, 255, 255) else clear }
|
||||||
|
assertFalse(ColorGlyphArt.isColor(pixels))
|
||||||
|
assertContentEquals(ByteArray(pixels.size) { ((pixels[it] ushr 24) and 0xff).toByte() }, ColorGlyphArt.coverage(pixels))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun faintColorFringeIsNotColor() {
|
||||||
|
// Unpremultiplying a nearly transparent pixel can land anywhere; only real ink counts.
|
||||||
|
val pixels = glyph { x, _ -> if (x == 3) argb(8, 0, 0, 0) else if (x in 4..11) white else clear }
|
||||||
|
assertFalse(ColorGlyphArt.isColor(pixels))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun oneColorGlyphLightsFully() {
|
||||||
|
// 🖤: dark by lightness, but its shape is what matters.
|
||||||
|
val pixels = glyph { x, y -> if (inDisc(x, y)) darkGray else clear }
|
||||||
|
assertTrue(ColorGlyphArt.isColor(pixels))
|
||||||
|
val shade = ColorGlyphArt.coverage(pixels)
|
||||||
|
assertEquals(255, at(shade, 8, 8))
|
||||||
|
assertEquals(0, at(shade, 0, 0))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun darkDetailOnALightBodyStaysVisibleButDim() {
|
||||||
|
// 😀: a yellow disc with dark eyes and mouth; the body stays full, the features dim.
|
||||||
|
val pixels = glyph { x, y ->
|
||||||
|
when {
|
||||||
|
!inDisc(x, y) -> clear
|
||||||
|
(y in 5..6 && (x == 5 || x == 10)) || (y == 11 && x in 5..10) -> brown
|
||||||
|
else -> yellow
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val shade = ColorGlyphArt.coverage(pixels)
|
||||||
|
assertEquals(255, at(shade, 8, 8))
|
||||||
|
assertEquals(lit(0.3), at(shade, 5, 5))
|
||||||
|
assertTrue(at(shade, 5, 5) > 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun lightDetailOnADarkerBodyDimsTheBody() {
|
||||||
|
// ✅: a green tile with a large white check; the check lights fully above a dimmer tile.
|
||||||
|
val pixels = glyph { x, y ->
|
||||||
|
when {
|
||||||
|
x !in 1..14 || y !in 1..14 -> clear
|
||||||
|
(x in 4..7 && y in 9..11) || (x in 7..12 && y in 4..8) -> white
|
||||||
|
else -> green
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val shade = ColorGlyphArt.coverage(pixels)
|
||||||
|
assertEquals(255, at(shade, 9, 6))
|
||||||
|
assertEquals(lit(0.6), at(shade, 2, 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun smallHighlightLeavesTheBodyFull() {
|
||||||
|
// ❤️: a red body with a small glint; the glint is not detail worth dimming the body for.
|
||||||
|
val pixels = glyph { x, y ->
|
||||||
|
when {
|
||||||
|
!inDisc(x, y) -> clear
|
||||||
|
x in 4..5 && y in 4..5 -> lightRed
|
||||||
|
else -> red
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val shade = ColorGlyphArt.coverage(pixels)
|
||||||
|
assertEquals(255, at(shade, 8, 8))
|
||||||
|
assertEquals(255, at(shade, 4, 4))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun edgeAlphaScalesTheShade() {
|
||||||
|
val pixels = glyph { x, y -> if (x == 2) argb(128, 60, 60, 60) else if (x in 3..12 && y in 3..12) darkGray else clear }
|
||||||
|
val shade = ColorGlyphArt.coverage(pixels)
|
||||||
|
assertEquals(128, at(shade, 2, 8))
|
||||||
|
assertEquals(0, at(shade, 1, 8))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun emptyGlyphIsEmpty() {
|
||||||
|
assertTrue(ColorGlyphArt.shade(IntArray(size * size)).all { it.toInt() == 0 })
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user