From 38b4234c3282a7c044c18b77234ee8c204efe171 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 7 Oct 2023 18:11:16 +0200 Subject: [PATCH] Add `#[inline]` to all color-related function --- crates/ecolor/src/color32.rs | 39 +++++++++++++++------------ crates/ecolor/src/hsva.rs | 21 +++++++++++++++ crates/ecolor/src/rgba.rs | 52 +++++++++++++++++++----------------- 3 files changed, 71 insertions(+), 41 deletions(-) diff --git a/crates/ecolor/src/color32.rs b/crates/ecolor/src/color32.rs index 6f2c1127..56b96589 100644 --- a/crates/ecolor/src/color32.rs +++ b/crates/ecolor/src/color32.rs @@ -16,14 +16,14 @@ pub struct Color32(pub(crate) [u8; 4]); impl std::ops::Index for Color32 { type Output = u8; - #[inline(always)] + #[inline] fn index(&self, index: usize) -> &u8 { &self.0[index] } } impl std::ops::IndexMut for Color32 { - #[inline(always)] + #[inline] fn index_mut(&mut self, index: usize) -> &mut u8 { &mut self.0[index] } @@ -63,23 +63,24 @@ impl Color32 { /// An ugly color that is planned to be replaced before making it to the screen. pub const TEMPORARY_COLOR: Color32 = Color32::from_rgb(64, 254, 0); - #[inline(always)] + #[inline] pub const fn from_rgb(r: u8, g: u8, b: u8) -> Self { Self([r, g, b, 255]) } - #[inline(always)] + #[inline] pub const fn from_rgb_additive(r: u8, g: u8, b: u8) -> Self { Self([r, g, b, 0]) } /// From `sRGBA` with premultiplied alpha. - #[inline(always)] + #[inline] pub const fn from_rgba_premultiplied(r: u8, g: u8, b: u8, a: u8) -> Self { Self([r, g, b, a]) } /// From `sRGBA` WITHOUT premultiplied alpha. + #[inline] pub fn from_rgba_unmultiplied(r: u8, g: u8, b: u8, a: u8) -> Self { if a == 255 { Self::from_rgb(r, g, b) // common-case optimization @@ -99,80 +100,83 @@ impl Color32 { } } - #[inline(always)] + #[inline] pub const fn from_gray(l: u8) -> Self { Self([l, l, l, 255]) } - #[inline(always)] + #[inline] pub const fn from_black_alpha(a: u8) -> Self { Self([0, 0, 0, a]) } + #[inline] pub fn from_white_alpha(a: u8) -> Self { Rgba::from_white_alpha(linear_f32_from_linear_u8(a)).into() } - #[inline(always)] + #[inline] pub const fn from_additive_luminance(l: u8) -> Self { Self([l, l, l, 0]) } - #[inline(always)] + #[inline] pub const fn is_opaque(&self) -> bool { self.a() == 255 } - #[inline(always)] + #[inline] pub const fn r(&self) -> u8 { self.0[0] } - #[inline(always)] + #[inline] pub const fn g(&self) -> u8 { self.0[1] } - #[inline(always)] + #[inline] pub const fn b(&self) -> u8 { self.0[2] } - #[inline(always)] + #[inline] pub const fn a(&self) -> u8 { self.0[3] } /// Returns an opaque version of self + #[inline] pub fn to_opaque(self) -> Self { Rgba::from(self).to_opaque().into() } /// Returns an additive version of self - #[inline(always)] + #[inline] pub const fn additive(self) -> Self { let [r, g, b, _] = self.to_array(); Self([r, g, b, 0]) } /// Is the alpha=0 ? - #[inline(always)] + #[inline] pub fn is_additive(self) -> bool { self.a() == 0 } /// Premultiplied RGBA - #[inline(always)] + #[inline] pub const fn to_array(&self) -> [u8; 4] { [self.r(), self.g(), self.b(), self.a()] } /// Premultiplied RGBA - #[inline(always)] + #[inline] pub const fn to_tuple(&self) -> (u8, u8, u8, u8) { (self.r(), self.g(), self.b(), self.a()) } + #[inline] pub fn to_srgba_unmultiplied(&self) -> [u8; 4] { Rgba::from(*self).to_srgba_unmultiplied() } @@ -198,6 +202,7 @@ impl Color32 { /// /// This is using linear space, which is not perceptually even. /// You may want to use [`Self::gamma_multiply`] instead. + #[inline] pub fn linear_multiply(self, factor: f32) -> Color32 { crate::ecolor_assert!(0.0 <= factor && factor <= 1.0); // As an unfortunate side-effect of using premultiplied alpha diff --git a/crates/ecolor/src/hsva.rs b/crates/ecolor/src/hsva.rs index 8a68cb93..1fa54a24 100644 --- a/crates/ecolor/src/hsva.rs +++ b/crates/ecolor/src/hsva.rs @@ -21,11 +21,13 @@ pub struct Hsva { } impl Hsva { + #[inline] pub fn new(h: f32, s: f32, v: f32, a: f32) -> Self { Self { h, s, v, a } } /// From `sRGBA` with premultiplied alpha + #[inline] pub fn from_srgba_premultiplied(srgba: [u8; 4]) -> Self { Self::from_rgba_premultiplied( linear_f32_from_gamma_u8(srgba[0]), @@ -36,6 +38,7 @@ impl Hsva { } /// From `sRGBA` without premultiplied alpha + #[inline] pub fn from_srgba_unmultiplied(srgba: [u8; 4]) -> Self { Self::from_rgba_unmultiplied( linear_f32_from_gamma_u8(srgba[0]), @@ -46,6 +49,7 @@ impl Hsva { } /// From linear RGBA with premultiplied alpha + #[inline] pub fn from_rgba_premultiplied(r: f32, g: f32, b: f32, a: f32) -> Self { #![allow(clippy::many_single_char_names)] if a == 0.0 { @@ -61,12 +65,14 @@ impl Hsva { } /// From linear RGBA without premultiplied alpha + #[inline] pub fn from_rgba_unmultiplied(r: f32, g: f32, b: f32, a: f32) -> Self { #![allow(clippy::many_single_char_names)] let (h, s, v) = hsv_from_rgb([r, g, b]); Hsva { h, s, v, a } } + #[inline] pub fn from_additive_rgb(rgb: [f32; 3]) -> Self { let (h, s, v) = hsv_from_rgb(rgb); Hsva { @@ -77,11 +83,13 @@ impl Hsva { } } + #[inline] pub fn from_rgb(rgb: [f32; 3]) -> Self { let (h, s, v) = hsv_from_rgb(rgb); Hsva { h, s, v, a: 1.0 } } + #[inline] pub fn from_srgb([r, g, b]: [u8; 3]) -> Self { Self::from_rgb([ linear_f32_from_gamma_u8(r), @@ -92,14 +100,17 @@ impl Hsva { // ------------------------------------------------------------------------ + #[inline] pub fn to_opaque(self) -> Self { Self { a: 1.0, ..self } } + #[inline] pub fn to_rgb(&self) -> [f32; 3] { rgb_from_hsv((self.h, self.s, self.v)) } + #[inline] pub fn to_srgb(&self) -> [u8; 3] { let [r, g, b] = self.to_rgb(); [ @@ -109,6 +120,7 @@ impl Hsva { ] } + #[inline] pub fn to_rgba_premultiplied(&self) -> [f32; 4] { let [r, g, b, a] = self.to_rgba_unmultiplied(); let additive = a < 0.0; @@ -120,12 +132,14 @@ impl Hsva { } /// Represents additive colors using a negative alpha. + #[inline] pub fn to_rgba_unmultiplied(&self) -> [f32; 4] { let Hsva { h, s, v, a } = *self; let [r, g, b] = rgb_from_hsv((h, s, v)); [r, g, b, a] } + #[inline] pub fn to_srgba_premultiplied(&self) -> [u8; 4] { let [r, g, b, a] = self.to_rgba_premultiplied(); [ @@ -136,6 +150,7 @@ impl Hsva { ] } + #[inline] pub fn to_srgba_unmultiplied(&self) -> [u8; 4] { let [r, g, b, a] = self.to_rgba_unmultiplied(); [ @@ -148,30 +163,35 @@ impl Hsva { } impl From for Rgba { + #[inline] fn from(hsva: Hsva) -> Rgba { Rgba(hsva.to_rgba_premultiplied()) } } impl From for Hsva { + #[inline] fn from(rgba: Rgba) -> Hsva { Self::from_rgba_premultiplied(rgba.0[0], rgba.0[1], rgba.0[2], rgba.0[3]) } } impl From for Color32 { + #[inline] fn from(hsva: Hsva) -> Color32 { Color32::from(Rgba::from(hsva)) } } impl From for Hsva { + #[inline] fn from(srgba: Color32) -> Hsva { Hsva::from(Rgba::from(srgba)) } } /// All ranges in 0-1, rgb is linear. +#[inline] pub fn hsv_from_rgb([r, g, b]: [f32; 3]) -> (f32, f32, f32) { #![allow(clippy::many_single_char_names)] let min = r.min(g.min(b)); @@ -195,6 +215,7 @@ pub fn hsv_from_rgb([r, g, b]: [f32; 3]) -> (f32, f32, f32) { } /// All ranges in 0-1, rgb is linear. +#[inline] pub fn rgb_from_hsv((h, s, v): (f32, f32, f32)) -> [f32; 3] { #![allow(clippy::many_single_char_names)] let h = (h.fract() + 1.0).fract(); // wrap diff --git a/crates/ecolor/src/rgba.rs b/crates/ecolor/src/rgba.rs index 849696dd..b40f70e3 100644 --- a/crates/ecolor/src/rgba.rs +++ b/crates/ecolor/src/rgba.rs @@ -13,20 +13,20 @@ pub struct Rgba(pub(crate) [f32; 4]); impl std::ops::Index for Rgba { type Output = f32; - #[inline(always)] + #[inline] fn index(&self, index: usize) -> &f32 { &self.0[index] } } impl std::ops::IndexMut for Rgba { - #[inline(always)] + #[inline] fn index_mut(&mut self, index: usize) -> &mut f32 { &mut self.0[index] } } -#[inline(always)] +#[inline] pub(crate) fn f32_hash(state: &mut H, f: f32) { if f == 0.0 { state.write_u8(0); @@ -57,17 +57,17 @@ impl Rgba { pub const GREEN: Rgba = Rgba::from_rgb(0.0, 1.0, 0.0); pub const BLUE: Rgba = Rgba::from_rgb(0.0, 0.0, 1.0); - #[inline(always)] + #[inline] pub const fn from_rgba_premultiplied(r: f32, g: f32, b: f32, a: f32) -> Self { Self([r, g, b, a]) } - #[inline(always)] + #[inline] pub fn from_rgba_unmultiplied(r: f32, g: f32, b: f32, a: f32) -> Self { Self([r * a, g * a, b * a, a]) } - #[inline(always)] + #[inline] pub fn from_srgba_premultiplied(r: u8, g: u8, b: u8, a: u8) -> Self { let r = linear_f32_from_gamma_u8(r); let g = linear_f32_from_gamma_u8(g); @@ -76,7 +76,7 @@ impl Rgba { Self::from_rgba_premultiplied(r, g, b, a) } - #[inline(always)] + #[inline] pub fn from_srgba_unmultiplied(r: u8, g: u8, b: u8, a: u8) -> Self { let r = linear_f32_from_gamma_u8(r); let g = linear_f32_from_gamma_u8(g); @@ -85,16 +85,17 @@ impl Rgba { Self::from_rgba_premultiplied(r * a, g * a, b * a, a) } - #[inline(always)] + #[inline] pub const fn from_rgb(r: f32, g: f32, b: f32) -> Self { Self([r, g, b, 1.0]) } - #[inline(always)] + #[inline] pub const fn from_gray(l: f32) -> Self { Self([l, l, l, 1.0]) } + #[inline] pub fn from_luminance_alpha(l: f32, a: f32) -> Self { crate::ecolor_assert!(0.0 <= l && l <= 1.0); crate::ecolor_assert!(0.0 <= a && a <= 1.0); @@ -102,34 +103,34 @@ impl Rgba { } /// Transparent black - #[inline(always)] + #[inline] pub fn from_black_alpha(a: f32) -> Self { crate::ecolor_assert!(0.0 <= a && a <= 1.0); Self([0.0, 0.0, 0.0, a]) } /// Transparent white - #[inline(always)] + #[inline] pub fn from_white_alpha(a: f32) -> Self { crate::ecolor_assert!(0.0 <= a && a <= 1.0, "a: {}", a); Self([a, a, a, a]) } /// Return an additive version of this color (alpha = 0) - #[inline(always)] + #[inline] pub fn additive(self) -> Self { let [r, g, b, _] = self.0; Self([r, g, b, 0.0]) } /// Is the alpha=0 ? - #[inline(always)] + #[inline] pub fn is_additive(self) -> bool { self.a() == 0.0 } /// Multiply with e.g. 0.5 to make us half transparent - #[inline(always)] + #[inline] pub fn multiply(self, alpha: f32) -> Self { Self([ alpha * self[0], @@ -139,22 +140,22 @@ impl Rgba { ]) } - #[inline(always)] + #[inline] pub fn r(&self) -> f32 { self.0[0] } - #[inline(always)] + #[inline] pub fn g(&self) -> f32 { self.0[1] } - #[inline(always)] + #[inline] pub fn b(&self) -> f32 { self.0[2] } - #[inline(always)] + #[inline] pub fn a(&self) -> f32 { self.0[3] } @@ -166,6 +167,7 @@ impl Rgba { } /// Returns an opaque version of self + #[inline] pub fn to_opaque(&self) -> Self { if self.a() == 0.0 { // Additive or fully transparent black. @@ -181,18 +183,19 @@ impl Rgba { } /// Premultiplied RGBA - #[inline(always)] + #[inline] pub fn to_array(&self) -> [f32; 4] { [self.r(), self.g(), self.b(), self.a()] } /// Premultiplied RGBA - #[inline(always)] + #[inline] pub fn to_tuple(&self) -> (f32, f32, f32, f32) { (self.r(), self.g(), self.b(), self.a()) } /// unmultiply the alpha + #[inline] pub fn to_rgba_unmultiplied(&self) -> [f32; 4] { let a = self.a(); if a == 0.0 { @@ -204,6 +207,7 @@ impl Rgba { } /// unmultiply the alpha + #[inline] pub fn to_srgba_unmultiplied(&self) -> [u8; 4] { let [r, g, b, a] = self.to_rgba_unmultiplied(); [ @@ -218,7 +222,7 @@ impl Rgba { impl std::ops::Add for Rgba { type Output = Rgba; - #[inline(always)] + #[inline] fn add(self, rhs: Rgba) -> Rgba { Rgba([ self[0] + rhs[0], @@ -232,7 +236,7 @@ impl std::ops::Add for Rgba { impl std::ops::Mul for Rgba { type Output = Rgba; - #[inline(always)] + #[inline] fn mul(self, other: Rgba) -> Rgba { Rgba([ self[0] * other[0], @@ -246,7 +250,7 @@ impl std::ops::Mul for Rgba { impl std::ops::Mul for Rgba { type Output = Rgba; - #[inline(always)] + #[inline] fn mul(self, factor: f32) -> Rgba { Rgba([ self[0] * factor, @@ -260,7 +264,7 @@ impl std::ops::Mul for Rgba { impl std::ops::Mul for f32 { type Output = Rgba; - #[inline(always)] + #[inline] fn mul(self, rgba: Rgba) -> Rgba { Rgba([ self * rgba[0],