Skip to main content

arctk/render/
gradient_builder.rs

1//! Gradient builder.
2
3use palette::{Gradient, LinSrgba};
4use serde::Deserialize;
5
6/// Colour gradient.
7#[derive(Deserialize)]
8pub struct GradientBuilder(
9    /// List of colours.
10    Vec<String>,
11);
12
13impl GradientBuilder {
14    /// Build the colour gradient instance.
15    #[inline]
16    #[must_use]
17    pub fn build(self) -> Gradient<LinSrgba> {
18        let mut cols = Vec::with_capacity(self.0.len());
19
20        for col in self.0 {
21            let col_arr = hex::decode(col.replace('#', ""))
22                .unwrap_or_else(|_| panic!("Failed to parse hexadecimal string: {col}."));
23
24            let r = f32::from(col_arr[0]) / 255.0;
25            let g = f32::from(col_arr[1]) / 255.0;
26            let b = f32::from(col_arr[2]) / 255.0;
27            let a = f32::from(col_arr[3]) / 255.0;
28
29            cols.push(LinSrgba::new(r, g, b, a));
30        }
31
32        Gradient::new(cols)
33    }
34}