Skip to content

Commit

Permalink
Add #[inline] to all color-related function
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Oct 7, 2023
1 parent 2bc2fb9 commit 38b4234
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 41 deletions.
39 changes: 22 additions & 17 deletions crates/ecolor/src/color32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub struct Color32(pub(crate) [u8; 4]);
impl std::ops::Index<usize> for Color32 {
type Output = u8;

#[inline(always)]
#[inline]
fn index(&self, index: usize) -> &u8 {
&self.0[index]
}
}

impl std::ops::IndexMut<usize> for Color32 {
#[inline(always)]
#[inline]
fn index_mut(&mut self, index: usize) -> &mut u8 {
&mut self.0[index]
}
Expand Down Expand Up @@ -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
Expand All @@ -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()
}
Expand All @@ -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
Expand Down
21 changes: 21 additions & 0 deletions crates/ecolor/src/hsva.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand All @@ -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]),
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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),
Expand All @@ -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();
[
Expand All @@ -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;
Expand All @@ -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();
[
Expand All @@ -136,6 +150,7 @@ impl Hsva {
]
}

#[inline]
pub fn to_srgba_unmultiplied(&self) -> [u8; 4] {
let [r, g, b, a] = self.to_rgba_unmultiplied();
[
Expand All @@ -148,30 +163,35 @@ impl Hsva {
}

impl From<Hsva> for Rgba {
#[inline]
fn from(hsva: Hsva) -> Rgba {
Rgba(hsva.to_rgba_premultiplied())
}
}

impl From<Rgba> 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<Hsva> for Color32 {
#[inline]
fn from(hsva: Hsva) -> Color32 {
Color32::from(Rgba::from(hsva))
}
}

impl From<Color32> 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));
Expand All @@ -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
Expand Down
Loading

0 comments on commit 38b4234

Please sign in to comment.