From 946d61673d90389ce30c084e1f88ed52dfcbbd08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9ctor=20Ram=C3=B3n=20Jim=C3=A9nez?= Date: Tue, 6 Jun 2023 13:37:40 +0200 Subject: [PATCH] Implement explicit `Color::into_u32` instead of `Into` trait --- core/src/color.rs | 25 ++++++++++++------------- graphics/src/gradient.rs | 8 ++++---- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/core/src/color.rs b/core/src/color.rs index e3bbab7333..9ea6ccbf6f 100644 --- a/core/src/color.rs +++ b/core/src/color.rs @@ -120,6 +120,18 @@ impl Color { ] } + /// Converts the [`Color`] into a `u32` value containing its RGBA8 components. + pub fn into_u32(self) -> u32 { + let [r, g, b, a] = self.into_rgba8(); + + let r = (r as u32) << 24; + let g = (g as u32) << 16; + let b = (b as u32) << 8; + let a = a as u32; + + r | g | b | a + } + /// Inverts the [`Color`] in-place. pub fn invert(&mut self) { self.r = 1.0f32 - self.r; @@ -145,19 +157,6 @@ impl From<[f32; 4]> for Color { } } -impl Into for Color { - fn into(self) -> u32 { - let [r, g, b, a] = self.into_rgba8(); - - let r = (r as u32) << 24; - let g = (g as u32) << 16; - let b = (b as u32) << 8; - let a = a as u32; - - r | g | b | a - } -} - /// Creates a [`Color`] with shorter and cleaner syntax. /// /// # Examples diff --git a/graphics/src/gradient.rs b/graphics/src/gradient.rs index 4c886059b8..5f2fc27584 100644 --- a/graphics/src/gradient.rs +++ b/graphics/src/gradient.rs @@ -102,8 +102,8 @@ impl Linear { for (index, stop) in self.stops.iter().enumerate() { let (color, offset) = stop - .map_or((Color::default().into(), 2.0), |s| { - (s.color.into(), s.offset) + .map_or((Color::default().into_u32(), 2.0), |s| { + (s.color.into_u32(), s.offset) }); colors[index] = color; offsets[index] = offset; @@ -138,8 +138,8 @@ pub fn pack(gradient: &core::Gradient, bounds: Rectangle) -> Packed { for (index, stop) in linear.stops.iter().enumerate() { let (color, offset) = stop - .map_or((Color::default().into(), 2.0), |s| { - (s.color.into(), s.offset) + .map_or((Color::default().into_u32(), 2.0), |s| { + (s.color.into_u32(), s.offset) }); colors[index] = color;