diff options
Diffstat (limited to 'src/colors.zig')
| -rw-r--r-- | src/colors.zig | 102 |
1 files changed, 88 insertions, 14 deletions
diff --git a/src/colors.zig b/src/colors.zig index 3d4bed8c..bb2c5b60 100644 --- a/src/colors.zig +++ b/src/colors.zig @@ -642,23 +642,56 @@ pub const ChromeTheme = struct { .scroll_thumb = th.scroll_thumb, }; } +}; + +/// The chrome as it is drawn: the theme's, fading from the last one over +/// `animation.transition_steps` frames, each colour through OKLab (G10: a +/// fade keeps its lightness even, never going muddy or dark between two +/// hues as a mix of sRGB bytes does). A change eases in and out (§8.1); one +/// set off partway through another eases out, moving at once, so it never +/// stalls (§7.2). A build without theme_animation (the board) snaps instead +/// (finishThemeInitialization). +pub const ChromeAnimation = struct { + from: ChromeTheme, + to: ChromeTheme, + displayed: ChromeTheme, + step: u16 = animation.transition_steps, + retargeted: bool = false, + + pub fn init(chrome: ChromeTheme) ChromeAnimation { + return .{ .from = chrome, .to = chrome, .displayed = chrome }; + } + + pub fn isActive(a: *const ChromeAnimation) bool { + return a.step < animation.transition_steps; + } - /// Partway from one chrome to another, each colour through OKLab - /// (G10): a fade keeps its lightness even, never going muddy or dark - /// between two hues as a mix of sRGB bytes does. - pub fn interpolate(from: ChromeTheme, to: ChromeTheme, step: u16, steps: u16) ChromeTheme { - var out: ChromeTheme = undefined; - const t = @as(f32, @floatFromInt(@min(step, steps))) / @as(f32, @floatFromInt(@max(1, steps))); + pub fn retarget(a: *ChromeAnimation, target: ChromeTheme) void { + a.retargeted = a.isActive(); + a.from = a.displayed; + a.to = target; + a.step = if (std.meta.eql(a.from, target)) animation.transition_steps else 0; + if (a.step == animation.transition_steps) a.displayed = target; + } + + pub fn advance(a: *ChromeAnimation) void { + if (!a.isActive()) return; + a.step += 1; + if (a.step == animation.transition_steps) { + a.displayed = a.to; + return; + } + const t = @as(f32, @floatFromInt(a.step)) / @as(f32, @floatFromInt(animation.transition_steps)); + // In thousandths of the way, as the fade has always stepped. + const k = @round(animation.sample(if (a.retargeted) .out_cubic else .smooth, t) * 1000); inline for (@typeInfo(ChromeTheme).@"struct".field_names) |name| - @field(out, name) = if (step == 0) @field(from, name) else if (step >= steps) @field(to, name) else mixOklab(@field(from, name), @field(to, name), t); - return out; + @field(a.displayed, name) = if (k == 0) @field(a.from, name) else if (k >= 1000) @field(a.to, name) else mixOklab(@field(a.from, name), @field(a.to, name), k / 1000); } -}; -pub const ChromeAnimation = if (theme_animation) - animation.Fade(ChromeTheme) -else - animation.Immediate(ChromeTheme); + pub fn snap(a: *ChromeAnimation, chrome: ChromeTheme) void { + a.* = init(chrome); + } +}; pub const initial_chrome = ChromeTheme.fromTheme(&themes[0]); @@ -805,7 +838,7 @@ pub fn loadThemeFile(p: *Pardes, generation: u32, bytes: []const u8, animate: bo pub fn finishThemeInitialization(p: *Pardes) void { p.chrome_animation.snap(ChromeTheme.fromTheme(p.theme())); - p.animate_theme_changes = true; + p.animate_theme_changes = theme_animation; } fn invalidateThemeDependentRasters(p: *Pardes) void { @@ -1011,6 +1044,47 @@ test "runtime theme changes animate chrome and retarget without a jump" { try std.testing.expectEqual(ChromeTheme.fromTheme(p.theme()), p.chromeTheme().*); } +/// A chrome of one colour everywhere, for the fade's tests. +fn chromeOf(rgb: [3]u8) ChromeTheme { + var out: ChromeTheme = undefined; + inline for (@typeInfo(ChromeTheme).@"struct".field_names) |name| @field(out, name) = rgb; + return out; +} + +test "a chrome fade starts where it is, lands exactly with every channel on its way, and goes no further" { + const from = chromeOf(.{ 240, 240, 240 }); + const to = chromeOf(.{ 5, 5, 5 }); + var fade: ChromeAnimation = .init(from); + fade.retarget(to); + try std.testing.expectEqual(from, fade.displayed); + var previous = fade.displayed.tag_bg; + for (0..animation.transition_steps) |_| { + fade.advance(); + for (fade.displayed.tag_bg, previous) |now, was| try std.testing.expect(now <= was); + previous = fade.displayed.tag_bg; + } + try std.testing.expect(!fade.isActive()); + try std.testing.expectEqual(to, fade.displayed); + fade.advance(); + try std.testing.expectEqual(to, fade.displayed); +} + +test "a chrome fade retargeted partway sets off from what is shown" { + const first = chromeOf(.{ 0, 40, 200 }); + const second = chromeOf(.{ 200, 140, 0 }); + const third = chromeOf(.{ 20, 10, 250 }); + var fade: ChromeAnimation = .init(first); + fade.retarget(second); + for (0..3) |_| fade.advance(); + const on_screen = fade.displayed; + fade.retarget(third); + try std.testing.expectEqual(on_screen, fade.from); + try std.testing.expectEqual(on_screen, fade.displayed); + try std.testing.expect(fade.isActive()); + for (0..animation.transition_steps) |_| fade.advance(); + try std.testing.expectEqual(third, fade.displayed); +} + test "render uses target content colors while chrome is in flight" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 40, .rows = 12 }); defer p.deinit(); |
