//! 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("math"); 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] }; }