// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The draw list: what a frame is, as shapes a backend can draw however it //! likes. Coordinates are pixels of the target, from its top left. const std = @import("std"); pub const Point = @Vector(2, f32); /// Straight (not premultiplied) RGBA. pub const Color = [4]u8; pub const Shape = union(enum) { /// A filled disc. circle: struct { center: Point, radius: f32 }, /// A line with round ends: every point within `radius` of the segment /// from `a` to `b`. segment: struct { a: Point, b: Point, radius: f32 }, /// A filled triangle. triangle: struct { a: Point, b: Point, c: Point }, }; pub const Item = struct { shape: Shape, color: Color, }; pub const List = std.ArrayList(Item); /// `color` mixed into `background`: all of it at an `amount` of 1, none at /// 0. Fading a shape this way rather than by its alpha keeps it one color /// where the parts it is drawn in overlap, as an arrow's segments do at its /// corners. pub fn mix(color: Color, background: Color, amount: f32) Color { const t = std.math.clamp(amount, 0, 1); var out: Color = undefined; for (&out, color, background) |*o, c, b| { const cf: f32 = @floatFromInt(c); const bf: f32 = @floatFromInt(b); o.* = @intFromFloat(@round(bf + (cf - bf) * t)); } return out; } test mix { const black: Color = .{ 0, 0, 0, 255 }; const white: Color = .{ 255, 255, 255, 255 }; try std.testing.expectEqual(black, mix(black, white, 1)); try std.testing.expectEqual(white, mix(black, white, 0)); try std.testing.expectEqual(Color{ 128, 128, 128, 255 }, mix(black, white, 0.5)); try std.testing.expectEqual(white, mix(black, white, -3)); } /// A color written `#rrggbb` or `#rrggbbaa`, or null if it is not one. pub fn parseColor(text: []const u8) ?Color { if (text.len != 7 and text.len != 9) return null; if (text[0] != '#') return null; var out: Color = .{ 0, 0, 0, 0xff }; for (text[1..], 0..) |c, i| { const digit = std.fmt.charToDigit(c, 16) catch return null; if (i % 2 == 0) out[i / 2] = digit << 4 else out[i / 2] |= digit; } return out; } test parseColor { try std.testing.expectEqual(Color{ 0x35, 0x84, 0xe4, 0xff }, parseColor("#3584e4").?); try std.testing.expectEqual(Color{ 0x35, 0x84, 0xe4, 0x80 }, parseColor("#3584E480").?); try std.testing.expectEqual(null, parseColor("3584e4")); try std.testing.expectEqual(null, parseColor("#3584e")); try std.testing.expectEqual(null, parseColor("#3584eg")); try std.testing.expectEqual(null, parseColor("#+584e4")); } /// The colors a frame is drawn in. pub const Theme = struct { background: Color = .{ 0xff, 0xff, 0xff, 0xff }, dot: Color = .{ 0xbb, 0xbb, 0xbb, 0xff }, arrow: Color = .{ 0x00, 0x00, 0x00, 0xff }, /// An arrow on its way out, and the board celebrating a level won. accent: Color = default_accent, /// A blocked arrow, and a life being lost. block: Color = .{ 0xe0, 0x1b, 0x24, 0xff }, pub const default_accent: Color = .{ 0x35, 0x84, 0xe4, 0xff }; };