|
| 1 | +use std::time::Instant; |
| 2 | + |
| 3 | +use ratatui::{ |
| 4 | + layout::Rect, |
| 5 | + style::Color, |
| 6 | +}; |
| 7 | +use tachyonfx::{ |
| 8 | + fx, |
| 9 | + Effect, |
| 10 | + HslConvertable, |
| 11 | +}; |
| 12 | + |
| 13 | +use tachyonfx::Interpolatable; |
| 14 | + |
| 15 | +pub trait IndexResolver<T: Clone> { |
| 16 | + fn resolve(idx: usize, data: &[T]) -> &T; |
| 17 | +} |
| 18 | + |
| 19 | +#[derive(Clone, Debug)] |
| 20 | +pub struct ColorCycle<T: IndexResolver<Color>> { |
| 21 | + colors: Vec<Color>, |
| 22 | + _marker: std::marker::PhantomData<T>, |
| 23 | +} |
| 24 | + |
| 25 | +#[derive(Clone, Debug)] |
| 26 | +pub struct PingPongCycle; |
| 27 | + |
| 28 | +impl IndexResolver<Color> for PingPongCycle { |
| 29 | + fn resolve(idx: usize, data: &[Color]) -> &Color { |
| 30 | + let dbl_idx = idx % (2 * data.len()); |
| 31 | + let final_index = if dbl_idx < data.len() { |
| 32 | + dbl_idx |
| 33 | + } else { |
| 34 | + 2 * data.len() - 1 - dbl_idx |
| 35 | + }; |
| 36 | + |
| 37 | + data.get(final_index) |
| 38 | + .expect("ColorCycle: index out of bounds") |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +pub type PingPongColorCycle = ColorCycle<PingPongCycle>; |
| 43 | + |
| 44 | +#[derive(Clone, Debug)] |
| 45 | +pub struct RepeatingCycle; |
| 46 | + |
| 47 | +impl IndexResolver<Color> for RepeatingCycle { |
| 48 | + fn resolve(idx: usize, data: &[Color]) -> &Color { |
| 49 | + data.get(idx % data.len()) |
| 50 | + .expect("ColorCycle: index out of bounds") |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +pub type RepeatingColorCycle = ColorCycle<RepeatingCycle>; |
| 55 | + |
| 56 | +impl<T> ColorCycle<T> |
| 57 | +where |
| 58 | + T: IndexResolver<Color>, |
| 59 | +{ |
| 60 | + pub fn new(initial_color: Color, colors: &[(usize, Color)]) -> Self { |
| 61 | + let mut gradient = vec![initial_color]; |
| 62 | + colors |
| 63 | + .iter() |
| 64 | + .fold((0, initial_color), |(_, prev_color), (len, color)| { |
| 65 | + (0..=*len).for_each(|i| { |
| 66 | + let color = prev_color.lerp(color, i as f32 / *len as f32); |
| 67 | + gradient.push(color); |
| 68 | + }); |
| 69 | + gradient.push(*color); |
| 70 | + (*len, *color) |
| 71 | + }); |
| 72 | + |
| 73 | + Self { |
| 74 | + colors: gradient, |
| 75 | + _marker: std::marker::PhantomData, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + pub fn color_at(&self, idx: usize) -> &Color { |
| 80 | + T::resolve(idx, &self.colors) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Creates a repeating color cycle based on a base color. |
| 85 | +/// |
| 86 | +/// # Arguments |
| 87 | +/// * `base_color` - Primary color to derive the cycle from |
| 88 | +/// * `length_multiplier` - Factor to adjust the cycle length |
| 89 | +/// |
| 90 | +/// # Returns |
| 91 | +/// A ColorCycle instance with derived colors and adjusted steps. |
| 92 | +fn create_color_cycle( |
| 93 | + base_color: Color, |
| 94 | + length_multiplier: usize, |
| 95 | +) -> ColorCycle<RepeatingCycle> { |
| 96 | + let color_step: usize = 7 * length_multiplier; |
| 97 | + |
| 98 | + let (h, s, l) = base_color.to_hsl(); |
| 99 | + |
| 100 | + let color_l = Color::from_hsl(h, s, 80.0); |
| 101 | + let color_d = Color::from_hsl(h, s, 40.0); |
| 102 | + |
| 103 | + RepeatingColorCycle::new(base_color, &[ |
| 104 | + (4 * length_multiplier, color_d), |
| 105 | + (2 * length_multiplier, color_l), |
| 106 | + ( |
| 107 | + 4 * length_multiplier, |
| 108 | + Color::from_hsl((h - 25.0) % 360.0, s, (l + 10.0).min(100.0)), |
| 109 | + ), |
| 110 | + ( |
| 111 | + color_step, |
| 112 | + Color::from_hsl(h, (s - 20.0).max(0.0), (l + 10.0).min(100.0)), |
| 113 | + ), |
| 114 | + ( |
| 115 | + color_step, |
| 116 | + Color::from_hsl((h + 25.0) % 360.0, s, (l + 10.0).min(100.0)), |
| 117 | + ), |
| 118 | + ( |
| 119 | + color_step, |
| 120 | + Color::from_hsl(h, (s + 20.0).max(0.0), (l + 10.0).min(100.0)), |
| 121 | + ), |
| 122 | + ]) |
| 123 | +} |
| 124 | + |
| 125 | +/// Creates an animated border effect using color cycling. |
| 126 | +/// |
| 127 | +/// # Arguments |
| 128 | +/// * `base_color` - The primary color to base the cycling effect on |
| 129 | +/// * `area` - The rectangular area where the effect should be rendered |
| 130 | +/// |
| 131 | +/// # Returns |
| 132 | +/// |
| 133 | +/// An Effect that animates a border around the specified area using cycled |
| 134 | +/// colors |
| 135 | +pub fn create_border_effect( |
| 136 | + base_color: Color, |
| 137 | + speed: f32, |
| 138 | + length: usize, |
| 139 | + area: Rect, |
| 140 | +) -> Effect { |
| 141 | + let color_cycle = create_color_cycle(base_color, length); |
| 142 | + |
| 143 | + let effect = |
| 144 | + fx::effect_fn_buf(Instant::now(), u32::MAX, move |started_at, ctx, buf| { |
| 145 | + let elapsed = started_at.elapsed().as_secs_f32(); |
| 146 | + |
| 147 | + // speed n cells/s |
| 148 | + let idx = (elapsed * speed) as usize; |
| 149 | + |
| 150 | + let area = ctx.area; |
| 151 | + |
| 152 | + let mut update_cell = |(x, y): (u16, u16), idx: usize| { |
| 153 | + if let Some(cell) = buf.cell_mut((x, y)) { |
| 154 | + cell.set_fg(*color_cycle.color_at(idx)); |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + (area.x..area.right()).enumerate().for_each(|(i, x)| { |
| 159 | + update_cell((x, area.y), idx + i); |
| 160 | + }); |
| 161 | + |
| 162 | + let cell_idx_offset = area.width as usize; |
| 163 | + (area.y + 1..area.bottom() - 1) |
| 164 | + .enumerate() |
| 165 | + .for_each(|(i, y)| { |
| 166 | + update_cell((area.right() - 1, y), idx + i + cell_idx_offset); |
| 167 | + }); |
| 168 | + |
| 169 | + let cell_idx_offset = |
| 170 | + cell_idx_offset + area.height.saturating_sub(2) as usize; |
| 171 | + (area.x..area.right()).rev().enumerate().for_each(|(i, x)| { |
| 172 | + update_cell((x, area.bottom() - 1), idx + i + cell_idx_offset); |
| 173 | + }); |
| 174 | + |
| 175 | + let cell_idx_offset = cell_idx_offset + area.width as usize; |
| 176 | + (area.y + 1..area.bottom()) |
| 177 | + .rev() |
| 178 | + .enumerate() |
| 179 | + .for_each(|(i, y)| { |
| 180 | + update_cell((area.x, y), idx + i + cell_idx_offset); |
| 181 | + }); |
| 182 | + }); |
| 183 | + |
| 184 | + effect.with_area(area) |
| 185 | +} |
0 commit comments