summaryrefslogtreecommitdiff
path: root/src/vector/Paint.zig
blob: 0c5658836c3cd96e49105c0f7f50f8df246fac72 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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("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] };
}