summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/colors.zig25
1 files changed, 18 insertions, 7 deletions
diff --git a/core/colors.zig b/core/colors.zig
index 6b578f09..b2069465 100644
--- a/core/colors.zig
+++ b/core/colors.zig
@@ -708,17 +708,23 @@ fn inksOn(ground: [3]u8, ink: [3]u8) Inks {
}
/// `ink` mixed toward `ground` (OKLab) by hundredths as far as it still
-/// reads at `floor` on it.
+/// reads at `floor` on it: found by halves (its contrast falls as it nears
+/// the ground), seven mixes where a walk took up to a hundred (the first
+/// frame's bodyChrome derives them too).
fn toward(ink: [3]u8, ground: [3]u8, floor: f32) [3]u8 {
const a = toOklab(ink);
const b = toOklab(ground);
var best = ink;
- var step: f32 = 1;
- while (step <= 100) : (step += 1) {
- const t = step / 100;
+ var lo: u32 = 0;
+ var hi: u32 = 101;
+ while (hi - lo > 1) {
+ const mid = (lo + hi) / 2;
+ const t = @as(f32, @floatFromInt(mid)) / 100;
const c = fromOklab(.{ a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t });
- if (contrast(c, ground) < floor) break;
- best = c;
+ if (contrast(c, ground) >= floor) {
+ lo = mid;
+ best = c;
+ } else hi = mid;
}
return best;
}
@@ -729,6 +735,7 @@ fn toward(ink: [3]u8, ground: [3]u8, floor: f32) [3]u8 {
fn pushAway(ink: [3]u8, grounds: []const [3]u8, floor: f32) [3]u8 {
const white: [3]u8 = @splat(255);
const black: [3]u8 = @splat(0);
+ if (leastContrast(ink, grounds) >= floor) return ink;
const end = if (leastContrast(white, grounds) >= leastContrast(black, grounds)) white else black;
const a = toOklab(ink);
const b = toOklab(end);
@@ -765,10 +772,14 @@ fn selectionOf(own: [3]u8, ground: [3]u8, text: [3]u8) [3]u8 {
var best = own;
var best_off: f32 = 0;
const away: [3]u8 = if (relativeLuminance(text) > relativeLuminance(own)) @splat(0) else @splat(255);
+ if (contrast(own, ground) >= Roles.selectionFloor(ground) and contrast(text, own) >= Roles.text_floor) return own;
+ const o = toOklab(own);
for ([2][3]u8{ text, away }) |end| {
+ const e = toOklab(end);
var step: f32 = 0;
while (step <= 100) : (step += 1) {
- const c = mixOklab(own, end, step / 100);
+ const t = step / 100;
+ const c = fromOklab(.{ o[0] + (e[0] - o[0]) * t, o[1] + (e[1] - o[1]) * t, o[2] + (e[2] - o[2]) * t });
if (contrast(text, c) < Roles.text_floor) break;
const off = contrast(c, ground) / Roles.selectionFloor(ground);
if (off >= 1) return c;