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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! Trait implemented by all colour types.
use num_traits::Float;
use std::ops::AddAssign;
use crate::error::{InterpolationError, Result};
/// Common trait for all colour types.
pub trait Colour<T: Float + Send + Sync, const N: usize> {
/// Number of components in the colour.
const NUM_COMPONENTS: usize = N;
/// Create a new colour from a hex string.
///
/// # Errors
///
/// Returns an error if the hex string is invalid or out of range.
fn from_hex(hex: &str) -> Result<Self>
where
Self: Sized;
/// Convert the colour to a hex string.
///
/// # Errors
///
/// Returns an error if conversion fails or components are out of range.
fn to_hex(&self) -> Result<String>;
/// Create a new colour from a byte array.
///
/// # Errors
///
/// Returns an error if byte conversion fails or values are out of range.
fn from_bytes(bytes: [u8; N]) -> Result<Self>
where
Self: Sized;
/// Convert the colour to a byte array.
///
/// # Errors
///
/// Returns an error if conversion fails or components are out of range.
fn to_bytes(self) -> Result<[u8; N]>;
/// Linear interpolate between two colours of the same type.
///
/// # Arguments
///
/// * `lhs` - The left-hand side colour (at t=0)
/// * `rhs` - The right-hand side colour (at t=1)
/// * `t` - The interpolation factor, must be in range [0, 1]
///
/// # Errors
///
/// Returns an error if the interpolation factor is outside [0, 1] or if
/// the interpolation calculation fails.
fn lerp(lhs: &Self, rhs: &Self, t: T) -> Result<Self>
where
Self: Sized;
/// Mix multiple colours using weighted interpolation.
///
/// This method combines multiple colours using their associated weights.
/// The weights do not need to sum to 1.0 - they will be normalized internally.
///
/// # Arguments
///
/// * `colours` - A slice of colours to mix, must not be empty
/// * `weights` - A slice of weights corresponding to each colour, must be non-negative
///
/// # Errors
///
/// Returns an error if:
/// - The colour list is empty
/// - The lengths of colours and weights do not match
/// - Any weight is negative
/// - The sum of weights is zero or invalid
/// - Interpolation calculations fail
///
/// # Examples
///
/// ```rust,ignore
/// // Mix red and blue with equal weights
/// let red = Rgb::new(1.0, 0.0, 0.0)?;
/// let blue = Rgb::new(0.0, 0.0, 1.0)?;
/// let purple = Rgb::mix(&[red, blue], &[1.0, 1.0])?;
///
/// // Mix three colours with different weights
/// let result = Rgb::mix(&[red, green, blue], &[0.5, 0.3, 0.2])?;
/// ```
fn mix(colours: &[Self], weights: &[T]) -> Result<Self>
where
Self: Clone,
T: AddAssign,
{
// Validate inputs
if colours.is_empty() {
return Err(InterpolationError::EmptyColourList.into());
}
if colours.len() != weights.len() {
return Err(InterpolationError::MismatchedArrayLengths {
colours: colours.len(),
weights: weights.len(),
}
.into());
}
// Validate all weights are non-negative
for (i, &weight) in weights.iter().enumerate() {
if weight < T::zero() {
return Err(InterpolationError::NegativeWeight {
weight: weight.to_f64().unwrap_or(f64::NAN),
index: i,
}
.into());
}
}
// Handle the single colour case
if colours.len() == 1 {
return Ok(colours[0].clone());
}
// Calculate total weight and validate it's not zero
let total_weight = weights.iter().fold(T::zero(), |acc, &w| acc + w);
if total_weight <= T::zero() {
return Err(InterpolationError::InvalidWeightSum.into());
}
// Normalize weights and perform weighted mixing
let mut result = colours[0].clone();
let mut accumulated_normalized_weight = weights[0] / total_weight;
for i in 1..colours.len() {
let normalized_weight = weights[i] / total_weight;
// Calculate interpolation factor for this step
// t = current_weight / (accumulated_weight + current_weight)
let denominator = accumulated_normalized_weight + normalized_weight;
if denominator <= T::zero() {
return Err(InterpolationError::InvalidWeightSum.into());
}
let t = normalized_weight / denominator;
// Interpolate between current result and next colour
result = Self::lerp(&result, &colours[i], t)?;
// Update accumulated weight
accumulated_normalized_weight += normalized_weight;
}
Ok(result)
}
/// Mix two colours with specified weights.
///
/// This is a convenience method for mixing exactly two colours.
///
/// # Arguments
///
/// * `colour1` - The first colour
/// * `weight1` - Weight for the first colour, must be non-negative
/// * `colour2` - The second colour
/// * `weight2` - Weight for the second colour, must be non-negative
///
/// # Errors
///
/// Returns an error if weights are negative, sum to zero, or if interpolation fails.
fn mix_two(colour1: &Self, weight1: T, colour2: &Self, weight2: T) -> Result<Self>
where
Self: Clone,
T: AddAssign,
{
Self::mix(&[colour1.clone(), colour2.clone()], &[weight1, weight2])
}
/// Create a gradient between two colours with a specified number of steps.
///
/// # Arguments
///
/// * `start` - The starting colour
/// * `end` - The ending colour
/// * `steps` - Number of colours in the gradient (including start and end)
///
/// # Errors
///
/// Returns an error if steps is less than 2 or if interpolation fails.
fn gradient(start: &Self, end: &Self, steps: usize) -> Result<Vec<Self>>
where
Self: Clone,
{
if steps < 2 {
return Err(InterpolationError::InvalidGradientSteps { steps }.into());
}
if steps == 2 {
return Ok(vec![start.clone(), end.clone()]);
}
let mut gradient = Vec::with_capacity(steps);
let denominator = T::from(steps - 1).ok_or_else(|| InterpolationError::Math {
operation: format!("Converting steps {steps} to target type"),
})?;
for i in 0..steps {
let t = T::from(i).ok_or_else(|| InterpolationError::Math {
operation: format!("Converting step index {i} to target type"),
})? / denominator;
gradient.push(Self::lerp(start, end, t)?);
}
Ok(gradient)
}
}