rusty_gfx/
color.rs

1use serde::{Deserialize, Serialize};
2use std::hash::{Hash, Hasher};
3
4/// A color with 32-bit float parts from `[0.0, 1.0]` suitable for OpenGL.
5#[derive(Copy, Clone, Debug, Deserialize, Serialize)]
6pub struct Color([f32; 3]);
7
8impl Color {
9    /// Red, Green, Blue! Values should be in the range `[0.0, 1.0]`
10    pub fn new(r: f32, g: f32, b: f32) -> Self {
11        Self([r, g, b])
12    }
13}
14
15/// So converting back and forth between `Color` and `[f32; 3]` is easy.
16impl From<Color> for [f32; 3] {
17    fn from(color: Color) -> Self {
18        color.0
19    }
20}
21
22impl Hash for Color {
23    fn hash<H: Hasher>(&self, state: &mut H) {
24        (self.0[0] as u32).hash(state);
25        (self.0[1] as u32).hash(state);
26        (self.0[2] as u32).hash(state);
27    }
28}
29
30impl PartialEq for Color {
31    fn eq(&self, other: &Self) -> bool {
32        (self.0[0] == other.0[0]) && (self.0[1] == other.0[1]) && (self.0[2] == other.0[2])
33    }
34}