diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/animation.zig | 157 | ||||
| -rw-r--r-- | src/colors.zig | 102 |
2 files changed, 91 insertions, 168 deletions
diff --git a/src/animation.zig b/src/animation.zig index ce0b382a..1738ba7d 100644 --- a/src/animation.zig +++ b/src/animation.zig @@ -1,7 +1,8 @@ //! Animation curves and the panel transition records every shell reads: //! easing presets, transition kinds and their tracks, the fractional boxes -//! they move through, the character effects' per-cell sources, and the -//! generic displayed-value Transition the chrome colours use. +//! they move through, and the character effects' per-cell sources; the +//! springs that settle a view, a lift and the cursor (colors.zig fades the +//! chrome). const std = @import("std"); pub const ascii_max_movement_frames: u16 = 12; @@ -1324,155 +1325,3 @@ test "a blink holds while typing, eases at its edges and stops when idle" { try std.testing.expectEqual(@as(?u64, null), Blink.at(Blink.hold_ns + Blink.idle_ns + ms, 0).next_ns); try std.testing.expectEqual(@as(f32, 1), Blink.at(Blink.hold_ns + Blink.idle_ns + ms, 0).alpha); } - -/// A displayed value that fades from one target to the next over -/// `transition_steps` frames; the chrome colours are one. -pub fn Fade(comptime Value: type) type { - return struct { - const Self = @This(); - - from: Value, - to: Value, - displayed: Value, - step: u16 = transition_steps, - /// Set off from partway through another fade: it eases out (moving - /// at once) instead of in and out, so it never stalls (§7.2). - retargeted: bool = false, - - pub fn init(value: Value) Self { - return .{ .from = value, .to = value, .displayed = value }; - } - - pub fn isActive(a: *const Self) bool { - return a.step < transition_steps; - } - - pub fn retarget(a: *Self, target: Value) void { - a.retargeted = a.isActive(); - a.from = a.displayed; - a.to = target; - a.step = if (std.meta.eql(a.from, target)) transition_steps else 0; - if (a.step == transition_steps) a.displayed = target; - } - - /// A state change eases in and out (§8.1); a retargeted one out. - const resolution: u16 = 1000; - - pub fn advance(a: *Self) void { - if (!a.isActive()) return; - a.step += 1; - const t = @as(f32, @floatFromInt(a.step)) / @as(f32, @floatFromInt(transition_steps)); - const eased = sample(if (a.retargeted) .out_cubic else .smooth, t); - a.displayed = if (a.step == transition_steps) - a.to - else - Value.interpolate(a.from, a.to, @intFromFloat(@round(eased * resolution)), resolution); - } - - pub fn snap(a: *Self, value: Value) void { - a.* = init(value); - } - }; -} - -pub fn Immediate(comptime Value: type) type { - return struct { - const Self = @This(); - - displayed: Value, - - pub fn init(value: Value) Self { - return .{ .displayed = value }; - } - - pub fn isActive(_: *const Self) bool { - return false; - } - - pub fn retarget(a: *Self, target: Value) void { - a.displayed = target; - } - - pub fn advance(_: *Self) void {} - - pub fn snap(a: *Self, value: Value) void { - a.displayed = value; - } - }; -} - -/// A Fade's value for its tests: a colour stepped channel by channel. -const TestColor = struct { - rgb: [3]u8, - - pub fn interpolate(from: TestColor, to: TestColor, step: u16, steps: u16) TestColor { - var out = from; - for (&out.rgb, to.rgb) |*c, b| c.* = @intCast((@as(u32, c.*) * (steps - step) + @as(u32, b) * step + steps / 2) / steps); - return out; - } -}; - -test "Immediate lands where a completed Transition lands" { - const from: TestColor = .{ .rgb = .{ 240, 3, 90 } }; - const to: TestColor = .{ .rgb = .{ 5, 222, 90 } }; - - var faded = Fade(TestColor).init(from); - faded.retarget(to); - for (0..transition_steps) |_| faded.advance(); - - var instant = Immediate(TestColor).init(from); - try std.testing.expect(!instant.isActive()); - instant.retarget(to); - try std.testing.expectEqual(faded.displayed, instant.displayed); - - // Never active, so a frontend that renders only while something is animating stops immediately - // rather than spending ten frames discovering there is nothing to draw. - try std.testing.expect(!instant.isActive()); - instant.advance(); - try std.testing.expectEqual(to, instant.displayed); - - instant.snap(from); - try std.testing.expectEqual(from, instant.displayed); -} - -test "fixed-step interpolation has exact monotonic endpoints" { - const Tween = Fade(TestColor); - const from: TestColor = .{ .rgb = .{ 240, 3, 90 } }; - const to: TestColor = .{ .rgb = .{ 5, 222, 90 } }; - var tween = Tween.init(from); - tween.retarget(to); - try std.testing.expectEqual(from, tween.displayed); - - var previous = tween.displayed; - for (0..transition_steps) |_| { - tween.advance(); - try std.testing.expect(tween.displayed.rgb[0] <= previous.rgb[0]); - try std.testing.expect(tween.displayed.rgb[1] >= previous.rgb[1]); - try std.testing.expectEqual(@as(u8, 90), tween.displayed.rgb[2]); - previous = tween.displayed; - } - try std.testing.expect(!tween.isActive()); - try std.testing.expectEqual(to, tween.displayed); - tween.advance(); - try std.testing.expectEqual(to, tween.displayed); -} - -test "retarget starts at the currently displayed value" { - const Tween = Fade(TestColor); - const first: TestColor = .{ .rgb = .{ 0, 40, 200 } }; - const second: TestColor = .{ .rgb = .{ 200, 140, 0 } }; - const third: TestColor = .{ .rgb = .{ 20, 10, 250 } }; - var tween = Tween.init(first); - tween.retarget(second); - tween.advance(); - tween.advance(); - tween.advance(); - const on_screen = tween.displayed; - - tween.retarget(third); - try std.testing.expectEqual(on_screen, tween.from); - try std.testing.expectEqual(on_screen, tween.displayed); - try std.testing.expect(tween.isActive()); - for (0..transition_steps) |_| tween.advance(); - try std.testing.expectEqual(third, tween.displayed); -} 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(); |
