iced_graphics/
color.rs

1//! Manage colors for shaders.
2use crate::core::Color;
3
4use bytemuck::{Pod, Zeroable};
5
6/// A color packed as 4 floats representing RGBA channels.
7#[derive(Debug, Clone, Copy, PartialEq, Zeroable, Pod)]
8#[repr(C)]
9pub struct Packed([f32; 4]);
10
11impl Packed {
12    /// Returns the internal components of the [`Packed`] color.
13    pub fn components(self) -> [f32; 4] {
14        self.0
15    }
16}
17
18/// A flag that indicates whether the renderer should perform gamma correction.
19pub const GAMMA_CORRECTION: bool = internal::GAMMA_CORRECTION;
20
21/// Packs a [`Color`].
22pub fn pack(color: impl Into<Color>) -> Packed {
23    Packed(internal::pack(color.into()))
24}
25
26#[cfg(not(feature = "web-colors"))]
27mod internal {
28    use crate::core::Color;
29
30    pub const GAMMA_CORRECTION: bool = true;
31
32    pub fn pack(color: Color) -> [f32; 4] {
33        color.into_linear()
34    }
35}
36
37#[cfg(feature = "web-colors")]
38mod internal {
39    use crate::core::Color;
40
41    pub const GAMMA_CORRECTION: bool = false;
42
43    pub fn pack(color: Color) -> [f32; 4] {
44        [color.r, color.g, color.b, color.a]
45    }
46}