1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-03 15:20:05 -04:00

Add #[inline] to all color-related function

This commit is contained in:
Emil Ernerfeldt
2023-10-07 18:11:16 +02:00
parent 2bc2fb9c39
commit 38b4234c32
3 changed files with 71 additions and 41 deletions

View File

@@ -13,20 +13,20 @@ pub struct Rgba(pub(crate) [f32; 4]);
impl std::ops::Index<usize> for Rgba {
type Output = f32;
#[inline(always)]
#[inline]
fn index(&self, index: usize) -> &f32 {
&self.0[index]
}
}
impl std::ops::IndexMut<usize> 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<H: std::hash::Hasher>(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<Rgba> 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<Rgba> for Rgba {
impl std::ops::Mul<f32> 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<f32> for Rgba {
impl std::ops::Mul<Rgba> for f32 {
type Output = Rgba;
#[inline(always)]
#[inline]
fn mul(self, rgba: Rgba) -> Rgba {
Rgba([
self * rgba[0],