Skip to main content

agg_gui/
color.rs

1//! Color type for agg-gui.
2//!
3//! Colors are stored as f32 RGBA in linear space. Conversion to AGG's `Rgba8`
4//! happens at the rasterizer boundary.
5
6use agg_rust::color::Rgba8;
7
8/// An RGBA color with f32 components in [0.0, 1.0].
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct Color {
11    pub r: f32,
12    pub g: f32,
13    pub b: f32,
14    pub a: f32,
15}
16
17impl Color {
18    pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
19        Self { r, g, b, a }
20    }
21
22    pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
23        Self { r, g, b, a: 1.0 }
24    }
25
26    pub const fn white() -> Self {
27        Self::rgb(1.0, 1.0, 1.0)
28    }
29
30    pub const fn black() -> Self {
31        Self::rgb(0.0, 0.0, 0.0)
32    }
33
34    pub const fn transparent() -> Self {
35        Self::rgba(0.0, 0.0, 0.0, 0.0)
36    }
37
38    pub fn with_alpha(self, a: f32) -> Self {
39        Self { a, ..self }
40    }
41
42    /// Convert to AGG's 8-bit RGBA format (used at the rasterizer boundary).
43    pub(crate) fn to_rgba8(self) -> Rgba8 {
44        Rgba8::new(
45            (self.r * 255.0).clamp(0.0, 255.0) as u32,
46            (self.g * 255.0).clamp(0.0, 255.0) as u32,
47            (self.b * 255.0).clamp(0.0, 255.0) as u32,
48            (self.a * 255.0).clamp(0.0, 255.0) as u32,
49        )
50    }
51}
52
53impl Default for Color {
54    fn default() -> Self {
55        Self::black()
56    }
57}