summaryrefslogtreecommitdiff
path: root/src/vector/Paint.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-09-25 21:47:58 -0300
committerGabriel Schneider <[email protected]>2026-09-25 21:48:16 -0300
commit9c33714591a2147f7e5879e77212ca9886d0c967 (patch)
treed250be9d9bc365d7894d49455c0f6376966b58be /src/vector/Paint.zig
downloadsketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.tar.gz
sketchbook-9c33714591a2147f7e5879e77212ca9886d0c967.zip
Put the game first: lib/sketchbook, sketches/vecanim
The root is the game. The platform library moves to lib/sketchbook (with serve_web.py, which serves any sketch's web build, as a tool of it), and vecanim to sketches/vecanim, still its own package on the same library.
Diffstat (limited to 'src/vector/Paint.zig')
-rw-r--r--src/vector/Paint.zig110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/vector/Paint.zig b/src/vector/Paint.zig
new file mode 100644
index 0000000..fb838d0
--- /dev/null
+++ b/src/vector/Paint.zig
@@ -0,0 +1,110 @@
+//! A paint resolved for one fill: a premultiplied color, or a gradient with
+//! the map from device pixels into its own space. The CPU backend samples it
+//! here; the GPU backend uploads the same numbers to its fragment shader,
+//! which mirrors `param` and `stopColor`.
+const Paint = @This();
+const std = @import("std");
+const geom = @import("geom.zig");
+const Drawing = @import("Drawing.zig");
+const Vec = geom.Vec;
+
+pub const Kind = enum(u32) { solid, linear, radial };
+pub const Spread = enum(u32) { pad, reflect, repeat };
+
+kind: Kind = .solid,
+/// Premultiplied, for solid paints.
+color: @Vector(4, f32) = .{ 0, 0, 0, 0 },
+/// Linear: start and end. Radial: center and focus.
+p0: Vec = .{ 0, 0 },
+p1: Vec = .{ 0, 0 },
+radius: f32 = 0,
+spread: Spread = .pad,
+/// Device pixels to gradient space.
+to_gradient: geom.Affine = .identity,
+stops: []const Drawing.Gradient.Stop = &.{},
+
+pub fn solid(color: Drawing.Color) Paint {
+ return .{ .color = premultiply(color) };
+}
+
+/// `paint` of `shape`, drawn with `m` from shape to device space.
+pub fn of(d: Drawing, shape: Drawing.Shape, paint: Drawing.Paint, m: geom.Affine) Paint {
+ return switch (paint) {
+ .none => .{},
+ .color => |c| solid(c),
+ .gradient => |index| blk: {
+ const g = d.gradients[index];
+ var space = m;
+ if (g.bounding_box) {
+ const b = d.shapeBounds(shape).rect();
+ space = space.mul(.translate(b.x, b.y)).mul(.scale(@max(b.w, 1e-6), @max(b.h, 1e-6)));
+ }
+ break :blk .{
+ .kind = if (g.kind == .linear) .linear else .radial,
+ .p0 = g.p0,
+ .p1 = g.p1,
+ .radius = g.radius,
+ .spread = switch (g.spread) {
+ .pad => .pad,
+ .reflect => .reflect,
+ .repeat => .repeat,
+ },
+ .to_gradient = space.mul(g.transform).invert(),
+ .stops = g.stops,
+ };
+ },
+ };
+}
+
+/// The gradient parameter at device point `q`, spread applied: 0..1.
+pub fn param(p: *const Paint, q: Vec) f32 {
+ const g = p.to_gradient.apply(q);
+ var t: f32 = switch (p.kind) {
+ .solid => 0,
+ .linear => blk: {
+ const dir = p.p1 - p.p0;
+ const len2 = geom.dot(dir, dir);
+ break :blk if (len2 > 0) geom.dot(g - p.p0, dir) / len2 else 0;
+ },
+ .radial => blk: {
+ // Where the ray from the focus through g meets the circle.
+ const dq = g - p.p1;
+ const cf = p.p1 - p.p0;
+ const a = geom.dot(dq, dq);
+ if (a <= 0) break :blk 0;
+ const b = geom.dot(dq, cf);
+ const c = geom.dot(cf, cf) - p.radius * p.radius;
+ const s = (-b + @sqrt(@max(b * b - a * c, 0))) / a;
+ break :blk if (s > 0) 1 / s else 1;
+ },
+ };
+ t = switch (p.spread) {
+ .pad => std.math.clamp(t, 0, 1),
+ .repeat => t - @floor(t),
+ .reflect => blk: {
+ const r = @mod(t, 2);
+ break :blk if (r > 1) 2 - r else r;
+ },
+ };
+ return t;
+}
+
+/// The premultiplied color at gradient parameter `t`.
+pub fn stopColor(p: *const Paint, t: f32) @Vector(4, f32) {
+ const stops = p.stops;
+ if (stops.len == 0) return .{ 0, 0, 0, 0 };
+ if (t <= stops[0].offset) return premultiply(stops[0].color);
+ for (stops[0 .. stops.len - 1], stops[1..]) |a, b| {
+ if (t <= b.offset) {
+ const span = b.offset - a.offset;
+ const u = if (span > 0) (t - a.offset) / span else 1;
+ // Interpolate straight colors, as SVG does, then premultiply.
+ return premultiply(a.color + (b.color - a.color) * @as(Drawing.Color, @splat(u)));
+ }
+ }
+ return premultiply(stops[stops.len - 1].color);
+}
+
+pub fn premultiply(c: Drawing.Color) @Vector(4, f32) {
+ return .{ c[0] * c[3], c[1] * c[3], c[2] * c[3], c[3] };
+}