Skip to main content

ink_rs/
color.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! Brush colour.
6
7use crate::ffi;
8
9/// The colour space a colour's components are expressed in.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
11pub enum ColorSpace {
12    #[default]
13    Srgb,
14    DisplayP3,
15}
16
17impl ColorSpace {
18    pub(crate) fn to_ffi(self) -> ffi::ColorSpace {
19        match self {
20            Self::Srgb => ffi::ColorSpace::kSrgb,
21            Self::DisplayP3 => ffi::ColorSpace::kDisplayP3,
22        }
23    }
24
25    pub(crate) fn from_ffi(space: ffi::ColorSpace) -> Self {
26        if space == ffi::ColorSpace::kDisplayP3 {
27            Self::DisplayP3
28        } else {
29            Self::Srgb
30        }
31    }
32}
33
34/// How a colour's components are encoded.
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
36pub enum ColorFormat {
37    /// Components are linear light.
38    Linear,
39    /// Components carry the colour space's transfer function. This is what
40    /// hex colours and most colour pickers give you.
41    #[default]
42    GammaEncoded,
43    /// Components are linear and already multiplied by alpha.
44    PremultipliedAlpha,
45}
46
47impl ColorFormat {
48    pub(crate) fn to_ffi(self) -> ffi::ColorFormat {
49        match self {
50            Self::Linear => ffi::ColorFormat::kLinear,
51            Self::GammaEncoded => ffi::ColorFormat::kGammaEncoded,
52            Self::PremultipliedAlpha => ffi::ColorFormat::kPremultipliedAlpha,
53        }
54    }
55}
56
57/// A colour, together with how to interpret its components.
58///
59/// Ink stores colour internally as linear, non-premultiplied sRGB, and converts
60/// on the way in and out. The `space` and `format` here say what the `r`, `g`,
61/// `b` and `a` fields mean, not how Ink will store them.
62#[derive(Debug, Clone, Copy, PartialEq)]
63pub struct Color {
64    pub r: f32,
65    pub g: f32,
66    pub b: f32,
67    pub a: f32,
68    pub space: ColorSpace,
69    pub format: ColorFormat,
70}
71
72impl Color {
73    pub const BLACK: Color = Color::srgb(0.0, 0.0, 0.0, 1.0);
74    pub const WHITE: Color = Color::srgb(1.0, 1.0, 1.0, 1.0);
75    pub const TRANSPARENT: Color = Color::srgb(0.0, 0.0, 0.0, 0.0);
76
77    /// A gamma-encoded sRGB colour, the usual way to spell a colour by hand.
78    pub const fn srgb(r: f32, g: f32, b: f32, a: f32) -> Self {
79        Self {
80            r,
81            g,
82            b,
83            a,
84            space: ColorSpace::Srgb,
85            format: ColorFormat::GammaEncoded,
86        }
87    }
88
89    /// A gamma-encoded sRGB colour from 8-bit components.
90    pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
91        Self::srgb(
92            f32::from(r) / 255.0,
93            f32::from(g) / 255.0,
94            f32::from(b) / 255.0,
95            f32::from(a) / 255.0,
96        )
97    }
98
99    /// A gamma-encoded sRGB colour from a packed `0xRRGGBBAA` value.
100    pub fn from_packed_rgba(rgba: u32) -> Self {
101        Self::from_rgba8(
102            (rgba >> 24) as u8,
103            (rgba >> 16) as u8,
104            (rgba >> 8) as u8,
105            rgba as u8,
106        )
107    }
108
109    /// The same colour with a different alpha.
110    pub fn with_alpha(self, a: f32) -> Self {
111        Self { a, ..self }
112    }
113
114    pub(crate) fn to_ffi(self) -> ffi::Rgba {
115        ffi::Rgba {
116            r: self.r,
117            g: self.g,
118            b: self.b,
119            a: self.a,
120        }
121    }
122
123    pub(crate) fn from_ffi(rgba: ffi::Rgba, space: ColorSpace, format: ColorFormat) -> Self {
124        Self {
125            r: rgba.r,
126            g: rgba.g,
127            b: rgba.b,
128            a: rgba.a,
129            space,
130            format,
131        }
132    }
133}
134
135impl Default for Color {
136    fn default() -> Self {
137        Self::BLACK
138    }
139}