devela/media/color/
base.rs

1// devela::media::color::base
2//
3//!
4//
5
6#[cfg(feature = "alloc")]
7use crate::{vec_ as vec, Vec};
8
9/// Base trait for general color data representation.
10///
11/// Provides a core interface for working with color data across different
12/// formats and models, supporting both practical and scientific applications.
13pub trait ColorBase {
14    /// The type used for color components.
15    type Component;
16
17    /// Returns the number of color components.
18    ///
19    /// For example:
20    /// - RGB returns `3`
21    /// - Spectral data may return `n`
22    fn color_component_count(&self) -> usize;
23
24    /// Writes the color components to a pre-allocated buffer.
25    ///
26    /// # Panics
27    /// Panics if the buffer size is less than `color_component_count()`.
28    // TODO: Return Error
29    fn color_components_write(&self, buffer: &mut [Self::Component]);
30
31    /// Returns a vector containing the color components.
32    #[cfg(feature = "alloc")]
33    fn color_components_vec(&self) -> Vec<Self::Component>
34    where
35        Self::Component: Default + Clone,
36    {
37        let mut buffer = vec![Self::Component::default(); self.color_component_count()];
38        self.color_components_write(&mut buffer);
39        buffer
40    }
41}