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
//! This module contains a struct, [`Coord`], that models a 3D coordinate space
//! and supports limited math in 3 dimensions with scalars and other
//! coordinates. Used to unify math with colors that is the same, just with
//! different projections into 3D space.
use num;
use ;
use ;
/// Represents a scalar value that can be easily converted, described using the common numeric traits
/// in [`num`]. Anything that falls under this category can be multiplied by a [`Coord`] to scale
/// it. This has no added functionality: it's just for convenience.
/// A point in 3D space. Supports many common arithmetic operations on points.
/// `Coord` has three axes, denoted `x`, `y`, and `z`. These are not any different in any method of
/// `Coord`, so the distinction between them is completely conventional. In Scarlet, any `Color`
/// that converts to and from a `Coord` will match its components with these axes in the order of the
/// letters in its name: for example, `CIELABColor` maps to a coordinate such that `l` is on the
/// x-axis, `a` is on the y-axis, and `b` is on the z-axis.
///
/// # Examples
/// ## Basic Operations
/// ```
/// # use scarlet::coord::Coord;
/// let point_1 = Coord{x: 1., y: 8., z: 7.};
/// let point_2 = Coord{x: 7., y: 2., z: 3.};
/// // Add two points together to do componentwise addition.
/// let sum = point_1 + point_2; // the point (8, 10, 10)
/// // Subtract two points the same way.
/// let diff = point_1 - point_2; // the point (-6, 6, 4)
/// // There is no multiplication of two points, because there are many different ways to conceptualize
/// // multiplying two points and Scarlet doesn't need it. Instead, it supports scalar multiplication
/// // and division. This has the unfortunate side effect of not allowing multiplication one way.
/// let prod = point_1 * 2u8; // the point (2, 16, 14)
/// // switching the above operands' order would cause an error!
/// let quot = point_1 / 2.; // the point (0.5, 4, 3.5)
/// ```
// Now we implement addition and subtraction, as well as division and multiplication by scalars. Note
// that because the multiplication of pnoints by points in 3D space has different defintions, we won't
// implement it: it's unclear what even the return type should be.
/// This is a perfect analogue to numbers: for any Coords c1, c2, and c3 with the same type, c1 + c2 =
/// c3 implies c3 - c2 = c1 and c3 - c1 = c2, down to floating point error if that exists.
// This implements basic scalar multiplication and division: (a, b, c) * s = (sa, sb, sc) and
// similarly for division. This is unfortunately not commutative, but it'll do.
// this will mostly be math stuff for colors