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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//! Math related functionality, and aliases to [`nalgebra`] structs.
//!
//! Also includes the [`Color32`] and [`Color8`] structs.

/// An alias to [`nalgebra::Vector2<f32>`].
pub type Vec2 = nalgebra::Vector2<f32>;

/// An alias to [`nalgebra::Vector3<f32>`].
pub type Vec3 = nalgebra::Vector3<f32>;

/// An alias to [`nalgebra::Vector4<f32>`].
pub type Vec4 = nalgebra::Vector4<f32>;

/// An alias to [`nalgebra::Matrix4<f32>`].
pub type Mat4 = nalgebra::Matrix4<f32>;

/// An alias to [`nalgebra::Orthographic3<f32>`].
pub type Ortho = nalgebra::Orthographic3<f32>;

/// An RGBA color represented with four [`f32`]s.
///
/// [`Color32`] is definied as a tuple-styled struct, with public members.
///
/// # Examples
///
/// ```
/// use moon_engine::math::Color32;
/// let color = Color32::default();
/// ```
#[derive(Clone, Copy, Debug)]
pub struct Color32(pub f32, pub f32, pub f32, pub f32);

impl Default for Color32 {
    fn default() -> Self {
        Self(1.0, 1.0, 1.0, 1.0)
    }
}

impl Add for Color32 {
    type Output = Color32;

    fn add(self, rhs: Self) -> Self::Output {
        Color32(
            clamp(self.0 + rhs.0, 0.0, 1.0),
            clamp(self.1 + rhs.1, 0.0, 1.0),
            clamp(self.2 + rhs.2, 0.0, 1.0),
            clamp(self.3 + rhs.3, 0.0, 1.0),
        )
    }
}

impl Mul<f32> for Color32 {
    type Output = Color32;

    fn mul(self, rhs: f32) -> Self::Output {
        Color32(self.0 * rhs, self.1 * rhs, self.2 * rhs, self.3 * rhs)
    }
}

impl Color32 {
    /// Get the Red component of the [`Color32`].
    ///
    /// This is the same as the X component.
    pub const fn r(&self) -> f32 {
        self.0
    }
    /// Get the X component of the [`Color32`].
    ///
    /// This is the same as the Red component.
    pub const fn x(&self) -> f32 {
        self.0
    }
    /// Get the Green component of the [`Color32`].
    ///
    /// This is the same as the Y component.
    pub const fn g(&self) -> f32 {
        self.1
    }
    /// Get the Y component of the [`Color32`].
    ///
    /// This is the same as the Green component.
    pub const fn y(&self) -> f32 {
        self.1
    }
    /// Get the Blue component of the [`Color32`].
    ///
    /// This is the same as the Z component.
    pub const fn b(&self) -> f32 {
        self.2
    }
    /// Get the Z component of the [`Color32`].
    ///
    /// This is the same as the Blue component.
    pub const fn z(&self) -> f32 {
        self.2
    }
    /// Get the Alpha component of the [`Color32`].
    ///
    /// This is the same as the W component.
    pub const fn a(&self) -> f32 {
        self.3
    }
    /// Get the W component of the [`Color32`].
    ///
    /// This is the same as the Alpha component.
    pub const fn w(&self) -> f32 {
        self.3
    }

    /// Pure White Color.
    pub const WHITE: Color32 = Color32(1.0, 1.0, 1.0, 1.0);
    /// Pure Black Color.
    pub const BLACK: Color32 = Color32(0.0, 0.0, 0.0, 1.0);
    /// Magenta Color.
    pub const MAGENTA: Color32 = Color32(1.0, 0.0, 1.0, 1.0);
    /// All fields zeroed out.
    pub const ZEROES: Color32 = Color32(0.0, 0.0, 0.0, 0.0);
}

impl From<&[f32; 4]> for Color32 {
    fn from(slice: &[f32; 4]) -> Self {
        Self(slice[0], slice[1], slice[2], slice[3])
    }
}

impl From<Color32> for [f32; 4] {
    fn from(color: Color32) -> Self {
        [color.0, color.1, color.2, color.3]
    }
}

impl From<&[u8; 4]> for Color32 {
    fn from(slice: &[u8; 4]) -> Self {
        Self::from(Color8(slice[0], slice[1], slice[2], slice[3]))
    }
}

impl From<Color32> for [u8; 4] {
    fn from(color: Color32) -> Self {
        let color = Color8::from(color);
        [color.0, color.1, color.2, color.3]
    }
}

impl From<Color32> for Vec<u8> {
    fn from(color: Color32) -> Self {
        let color = Color8::from(color);
        vec![color.0, color.1, color.2, color.3]
    }
}

