Skip to content

Commit

Permalink
refactor rend::color
Browse files Browse the repository at this point in the history
- new trait: `ColorBase`.
- rename `COLOR` namespace to `Color`.
- remove `color_` prefix from namespace methods.
- misc. refactoring.
  • Loading branch information
joseluis committed Nov 25, 2024
1 parent c31ef93 commit 11d45bb
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 81 deletions.
3 changes: 2 additions & 1 deletion DOCS/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The format is based on [Keep a Changelog], and this project adheres to
- enum variants:
- `DataError::ElementNotFound`.
- traits:
- `ExtCellOption`, `ExtOptRes`, `MemPod`, `TypeResourced`, `Unit`.
- `ColorBase`, `ExtCellOption`, `ExtOptRes`, `MemPod`, `TypeResourced`, `Unit`.
- `WaveletCompressionVec`, `WaveletTransformVec`.
- associated methods and constants for:
- `Array`: `from_fn`, `contains_[from|to|between]`.
Expand Down Expand Up @@ -127,6 +127,7 @@ The format is based on [Keep a Changelog], and this project adheres to
- structs:
- make `data::dst` types use `MemPod` instead of `bytemuck::Pod`.
- rename:
- `COLOR` to `Color`.
- `GcdExt` to `GcdResult`.
- `AllocMap` to `HashMap` and `AllocSet` to `HashSet`.
- `AllocOrdMap` to `BTreeMap` and `AllocOrdSet` to `BTreeSet`.
Expand Down
41 changes: 41 additions & 0 deletions src/rend/color/base.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// devela::rend::color::base
//
//!
//

#[cfg(feature = "alloc")]
use crate::{vec_ as vec, Vec};

/// Base trait for general color data representation.
///
/// Provides a core interface for working with color data across different
/// formats and models, supporting both practical and scientific applications.
pub trait ColorBase {
/// The type used for color components.
type Component;

/// Returns the number of color components.
///
/// For example:
/// - RGB returns `3`
/// - Spectral data may return `n`
fn color_component_count(&self) -> usize;

/// Writes the color components to a pre-allocated buffer.
///
/// # Panics
/// Panics if the buffer size is less than `color_component_count()`.
// TODO: Return Error
fn color_components_write(&self, buffer: &mut [Self::Component]);

/// Returns a vector containing the color components.
#[cfg(feature = "alloc")]
fn color_components_vec(&self) -> Vec<Self::Component>
where
Self::Component: Default + Clone,
{
let mut buffer = vec![Self::Component::default(); self.color_component_count()];
self.color_components_write(&mut buffer);
buffer
}
}
8 changes: 4 additions & 4 deletions src/rend/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
//! Chromatic functionality.
//

mod base;
mod error;
mod r#struct;
mod r#trait;
mod namespace;
#[allow(unused_imports)]
pub use {error::*, r#struct::*, r#trait::*};
pub use {base::*, error::*, namespace::*};

pub(crate) mod all {
#[doc(inline)]
#[allow(unused_imports)]
pub use super::{error::*, r#struct::*, r#trait::*};
pub use super::{base::*, error::*, namespace::*};
}
47 changes: 17 additions & 30 deletions src/rend/color/struct.rs → src/rend/color/namespace.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,28 @@
// devela::rend::color::fns
// devela::rend::color::namespace
//
//! Standalone color functions and constants.
//

#[allow(unused_imports)]
use crate::code::{iif, paste, sf};
use crate::code::{iif, paste, sf, CONST};

#[cfg(all(_float_·, not(feature = "std")))]
#[allow(unused_imports, reason = "!std: powf, powi")]
use crate::num::ExtFloat;

/// color namespace for constants and methods
pub struct COLOR;
/// Color namespace for constants and methods
pub struct Color;

sf! { macro_rules! LUMINANCE_RED { () => { 0.212639 }; } pub(crate) use LUMINANCE_RED; }
sf! { macro_rules! LUMINANCE_GREEN { () => { 0.715169 }; } pub(crate) use LUMINANCE_GREEN; }
sf! { macro_rules! LUMINANCE_BLUE { () => { 0.072192 }; } pub(crate) use LUMINANCE_BLUE; }

impl COLOR {
/* constants */

/// The coefficient used for calculating the red luminance.
pub const LUMINANCE_RED: f32 = LUMINANCE_RED![];

/// The coefficient used for calculating the green luminance.
pub const LUMINANCE_GREEN: f32 = LUMINANCE_GREEN![];

/// The coefficient used for calculating the blue luminance.
pub const LUMINANCE_BLUE: f32 = LUMINANCE_BLUE![];
}

// $t: the floating-point primitive
// $cap: the capability feature that enables the given implementation. E.g "_f32".
#[doc = crate::doc_private!()]
///
///
/// # Args
/// $t: the floating-point primitive
/// $cap: the capability feature that enables the given implementation. E.g "_f32".
macro_rules! color_gamma_fns {
($($t:ty : $cap:literal),+) => { $( color_gamma_fns![@$t:$cap]; )+ };
(@$t:ty : $cap:literal) => { paste! {
impl COLOR {
impl Color {
#[doc = "Applies the `gamma` *(γ)* to a linear `" $t "` channel to make it non-linear."]
///
/// # Algorithm
Expand All @@ -50,26 +37,26 @@ macro_rules! color_gamma_fns {
/// $$
#[cfg(any(feature = "std", feature = $cap))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(any(feature = "std", feature = $cap))))]
pub fn [<color_gamma_apply_ $t>](c: $t, gamma: $t) -> $t {
iif![c <= 0.0031308; 12.92 * c; 1.055 * c.powf(1.0 / gamma) - 0.055]
pub fn [<gamma_apply_ $t>](c: $t, gamma: $t) -> $t {
iif![c <= 0.003_130_8; 12.92 * c; 1.055 * c.powf(1.0 / gamma) - 0.055]
}

#[doc = "Removes the `gamma` *(γ)* from a non-linear `" $t "` channel to make it linear."]
///
/// # Algorithm
/// $$
/// \begin{align}
/// \notag f_\text{remove}(c) = \large\begin{cases}
/// \Large\frac{c}{12.92},
/// \notag f_\text{remove}(c) = \begin{cases}
/// c / 12.92,
/// & \normalsize\text{if } c <= 0.04045 \cr
/// \left(\Large\frac{c+0.055}{1.055} - \normalsize 0.055\right)^{\gamma},
/// & \normalsize \text{if } c > 0.04045 \end{cases} \cr
/// \end{align}
/// $$
#[cfg(any(feature = "std", feature = $cap))]
#[cfg_attr(feature = "nightly_doc", doc(cfg(any(feature = "std", feature = $cap))))]
pub fn [<color_gamma_remove_ $t>](c: $t, gamma: $t) -> $t {
iif![c <= 0.04045; c / 12.92; ((c + 0.055) / (1.055)).powf(gamma)]
pub fn [<gamma_remove_ $t>](c: $t, gamma: $t) -> $t {
iif![c <= 0.040_45; c / 12.92; ((c + 0.055) / (1.055)).powf(gamma)]
}
}
}};
Expand Down
46 changes: 0 additions & 46 deletions src/rend/color/trait.rs

This file was deleted.

0 comments on commit 11d45bb

Please sign in to comment.