2023-06-06 01:33:23 +00:00
|
|
|
// Sources:
|
|
|
|
// https://iquilezles.org/articles/palettes/
|
|
|
|
// http://dev.thi.ng/gradients/
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq, Debug)]
|
|
|
|
pub struct ColorPalette {
|
|
|
|
a: [f64; 3],
|
|
|
|
b: [f64; 3],
|
|
|
|
c: [f64; 3],
|
|
|
|
d: [f64; 3],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ColorPalette {
|
2023-06-07 21:17:40 +00:00
|
|
|
pub fn _new(a: [f64; 3], b: [f64; 3], c: [f64; 3], d: [f64; 3]) -> Self {
|
2023-06-06 01:33:23 +00:00
|
|
|
ColorPalette { a, b, c, d }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(&self, t: f64) -> [f64; 3] {
|
|
|
|
[
|
2023-06-08 15:57:03 +00:00
|
|
|
(self.a[0] + self.b[0] * f64::cos(std::f64::consts::TAU * (self.c[0] * t + self.d[0])))
|
|
|
|
* 255.0,
|
|
|
|
(self.a[1] + self.b[1] * f64::cos(std::f64::consts::TAU * (self.c[1] * t + self.d[1])))
|
|
|
|
* 255.0,
|
|
|
|
(self.a[2] + self.b[2] * f64::cos(std::f64::consts::TAU * (self.c[2] * t + self.d[2])))
|
|
|
|
* 255.0,
|
2023-06-06 01:33:23 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2023-06-16 02:23:12 +00:00
|
|
|
pub fn _get_u8(&self, t: u8) -> [f64; 3] {
|
2023-06-07 20:14:19 +00:00
|
|
|
assert!(t > 0, "t must be greater than zero!");
|
2023-06-16 02:23:12 +00:00
|
|
|
self.get((2f64.powi(-(t.ilog2() as i32))) * (t as f64 + 0.5f64) - 1.0f64)
|
2023-06-06 01:33:23 +00:00
|
|
|
}
|
2023-06-13 23:32:39 +00:00
|
|
|
|
2023-12-30 01:39:00 +00:00
|
|
|
// pub fn get_uuid(&self, t: uuid::Uuid) -> [f64; 3] {
|
|
|
|
// // self.get(t.as_u128() as f64 / (u128::MAX) as f64)
|
|
|
|
// let mut r = SmallRng::seed_from_u64(t.as_u128() as u64);
|
|
|
|
// self.get(r.gen_range(0.0..1.0f64))
|
|
|
|
// }
|
|
|
|
|
2024-02-11 03:00:39 +00:00
|
|
|
pub fn get_ordered(&self, t: uuid::Uuid, ordered_uuids: &[uuid::Uuid])
|
2023-12-30 01:39:00 +00:00
|
|
|
-> [f64; 3] {
|
|
|
|
let index = ordered_uuids.iter().position(|&x| x == t).expect("uuid must be in list of uuids") + 1;
|
2024-02-11 03:00:39 +00:00
|
|
|
self.get(Self::space_evenly(index))
|
2023-12-30 01:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn space_evenly(x: usize) -> f64 {
|
|
|
|
let e: usize = (x.ilog2() + 1) as usize;
|
2024-02-11 03:00:39 +00:00
|
|
|
let d: usize = 2usize.pow(e as u32);
|
2023-12-30 01:39:00 +00:00
|
|
|
let n: usize = (2*x + 1) % d;
|
2024-02-11 03:00:39 +00:00
|
|
|
(n as f64) / (d as f64)
|
2023-06-13 23:32:39 +00:00
|
|
|
}
|
2023-06-06 01:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct Palettes;
|
|
|
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
impl Palettes {
|
|
|
|
pub const RAINBOW: ColorPalette = ColorPalette {
|
2023-06-07 20:14:19 +00:00
|
|
|
a: [0.500, 0.500, 0.500],
|
2023-06-13 23:39:18 +00:00
|
|
|
b: [0.700, 0.700, 0.700],
|
2023-06-07 20:14:19 +00:00
|
|
|
c: [0.800, 0.800, 0.800],
|
|
|
|
d: [0.000, 0.333, 0.667],
|
2023-06-06 01:33:23 +00:00
|
|
|
};
|
|
|
|
pub const YELLOW_PINK: ColorPalette = ColorPalette {
|
2023-06-07 20:14:19 +00:00
|
|
|
a: [0.500, 0.500, 0.320],
|
|
|
|
b: [0.500, 0.500, 0.500],
|
|
|
|
c: [0.100, 0.500, 0.360],
|
|
|
|
d: [0.000, 0.000, 0.650],
|
2023-06-06 01:33:23 +00:00
|
|
|
};
|
2023-12-30 01:39:00 +00:00
|
|
|
|
2023-06-06 01:33:23 +00:00
|
|
|
}
|