Skip to content

Commit 7fdaa4a

Browse files
authoredOct 12, 2022
Implement the provided operators for Rgb manually (#13)
* Implement the provided operators for `Rgb` manually This removes the overload dependency by implementing the operators for the Rgb struct manually.
1 parent 67eb4e4 commit 7fdaa4a

File tree

2 files changed

+162
-41
lines changed

2 files changed

+162
-41
lines changed
 

‎Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ doctest = true
1919
derive_serde_style = ["serde"]
2020

2121
[dependencies]
22-
overload = "0.1.1"
2322
serde = { version="1.0.90", features=["derive"], optional=true }
2423

2524
[target.'cfg(target_os="windows")'.dependencies.winapi]

‎src/rgb.rs

+162-40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Code liberally borrowed from here
22
// https://github.com/navierr/coloriz
3-
use std::ops;
43
use std::u32;
54
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65
pub struct Rgb {
@@ -123,51 +122,174 @@ impl ANSIColorCode for Rgb {
123122
}
124123
}
125124

126-
overload::overload!(
127-
(lhs: ?Rgb) + (rhs: ?Rgb) -> Rgb {
128-
Rgb::new(
129-
lhs.r.saturating_add(rhs.r),
130-
lhs.g.saturating_add(rhs.g),
131-
lhs.b.saturating_add(rhs.b)
132-
)
125+
fn rgb_add(lhs: &Rgb, rhs: &Rgb) -> Rgb {
126+
Rgb::new(
127+
lhs.r.saturating_add(rhs.r),
128+
lhs.g.saturating_add(rhs.g),
129+
lhs.b.saturating_add(rhs.b),
130+
)
131+
}
132+
133+
fn rgb_sub(lhs: &Rgb, rhs: &Rgb) -> Rgb {
134+
Rgb::new(
135+
lhs.r.saturating_sub(rhs.r),
136+
lhs.g.saturating_sub(rhs.g),
137+
lhs.b.saturating_sub(rhs.b),
138+
)
139+
}
140+
141+
fn rgb_mul_f32(lhs: &Rgb, rhs: &f32) -> Rgb {
142+
Rgb::new(
143+
(lhs.r as f32 * rhs.clamp(0.0, 1.0)) as u8,
144+
(lhs.g as f32 * rhs.clamp(0.0, 1.0)) as u8,
145+
(lhs.b as f32 * rhs.clamp(0.0, 1.0)) as u8,
146+
)
147+
}
148+
149+
fn rgb_negate(rgb: &Rgb) -> Rgb {
150+
Rgb::new(255 - rgb.r, 255 - rgb.g, 255 - rgb.b)
151+
}
152+
153+
impl std::ops::Add<Rgb> for Rgb {
154+
type Output = Rgb;
155+
156+
fn add(self, rhs: Rgb) -> Self::Output {
157+
rgb_add(&self, &rhs)
133158
}
134-
);
159+
}
135160

136-
overload::overload!(
137-
(lhs: ?Rgb) - (rhs: ?Rgb) -> Rgb {
138-
Rgb::new(
139-
lhs.r.saturating_sub(rhs.r),
140-
lhs.g.saturating_sub(rhs.g),
141-
lhs.b.saturating_sub(rhs.b)
142-
)
161+
impl std::ops::Add<&Rgb> for Rgb {
162+
type Output = Rgb;
163+
164+
fn add(self, rhs: &Rgb) -> Self::Output {
165+
rgb_add(&self, rhs)
143166
}
144-
);
167+
}
145168

146-
overload::overload!(
147-
(lhs: ?Rgb) * (rhs: ?f32) -> Rgb {
148-
Rgb::new(
149-
(lhs.r as f32 * rhs.clamp(0.0, 1.0)) as u8,
150-
(lhs.g as f32 * rhs.clamp(0.0, 1.0)) as u8,
151-
(lhs.b as f32 * rhs.clamp(0.0, 1.0)) as u8
152-
)
169+
impl std::ops::Add<Rgb> for &Rgb {
170+
type Output = Rgb;
171+
172+
fn add(self, rhs: Rgb) -> Self::Output {
173+
rgb_add(self, &rhs)
153174
}
154-
);
175+
}
155176

156-
overload::overload!(
157-
(lhs: ?f32) * (rhs: ?Rgb) -> Rgb {
158-
Rgb::new(
159-
(rhs.r as f32 * lhs.clamp(0.0, 1.0)) as u8,
160-
(rhs.g as f32 * lhs.clamp(0.0, 1.0)) as u8,
161-
(rhs.b as f32 * lhs.clamp(0.0, 1.0)) as u8
162-
)
177+
impl std::ops::Add<&Rgb> for &Rgb {
178+
type Output = Rgb;
179+
180+
fn add(self, rhs: &Rgb) -> Self::Output {
181+
rgb_add(self, rhs)
182+
}
183+
}
184+
185+
impl std::ops::Sub<Rgb> for Rgb {
186+
type Output = Rgb;
187+
188+
fn sub(self, rhs: Rgb) -> Self::Output {
189+
rgb_sub(&self, &rhs)
190+
}
191+
}
192+
193+
impl std::ops::Sub<&Rgb> for Rgb {
194+
type Output = Rgb;
195+
196+
fn sub(self, rhs: &Rgb) -> Self::Output {
197+
rgb_sub(&self, rhs)
198+
}
199+
}
200+
201+
impl std::ops::Sub<Rgb> for &Rgb {
202+
type Output = Rgb;
203+
204+
fn sub(self, rhs: Rgb) -> Self::Output {
205+
rgb_sub(self, &rhs)
206+
}
207+
}
208+
209+
impl std::ops::Sub<&Rgb> for &Rgb {
210+
type Output = Rgb;
211+
212+
fn sub(self, rhs: &Rgb) -> Self::Output {
213+
rgb_sub(self, rhs)
214+
}
215+
}
216+
217+
impl std::ops::Mul<f32> for Rgb {
218+
type Output = Rgb;
219+
220+
fn mul(self, rhs: f32) -> Self::Output {
221+
rgb_mul_f32(&self, &rhs)
222+
}
223+
}
224+
225+
impl std::ops::Mul<&f32> for Rgb {
226+
type Output = Rgb;
227+
228+
fn mul(self, rhs: &f32) -> Self::Output {
229+
rgb_mul_f32(&self, rhs)
163230
}
164-
);
231+
}
232+
233+
impl std::ops::Mul<f32> for &Rgb {
234+
type Output = Rgb;
165235

166-
overload::overload!(
167-
-(rgb: ?Rgb) -> Rgb {
168-
Rgb::new(
169-
255 - rgb.r,
170-
255 - rgb.g,
171-
255 - rgb.b)
236+
fn mul(self, rhs: f32) -> Self::Output {
237+
rgb_mul_f32(self, &rhs)
172238
}
173-
);
239+
}
240+
241+
impl std::ops::Mul<&f32> for &Rgb {
242+
type Output = Rgb;
243+
244+
fn mul(self, rhs: &f32) -> Self::Output {
245+
rgb_mul_f32(self, rhs)
246+
}
247+
}
248+
249+
impl std::ops::Mul<Rgb> for f32 {
250+
type Output = Rgb;
251+
252+
fn mul(self, rhs: Rgb) -> Self::Output {
253+
rgb_mul_f32(&rhs, &self)
254+
}
255+
}
256+
257+
impl std::ops::Mul<&Rgb> for f32 {
258+
type Output = Rgb;
259+
260+
fn mul(self, rhs: &Rgb) -> Self::Output {
261+
rgb_mul_f32(rhs, &self)
262+
}
263+
}
264+
265+
impl std::ops::Mul<Rgb> for &f32 {
266+
type Output = Rgb;
267+
268+
fn mul(self, rhs: Rgb) -> Self::Output {
269+
rgb_mul_f32(&rhs, self)
270+
}
271+
}
272+
273+
impl std::ops::Mul<&Rgb> for &f32 {
274+
type Output = Rgb;
275+
276+
fn mul(self, rhs: &Rgb) -> Self::Output {
277+
rgb_mul_f32(rhs, self)
278+
}
279+
}
280+
281+
impl std::ops::Neg for Rgb {
282+
type Output = Rgb;
283+
284+
fn neg(self) -> Self::Output {
285+
rgb_negate(&self)
286+
}
287+
}
288+
289+
impl std::ops::Neg for &Rgb {
290+
type Output = Rgb;
291+
292+
fn neg(self) -> Self::Output {
293+
rgb_negate(self)
294+
}
295+
}

0 commit comments

Comments
 (0)
Please sign in to comment.