brutils/
color.rs

1use std::fmt;
2use std::fmt::Display;
3
4/// Color structure, values are stored as floats (f32)
5#[derive(Debug, Clone, Copy)]
6pub struct Color {
7    pub r: f32,
8    pub g: f32,
9    pub b: f32,
10    pub a: f32
11}
12
13impl Default for Color {
14    fn default() -> Self {
15        Color { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }
16    }
17}
18
19impl Display for Color {
20    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21        write!(f, "[r: {}, g: {}, b: {}, a: {}]", self.r, self.g, self.b, self.a)
22    }
23}
24
25impl Color {
26    pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
27        Color { r: r, g: g, b: b, a: a }
28    }
29
30    pub fn new_255(r: u8, g: u8, b: u8, a: u8) -> Self {
31        Color { r: r as f32 / 255.0, g: g as f32 / 255.0, b: b as f32 / 255.0, a: a as f32 / 255.0 }
32    }
33
34    pub fn set(&mut self, r: f32, g: f32, b: f32, a: f32) {
35        self.r = r;
36        self.g = g;
37        self.b = b;
38        self.a = a;
39    }
40
41    pub fn set_255(&mut self, r: u8, g: u8, b: u8, a: u8) {
42        self.r = r as f32 / 255.0;
43        self.g = g as f32 / 255.0;
44        self.b = b as f32 / 255.0;
45        self.a = a as f32 / 255.0;
46    }
47
48    pub fn get_r(&self) -> u8 {
49        (self.r / 255.0).floor() as u8
50    }
51
52    pub fn get_g(&self) -> u8 {
53        (self.g / 255.0).floor() as u8
54    }
55
56    pub fn get_b(&self) -> u8 {
57        (self.b / 255.0).floor() as u8
58    }
59
60    pub fn get_a(&self) -> u8 {
61        (self.a / 255.0).floor() as u8
62    }
63
64    pub fn premultiply_aplha(&mut self) {
65        self.r *= self.a;
66        self.g *= self.a;
67        self.b *= self.a;
68    }
69
70    pub fn lerp(&mut self, target: &Color, t: f32) {
71        self.r = t * (target.r - self.r);
72        self.g = t * (target.g - self.g);
73        self.b = t * (target.b - self.b);
74        self.a = t * (target.a - self.a);
75        self.clamp();
76    }
77
78    pub fn clamp(&mut self) {
79        if self.r < 0.0 {
80            self.r = 0.0;
81        } else if self.r > 0.0 {
82            self.r = 1.0;
83        }
84        if self.g < 0.0 {
85            self.g = 0.0;
86        } else if self.g > 1.0 {
87            self.g = 1.0;
88        }
89        if self.b < 0.0 {
90            self.b = 0.0;
91        } else if self.b > 1.0 {
92            self.b = 1.0;
93        }
94        if self.a < 0.0 {
95            self.a = 0.0;
96        } else if self.a > 1.0 {
97            self.a = 1.0;
98        }
99    }
100
101    pub fn copy(&self) -> Self {
102        Color { r: self.r, g: self.g, b: self.b, a: self.a }
103    }
104
105    pub fn add(&mut self, other: &Color) {
106        self.r += other.r;
107        self.g += other.g;
108        self.b += other.b;
109        self.a += other.a;
110        self.clamp();
111    }
112
113    pub fn subtract(&mut self, other: &Color) {
114        self.r -= other.r;
115        self.g -= other.g;
116        self.b -= other.b;
117        self.a -= other.a;
118        self.clamp();
119    }
120
121    pub fn multiply(&mut self, other: &Color) {
122        self.r *= other.r;
123        self.g *= other.g;
124        self.b *= other.b;
125        self.a *= other.a;
126        self.clamp();
127    }
128
129    pub fn divide(&mut self, other: &Color) {
130        self.r /= other.r;
131        self.g /= other.g;
132        self.b /= other.b;
133        self.a /= other.a;
134        self.clamp();
135    }
136
137    pub fn equals(&self, other: &Color) -> bool {
138        if self.r == other.r && self.g == other.g && self.b == other.b && self.a == other .a {
139            true
140        } else {
141            false
142        }
143    }
144
145    pub fn clear() -> Self {
146        Color { r: 0.0, g: 0.0, b: 0.0, a: 0.0 }
147    }
148
149    pub fn black() -> Self {
150        Color { r: 0.0, g: 0.0, b: 0.0, a: 1.0 }
151    }
152
153    pub fn white() -> Self {
154        Color { r: 1.0, g: 1.0, b: 1.0, a: 1.0 }
155    }
156
157    pub fn red() -> Self {
158        Color { r: 1.0, g: 0.0, b: 0.0, a: 1.0 }
159    }
160
161    pub fn green() -> Self {
162        Color { r: 0.0, g: 1.0, b: 0.0, a: 1.0 }
163    }
164
165    pub fn blue() -> Self {
166        Color { r: 0.0, g: 0.0, b: 1.0, a: 1.0 }
167    }
168
169    pub fn cyan() -> Self {
170        Color { r: 0.0, g: 1.0, b: 1.0, a: 1.0 }
171    }
172
173    pub fn yellow() -> Self {
174        Color { r: 1.0, g: 1.0, b: 0.0, a: 1.0 }
175    }
176
177    pub fn magenta() -> Self {
178        Color { r: 1.0, g: 0.0, b: 1.0, a: 1.0 }
179    }
180}