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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
//! Duck-typed geometry. A point is anything with `x` and `y`, a rect anything
//! that also has `w` and `h` (or `width`/`height`), in any int or float type;
//! `@Vector`s and arrays of two or four elements work too. Functions return
//! the caller's own type where they build one, so sketches keep their data in
//! whatever shape suits them and never convert to library types.
const std = @import("std");
const abi = @import("abi");
/// Anything with an `x` and a `y`: fields, or a 2+ element vector or array.
pub fn isPoint(comptime T: type) bool {
return switch (@typeInfo(T)) {
.vector => |v| v.len >= 2 and isNumber(v.child),
.array => |a| a.len >= 2 and isNumber(a.child),
.@"struct" => @hasField(T, "x") and @hasField(T, "y"),
.pointer => |p| p.size == .one and isPoint(p.child),
else => false,
};
}
/// A point that also has a size: `w`/`h` or `width`/`height`, or 4 elements.
pub fn isRect(comptime T: type) bool {
return isPoint(T) and switch (@typeInfo(T)) {
.vector => |v| v.len >= 4,
.array => |a| a.len >= 4,
.@"struct" => (@hasField(T, "w") and @hasField(T, "h")) or (@hasField(T, "width") and @hasField(T, "height")),
.pointer => |p| isRect(p.child),
else => false,
};
}
/// `r`, `g`, `b` (optionally `a`) fields, or a `0xRRGGBB` integer.
pub fn isColor(comptime T: type) bool {
return switch (@typeInfo(T)) {
.int, .comptime_int => true,
.@"struct" => @hasField(T, "r") and @hasField(T, "g") and @hasField(T, "b"),
.pointer => |p| p.size == .one and isColor(p.child),
else => false,
};
}
fn isNumber(comptime T: type) bool {
return switch (@typeInfo(T)) {
.int, .float, .comptime_int, .comptime_float => true,
else => false,
};
}
pub const Point = struct { x: f32 = 0, y: f32 = 0 };
/// The type to build results in for inputs shaped like `T`: `T` itself, or
/// for anonymous literals (whose fields are comptime-only) an f32 `Point` or
/// `abi.Rect`.
pub fn Out(comptime T: type) type {
if (@typeInfo(T) != .@"struct") return T;
for (@typeInfo(T).@"struct".field_types) |F| switch (@typeInfo(F)) {
.comptime_int, .comptime_float => return if (isRect(T)) abi.Rect else Point,
else => {},
};
return T;
}
/// Component `name` of a point- or rect-like value, as f32.
pub fn get(value: anytype, comptime name: []const u8) f32 {
const T = @TypeOf(value);
return switch (@typeInfo(T)) {
.vector, .array => num(value[comptime index(name)]),
.@"struct" => if (@hasField(T, name))
num(@field(value, name))
else if (comptime std.mem.eql(u8, name, "w") and @hasField(T, "width"))
num(value.width)
else if (comptime std.mem.eql(u8, name, "h") and @hasField(T, "height"))
num(value.height)
else
@compileError(@typeName(T) ++ " has no field '" ++ name ++ "'"),
.pointer => get(value.*, name),
else => @compileError("expected a point or rect, found " ++ @typeName(T)),
};
}
/// A value of type `T` with the given components, converted to `T`'s fields.
pub fn make(comptime T: type, x: f32, y: f32, w: f32, h: f32) T {
switch (@typeInfo(T)) {
.vector => |v| return if (v.len == 2) .{ cast(v.child, x), cast(v.child, y) } else .{ cast(v.child, x), cast(v.child, y), cast(v.child, w), cast(v.child, h) },
.array => |a| return if (a.len == 2) .{ cast(a.child, x), cast(a.child, y) } else .{ cast(a.child, x), cast(a.child, y), cast(a.child, w), cast(a.child, h) },
.@"struct" => {
var result: T = undefined;
const s = @typeInfo(T).@"struct";
inline for (s.field_names, s.field_types, s.field_attrs) |name, F, attrs| {
@field(result, name) = if (comptime eql(name, "x"))
cast(F, x)
else if (comptime eql(name, "y"))
cast(F, y)
else if (comptime eql(name, "w") or eql(name, "width"))
cast(F, w)
else if (comptime eql(name, "h") or eql(name, "height"))
cast(F, h)
else if (comptime attrs.defaultValue(F)) |default|
default
else
@compileError(@typeName(T) ++ "." ++ name ++ " has no default");
}
return result;
},
else => @compileError("cannot build a point or rect of type " ++ @typeName(T)),
}
}
pub fn point(comptime T: type, x: f32, y: f32) T {
return make(T, x, y, 0, 0);
}
pub fn rect(value: anytype) abi.Rect {
comptime if (!isRect(@TypeOf(value))) @compileError("expected a rect (x, y, w, h), found " ++ @typeName(@TypeOf(value)));
return .{ .x = get(value, "x"), .y = get(value, "y"), .w = get(value, "w"), .h = get(value, "h") };
}
pub fn contains(r: anytype, p: anytype) bool {
const x = get(p, "x");
const y = get(p, "y");
const rx = get(r, "x");
const ry = get(r, "y");
return x >= rx and y >= ry and x < rx + get(r, "w") and y < ry + get(r, "h");
}
/// `r` shrunk by `d` on every side, in `r`'s own type.
pub fn inset(r: anytype, d: f32) Out(@TypeOf(r)) {
return make(Out(@TypeOf(r)), get(r, "x") + d, get(r, "y") + d, get(r, "w") - 2 * d, get(r, "h") - 2 * d);
}
/// `r` moved by the point `by`, in `r`'s own type.
pub fn offset(r: anytype, by: anytype) Out(@TypeOf(r)) {
const has_size = comptime isRect(@TypeOf(r));
return make(Out(@TypeOf(r)), get(r, "x") + get(by, "x"), get(r, "y") + get(by, "y"), if (has_size) get(r, "w") else 0, if (has_size) get(r, "h") else 0);
}
/// Anything with `r`, `g`, `b` (and optionally `a`) in 0..255 or 0..1 floats,
/// or a `0xRRGGBB` integer.
pub fn color(value: anytype) abi.Color {
const T = @TypeOf(value);
comptime if (!isColor(T)) @compileError("expected a color (r, g, b[, a]) or 0xRRGGBB, found " ++ @typeName(T));
if (T == abi.Color) return value;
return switch (@typeInfo(T)) {
.int, .comptime_int => abi.Color.hex(@intCast(value)),
.@"struct" => .{
.r = channel(value.r),
.g = channel(value.g),
.b = channel(value.b),
.a = if (@hasField(T, "a")) channel(value.a) else 255,
},
.pointer => color(value.*),
else => @compileError("expected a color, found " ++ @typeName(T)),
};
}
fn channel(v: anytype) u8 {
return switch (@typeInfo(@TypeOf(v))) {
.float, .comptime_float => @intFromFloat(std.math.clamp(v, 0, 1) * 255 + 0.5),
else => @intCast(std.math.clamp(v, 0, 255)),
};
}
fn index(comptime name: []const u8) usize {
if (eql(name, "x")) return 0;
if (eql(name, "y")) return 1;
if (eql(name, "w")) return 2;
if (eql(name, "h")) return 3;
@compileError("no component " ++ name);
}
fn num(v: anytype) f32 {
return switch (@typeInfo(@TypeOf(v))) {
.int, .comptime_int => @floatFromInt(v),
.float, .comptime_float => @floatCast(v),
else => @compileError("not a number: " ++ @typeName(@TypeOf(v))),
};
}
fn cast(comptime T: type, v: f32) T {
return switch (@typeInfo(T)) {
.int => @intFromFloat(@round(v)),
.float => @floatCast(v),
else => @compileError("not a number type: " ++ @typeName(T)),
};
}
fn eql(a: []const u8, b: []const u8) bool {
return std.mem.eql(u8, a, b);
}
test "shapes are recognized by their data" {
try std.testing.expect(isPoint(struct { x: u8, y: u8 }));
try std.testing.expect(!isRect(struct { x: u8, y: u8 }));
try std.testing.expect(!isRect(struct { w: u8, h: u8 }));
try std.testing.expect(isRect(struct { x: f32, y: f32, width: f32, height: f32 }));
try std.testing.expect(isRect(@Vector(4, i16)));
try std.testing.expect(!isPoint([2][]const u8));
try std.testing.expect(isColor(struct { r: f32, g: f32, b: f32 }));
}
test "any shape with the right fields works and keeps its type" {
const P = struct { x: i32, y: i32, label: u8 = 7 };
const R = struct { x: f64, y: f64, width: f64, height: f64 };
try std.testing.expect(contains(R{ .x = 0, .y = 0, .width = 10, .height = 5 }, P{ .x = 3, .y = 4 }));
try std.testing.expect(!contains(@Vector(4, f32){ 0, 0, 10, 5 }, [2]f32{ 3, 6 }));
const moved = offset(P{ .x = 1, .y = 2 }, @Vector(2, f32){ 2, 3 });
try std.testing.expectEqual(P{ .x = 3, .y = 5 }, moved);
try std.testing.expectEqual(abi.Rect{ .x = 1, .y = 1, .w = 8, .h = 3 }, rect(inset(R{ .x = 0, .y = 0, .width = 10, .height = 5 }, 1)));
try std.testing.expectEqual(abi.Color{ .r = 255, .g = 0, .b = 128 }, color(0xff0080));
try std.testing.expectEqual(abi.Color{ .r = 255, .g = 128, .b = 0, .a = 255 }, color(.{ .r = 1.0, .g = 0.5, .b = 0.0 }));
}
|