1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! RGB colour with transparency representation.
use num_traits::Float;
use std::fmt::{Display, Formatter, Result as FmtResult};
use crate::{
error::{Result, validate_unit_component},
impl_transparent_colour, impl_transparent_convert, impl_transparent_display,
spaces::{Grey, GreyAlpha, Hsl, HslAlpha, Hsv, HsvAlpha, Lab, LabAlpha, Rgb, Srgb, SrgbAlpha, Xyz, XyzAlpha},
traits::{Colour, Convert},
};
/// RGB with alpha channel.
#[derive(Debug, Clone, Copy)]
pub struct RgbAlpha<T: Float + Send + Sync> {
/// Base colour
colour: Rgb<T>,
/// Alpha component in range [0, 1].
alpha: T,
}
impl<T: Float + Send + Sync> RgbAlpha<T> {
/// Create a new `RgbAlpha` instance.
///
/// # Arguments
///
/// * `red` - The red component, must be in range [0, 1]
/// * `green` - The green component, must be in range [0, 1]
/// * `blue` - The blue component, must be in range [0, 1]
/// * `alpha` - The alpha (transparency) component, must be in range [0, 1]
///
/// # Errors
///
/// Returns an error if any component is outside the range [0, 1].
pub fn new(red: T, green: T, blue: T, alpha: T) -> Result<Self> {
validate_unit_component(alpha, "alpha")?;
Ok(Self {
colour: Rgb::new(red, green, blue)?,
alpha,
})
}
/// Create a new `RgbAlpha` instance from a `Rgb` colour and an alpha component.
///
/// # Arguments
///
/// * `colour` - The base RGB colour
/// * `alpha` - The alpha (transparency) component, must be in range [0, 1]
///
/// # Errors
///
/// Returns an error if the alpha component is outside the range [0, 1].
fn new_colour_with_alpha(colour: Rgb<T>, alpha: T) -> Result<Self> {
validate_unit_component(alpha, "alpha")?;
Ok(Self { colour, alpha })
}
/// Get the base `colour`.
const fn colour(&self) -> &Rgb<T> {
&self.colour
}
/// Get the `red` component.
pub const fn red(&self) -> T {
self.colour.red()
}
/// Get the `green` component.
pub const fn green(&self) -> T {
self.colour.green()
}
/// Get the `blue` component.
pub const fn blue(&self) -> T {
self.colour.blue()
}
/// Get the `alpha` component.
pub const fn alpha(&self) -> T {
self.alpha
}
/// Set the `red` component.
///
/// # Arguments
///
/// * `red` - The new red value, must be in range [0, 1]
///
/// # Errors
///
/// Returns an error if the value is outside the range [0, 1].
pub fn set_red(&mut self, red: T) -> Result<()> {
self.colour.set_red(red)
}
/// Set the `green` component.
///
/// # Arguments
///
/// * `green` - The new green value, must be in range [0, 1]
///
/// # Errors
///
/// Returns an error if the value is outside the range [0, 1].
pub fn set_green(&mut self, green: T) -> Result<()> {
self.colour.set_green(green)
}
/// Set the `blue` component.
///
/// # Arguments
///
/// * `blue` - The new blue value, must be in range [0, 1]
///
/// # Errors
///
/// Returns an error if the value is outside the range [0, 1].
pub fn set_blue(&mut self, blue: T) -> Result<()> {
self.colour.set_blue(blue)
}
/// Set the `alpha` component.
///
/// # Arguments
///
/// * `alpha` - The new alpha value, must be in range [0, 1]
///
/// # Errors
///
/// Returns an error if the value is outside the range [0, 1].
pub fn set_alpha(&mut self, alpha: T) -> Result<()> {
validate_unit_component(alpha, "alpha")?;
self.alpha = alpha;
Ok(())
}
/// Set all components at once with validation.
///
/// # Arguments
///
/// * `red` - The red component, must be in range [0, 1]
/// * `green` - The green component, must be in range [0, 1]
/// * `blue` - The blue component, must be in range [0, 1]
/// * `alpha` - The alpha component, must be in range [0, 1]
///
/// # Errors
///
/// Returns an error if any component validation fails.
pub fn set_components(&mut self, red: T, green: T, blue: T, alpha: T) -> Result<()> {
validate_unit_component(red, "red")?;
validate_unit_component(green, "green")?;
validate_unit_component(blue, "blue")?;
validate_unit_component(alpha, "alpha")?;
// If all validations pass, update all components
self.colour = Rgb::new(red, green, blue)?;
self.alpha = alpha;
Ok(())
}
}
impl_transparent_colour!(RgbAlpha<T>, Rgb<T>, 3);
impl_transparent_convert!(RgbAlpha<T>, Rgb<T>);
impl_transparent_display!(RgbAlpha<T>);