impl From<Color8> for Color32 {
    fn from(color: Color8) -> Self {
        Self(
            color.0 as f32 / 255.0,
            color.1 as f32 / 255.0,
            color.2 as f32 / 255.0,
            color.3 as f32 / 255.0,
        )
    }
}

/// An RGBA color represented with four [`u8`]s.
///
/// [`Color8`] is definied as a tuple-styled struct, with public members.
///
/// # Examples
///
/// ```
/// use moon_engine::math::Color8;
/// let color = Color8::default();
/// ```
#[derive(Clone, Copy)]
pub struct Color8(pub u8, pub u8, pub u8, pub u8);

impl Default for Color8 {
    fn default() -> Self {
        Self(255, 255, 255, 255)
    }
}

impl Color8 {
    /// Get the Red component of the [`Color8`].
    ///
    /// This is the same as the X component.
    pub const fn r(&self) -> u8 {
        self.0
    }
    /// Get the X component of the [`Color8`].
    ///
    /// This is the same as the Red component.
    pub const fn x(&self) -> u8 {
        self.0
    }
    /// Get the Green component of the [`Color8`].
    ///
    /// This is the same as the Y component.
    pub const fn g(&self) -> u8 {
        self.1
    }
    /// Get the Y component of the [`Color8`].
    ///
    /// This is the same as the Green component.
    pub const fn y(&self) -> u8 {
        self.1
    }
    /// Get the Blue component of the [`Color8`].
    ///
    /// This is the same as the Z component.
    pub const fn b(&self) -> u8 {
        self.2
    }
    /// Get the Z component of the [`Color8`].
    ///
    /// This is the same as the Blue component.
    pub const fn z(&self) -> u8 {
        self.2
    }
    /// Get the Alpha component of the [`Color8`].
    ///
    /// This is the same as the W component.
    pub const fn a(&self) -> u8 {
        self.3
    }
    /// Get the W component of the [`Color8`].
    ///
    /// This is the same as the Alpha component.
    pub const fn w(&self) -> u8 {
        self.3
    }
}

impl From<&[u8; 4]> for Color8 {
    fn from(slice: &[u8; 4]) -> Self {
        Self(slice[0], slice[1], slice[2], slice[3])
    }
}

impl From<Color8> for [u8; 4] {
    fn from(color: Color8) -> Self {
        [color.0, color.1, color.2, color.3]
    }
}

impl From<Color32> for Color8 {
    fn from(color: Color32) -> Self {
        Self(
            (color.0 * 255.0) as u8,
            (color.1 * 255.0) as u8,
            (color.2 * 255.0) as u8,
            (color.3 * 255.0) as u8,
        )
    }
}

/// A [`Point`] is an alias to Vec2.
pub type Point = Vec2;

use std::ops::{Add, Mul};

pub use nalgebra::clamp;

/// Trait for generating random values
pub trait Random {
    /// Get a random value.
    fn random() -> Self;
    /// Get a random value, expanded to another range.
    fn random_range_max(max: Self) -> Self;
    /// Get a random value, expanded to another range.
    fn random_range(min: Self, max: Self) -> Self;
}

impl Random for f32 {
    fn random() -> Self {
        js_sys::Math::random() as f32
    }

    fn random_range_max(max: Self) -> Self {
        f32::random() * max
    }

    fn random_range(min: Self, max: Self) -> Self {
        f32::random() * (max - min) + min
    }
}

impl Random for Vec2 {
    fn random() -> Self {
        Vec2::new(f32::random(), f32::random())
    }

    fn random_range_max(max: Self) -> Self {
        Vec2::new(f32::random_range_max(max.x), f32::random_range_max(max.y))
    }

    fn random_range(min: Self, max: Self) -> Self {
        Vec2::new(
            f32::random_range(min.x, max.x),
            f32::random_range(min.y, max.y),
        )
    }
}

impl Random for Color32 {
    fn random() -> Self {
        Color32(f32::random(), f32::random(), f32::random(), 1.0)
    }

    fn random_range_max(max: Self) -> Self {
        Color32(
            f32::random_range_max(max.0),
            f32::random_range_max(max.1),
            f32::random_range_max(max.2),
            f32::random_range_max(max.3),
        )
    }

    fn random_range(min: Self, max: Self) -> Self {
        Color32(
            f32::random_range(min.0, max.0),
            f32::random_range(min.1, max.1),
            f32::random_range(min.2, max.2),
            f32::random_range(min.3, max.3),
        )
    }
}

/// Linearly interpolate between two values.
pub trait Lerp: Sized + Mul<f32, Output = Self> + Add<Self, Output = Self> {
    /// Linearly interpolate between two values.
    fn lerp(min: Self, max: Self, factor: f32) -> Self {
        min * (1.0 - factor) + max * factor
    }
}

impl Lerp for f32 {}
impl Lerp for Vec2 {}
impl Lerp for Color32 {}