| 1 | use std::{cell::RefCell, sync::LazyLock, time::Duration}; |
| 2 | |
| 3 | use codewhale_palette::{UiTheme, symbol_needs_text_contrast}; |
| 4 | use codewhale_ratatui::{ |
| 5 | OceanRamp, |
| 6 | color::{ColorDepth, contrast_ratio, mix_oklab, oklab_ramp, rgb}, |
| 7 | ocean::{OceanPaintFacts, ocean_volumetric_caustic}, |
| 8 | theme::LOGO_TOP, |
| 9 | }; |
| 10 | use ratatui::{ |
| 11 | buffer::{Buffer, Cell}, |
| 12 | layout::Rect, |
| 13 | style::{Color, Modifier}, |
| 14 | }; |
| 15 | |
| 16 | static WATER: LazyLock<[Color; 256]> = LazyLock::new(|| { |
| 17 | let shallows = mix_oklab(rgb(LOGO_TOP), rgb(0x159a9f), 0.74); |
| 18 | let stops = [ |
| 19 | OceanRamp::DEEP, |
| 20 | rgb(0x0a2c4a), |
| 21 | rgb(0x0b546f), |
| 22 | shallows, |
| 23 | rgb(0xbbf3e8), |
| 24 | ]; |
| 25 | std::array::from_fn(|index| oklab_ramp(&stops, index as f32 / 255.0)) |
| 26 | }); |
| 27 | |
| 28 | const INDEXED_DITHER: [[u8; 8]; 8] = [ |
| 29 | [56, 13, 49, 25, 39, 7, 46, 19], |
| 30 | [8, 41, 30, 2, 53, 18, 62, 28], |
| 31 | [51, 17, 60, 34, 24, 42, 4, 36], |
| 32 | [32, 1, 47, 9, 57, 12, 54, 23], |
| 33 | [61, 37, 21, 43, 16, 29, 48, 10], |
| 34 | [5, 27, 52, 6, 63, 35, 20, 44], |
| 35 | [58, 33, 11, 40, 31, 0, 50, 15], |
| 36 | [3, 45, 22, 59, 14, 55, 26, 38], |
| 37 | ]; |
| 38 | |
| 39 | fn indexed_water(light: u8, threshold: u8) -> Color { |
| 40 | let shade = u16::from(light) * 4; |
| 41 | let dither = u16::from(threshold) * 4 + 2; |
| 42 | Color::Indexed(233 + ((shade + dither) / 255) as u8) |
| 43 | } |
| 44 | |
| 45 | fn illumination(x: f32, depth: f32) -> f32 { |
| 46 | let transmission = 1.0 - depth; |
| 47 | let crown = x + 0.57 * depth; |
| 48 | let haze = (-2.6 * (crown - 0.82).powi(2)).exp(); |
| 49 | let surface = (-10.0 * depth).exp(); |
| 50 | let refracted = crown + 0.008 * (20.0 * crown + 3.0 * depth).sin(); |
| 51 | let shaft = |source: f32, aperture: f32| { |
| 52 | let distance = (refracted - source) / (aperture + 0.032 * depth); |
| 53 | (-distance * distance).exp() |
| 54 | }; |
| 55 | let beams = 0.33 * shaft(0.81, 0.053) |
| 56 | + 0.17 * shaft(0.736, 0.024) |
| 57 | + 0.16 * shaft(0.94, 0.034) |
| 58 | + 0.12 * shaft(0.59, 0.030) |
| 59 | + 0.08 * shaft(0.38, 0.031); |
| 60 | let cross_wave = (x * 33.0 - depth * 16.0 + 1.4 * (x * 12.0).sin()).sin(); |
| 61 | let swell = (depth * 83.0 + 2.5 * (x * 14.0 + depth * 9.0).sin()).sin(); |
| 62 | let ripple = (-4.0 * (cross_wave + 0.8 * swell).powi(2)).exp(); |
| 63 | let scattered = 0.045 + 0.32 * transmission + 0.15 * transmission * haze; |
| 64 | scattered + (0.22 + 0.78 * transmission) * beams + surface * haze * (0.055 + 0.15 * ripple) |
| 65 | } |
| 66 | |
| 67 | #[derive(Default)] |
| 68 | struct LightMap { |
| 69 | width: usize, |
| 70 | height: usize, |
| 71 | levels: Vec<u8>, |
| 72 | } |
| 73 | |
| 74 | impl LightMap { |
| 75 | fn fit(&mut self, viewport: Rect) { |
| 76 | let width = usize::from(viewport.width.min(512)); |
| 77 | let height = usize::from(viewport.height.min(256)); |
| 78 | if (width, height) == (self.width, self.height) { |
| 79 | return; |
| 80 | } |
| 81 | self.width = width; |
| 82 | self.height = height; |
| 83 | self.levels.clear(); |
| 84 | self.levels.reserve(width * height); |
| 85 | let dx = 0.25 / width as f32; |
| 86 | let dy = 0.25 / height as f32; |
| 87 | for y in 0..height { |
| 88 | let depth = (y as f32 + 0.5) / height as f32; |
| 89 | for x in 0..width { |
| 90 | let u = (x as f32 + 0.5) / width as f32; |
| 91 | let light = (illumination(u - dx, depth - dy) |
| 92 | + illumination(u + dx, depth - dy) |
| 93 | + illumination(u - dx, depth + dy) |
| 94 | + illumination(u + dx, depth + dy)) |
| 95 | * 0.25; |
| 96 | self.levels |
| 97 | .push((light * 255.0).round().clamp(0.0, 255.0) as u8); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | thread_local! { |
| 104 | static LIGHT: RefCell<LightMap> = RefCell::default(); |
| 105 | } |
| 106 | |
| 107 | fn water_for_ink( |
| 108 | cell: &Cell, |
| 109 | level: u8, |
| 110 | shade: impl Fn(u8) -> Color, |
| 111 | depth: ColorDepth, |
| 112 | ui: &UiTheme, |
| 113 | ) -> Option<Color> { |
| 114 | let water = shade(level); |
| 115 | if !symbol_needs_text_contrast(cell.symbol()) { |
| 116 | return Some(water); |
| 117 | } |
| 118 | let ink = crate::tui::color_compat::project_ocean_ink(cell, cell.bg, depth, ui); |
| 119 | let floor = if cell.fg == ui.border { |
| 120 | 1.0 |
| 121 | } else if cell.fg == ui.text_dim || cell.fg == ui.text_hint { |
| 122 | 3.0 |
| 123 | } else { |
| 124 | 4.5 |
| 125 | }; |
| 126 | [water, shade(level - level / 4), shade(level / 2)] |
| 127 | .into_iter() |
| 128 | .find(|ground| contrast_ratio(ink, *ground).is_some_and(|ratio| ratio >= floor)) |
| 129 | } |
| 130 | |
| 131 | fn native_row_shift(column: super::OceanColumn, y: u16) -> [i16; 3] { |
| 132 | let row = y.saturating_sub(column.viewport.y); |
| 133 | let height = column.viewport.height; |
| 134 | let ramp = column.ramp.native(); |
| 135 | let baseline = ramp.color_at_context_oklab(row, height, 0); |
| 136 | let active = match column.phase { |
| 137 | super::ShellPhase::Idle | super::ShellPhase::Typing => { |
| 138 | ramp.color_at_context_oklab(row, height, column.context_percent) |
| 139 | } |
| 140 | super::ShellPhase::Working | super::ShellPhase::Verifying if !column.animated => ramp |
| 141 | .color_at_phase_context_oklab( |
| 142 | row, |
| 143 | height, |
| 144 | Duration::ZERO, |
| 145 | super::native_phase(column.phase), |
| 146 | column.context_percent, |
| 147 | ), |
| 148 | _ => column.color_at_y(y), |
| 149 | }; |
| 150 | match (active, baseline) { |
| 151 | (Color::Rgb(r, g, b), Color::Rgb(r0, g0, b0)) => [ |
| 152 | i16::from(r) - i16::from(r0), |
| 153 | i16::from(g) - i16::from(g0), |
| 154 | i16::from(b) - i16::from(b0), |
| 155 | ], |
| 156 | _ => [0; 3], |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | pub(super) fn paint( |
| 161 | column: super::OceanColumn, |
| 162 | area: Rect, |
| 163 | buf: &mut Buffer, |
| 164 | depth: ColorDepth, |
| 165 | ui: &UiTheme, |
| 166 | facts: &OceanPaintFacts<'_>, |
| 167 | ) { |
| 168 | let viewport = column.viewport; |
| 169 | let area = area.intersection(viewport).intersection(buf.area); |
| 170 | if area.is_empty() { |
| 171 | return; |
| 172 | } |
| 173 | let indexed = depth == ColorDepth::Ansi256; |
| 174 | let transmission = 255 - u16::from(column.session_depth) * 3 / 4; |
| 175 | let phase_strength = 255 - i16::from(column.session_depth) / 2; |
| 176 | let shimmer = column.animated |
| 177 | && column.presence > 0 |
| 178 | && matches!( |
| 179 | column.phase, |
| 180 | super::ShellPhase::Working | super::ShellPhase::Verifying |
| 181 | ) |
| 182 | && viewport.width >= super::AMBIENT_MIN_WIDTH |
| 183 | && viewport.height >= super::AMBIENT_MIN_HEIGHT; |
| 184 | let clock = Duration::from_millis((column.elapsed_ms % 1_000_000) as u64); |
| 185 | let band = (viewport.height / 2).clamp(4, 14); |
| 186 | const DITHER: [[u8; 4]; 4] = [[0, 8, 2, 10], [12, 4, 14, 6], [3, 11, 1, 9], [15, 7, 13, 5]]; |
| 187 | LIGHT.with_borrow_mut(|light| { |
| 188 | light.fit(viewport); |
| 189 | for y in area.top()..area.bottom() { |
| 190 | let shift = native_row_shift(column, y) |
| 191 | .map(|offset| (i32::from(offset) * i32::from(phase_strength) / 255) as i16); |
| 192 | let index_shift = (shift[0] + 2 * shift[1] + shift[2]) / 2; |
| 193 | let local_y = usize::from(y - viewport.y); |
| 194 | let surface_row = local_y * light.height / usize::from(viewport.height); |
| 195 | let submerged_rows = |
| 196 | (light.height - 1 - surface_row) * usize::from(column.session_depth) / (255 * 3); |
| 197 | let row = (surface_row + submerged_rows) * light.width; |
| 198 | for x in area.left()..area.right() { |
| 199 | let cell = &mut buf[(x, y)]; |
| 200 | if cell.bg != facts.ground |
| 201 | || cell.modifier.contains(Modifier::REVERSED) |
| 202 | || facts |
| 203 | .protected |
| 204 | .iter() |
| 205 | .any(|rect| rect.contains((x, y).into())) |
| 206 | { |
| 207 | continue; |
| 208 | } |
| 209 | let local_x = usize::from(x - viewport.x); |
| 210 | let col = local_x * light.width / usize::from(viewport.width); |
| 211 | let mut level = light.levels[row + col]; |
| 212 | if shimmer |
| 213 | && local_y < usize::from(band) |
| 214 | && (column.session_depth == u8::MAX || cell.symbol() == " ") |
| 215 | { |
| 216 | let glint = ocean_volumetric_caustic( |
| 217 | clock, |
| 218 | x - viewport.x, |
| 219 | y - viewport.y, |
| 220 | viewport.width, |
| 221 | band, |
| 222 | ); |
| 223 | level = level |
| 224 | .saturating_add((glint * f32::from(column.presence) * 0.034).round() as u8); |
| 225 | } |
| 226 | level = ((u16::from(level) * transmission + 127) / 255) as u8; |
| 227 | let shade = |value| { |
| 228 | if indexed { |
| 229 | indexed_water( |
| 230 | (i16::from(value) + index_shift).clamp(0, 255) as u8, |
| 231 | INDEXED_DITHER[local_y % 8][local_x % 8], |
| 232 | ) |
| 233 | } else { |
| 234 | let noise = (i16::from(DITHER[local_y % 4][local_x % 4]) * 2 - 15) / 15; |
| 235 | let water = WATER[(i16::from(value) + noise).clamp(0, 255) as usize]; |
| 236 | if let Color::Rgb(r, g, b) = water { |
| 237 | let adjust = |channel: u8, offset: i16| { |
| 238 | (i16::from(channel) + offset).clamp(0, 255) as u8 |
| 239 | }; |
| 240 | Color::Rgb( |
| 241 | adjust(r, shift[0]), |
| 242 | adjust(g, shift[1]), |
| 243 | adjust(b, shift[2]), |
| 244 | ) |
| 245 | } else { |
| 246 | water |
| 247 | } |
| 248 | } |
| 249 | }; |
| 250 | if column.session_depth == u8::MAX { |
| 251 | cell.set_bg(shade(level)); |
| 252 | } else if let Some(water) = water_for_ink(cell, level, shade, depth, ui) { |
| 253 | cell.set_bg(water); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | #[cfg(test)] |
| 261 | mod tests { |
| 262 | use super::*; |
| 263 | use ratatui::style::Style; |
| 264 | |
| 265 | fn idle(area: Rect) -> super::super::OceanColumn { |
| 266 | let ramp = |
| 267 | super::super::OceanRamp::for_theme(&codewhale_palette::UNDERWATER_UI_THEME).unwrap(); |
| 268 | super::super::OceanColumn::new( |
| 269 | ramp, |
| 270 | area, |
| 271 | 0, |
| 272 | None, |
| 273 | super::super::ShellPhase::Idle, |
| 274 | false, |
| 275 | 0, |
| 276 | 0, |
| 277 | ) |
| 278 | } |
| 279 | |
| 280 | #[test] |
| 281 | fn indexed_water_dithers_between_neighboring_dark_grays() { |
| 282 | for level in 0..=255 { |
| 283 | let shades = (0..64) |
| 284 | .map(|threshold| { |
| 285 | let Color::Indexed(shade) = indexed_water(level, threshold) else { |
| 286 | unreachable!() |
| 287 | }; |
| 288 | shade |
| 289 | }) |
| 290 | .collect::<Vec<_>>(); |
| 291 | assert!(shades.iter().all(|shade| (233..=237).contains(shade))); |
| 292 | assert!(shades[63] - shades[0] <= 1); |
| 293 | } |
| 294 | assert_eq!(indexed_water(0, 63), Color::Indexed(233)); |
| 295 | assert_eq!(indexed_water(255, 0), Color::Indexed(237)); |
| 296 | assert_ne!(indexed_water(100, 0), indexed_water(100, 63)); |
| 297 | let ui = codewhale_palette::UNDERWATER_UI_THEME; |
| 298 | let mut text = Cell::default(); |
| 299 | text.set_char('A'); |
| 300 | for ink in [ui.text_body, ui.text_hint, ui.text_dim] { |
| 301 | text.set_fg(ink); |
| 302 | for shade in 233..=237 { |
| 303 | let water = Color::Indexed(shade); |
| 304 | let projected = crate::tui::color_compat::project_ocean_ink( |
| 305 | &text, |
| 306 | water, |
| 307 | ColorDepth::Ansi256, |
| 308 | &ui, |
| 309 | ); |
| 310 | assert!( |
| 311 | codewhale_ratatui::color::relative_luminance(projected).unwrap() |
| 312 | > codewhale_ratatui::color::relative_luminance(water).unwrap() |
| 313 | ); |
| 314 | assert!(contrast_ratio(projected, water).unwrap() >= 3.0); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | #[test] |
| 320 | fn directional_painter_keeps_work_attention_failure_context_and_completion() { |
| 321 | use super::super::ShellPhase::{Approval, Done, Failed, Idle, Working}; |
| 322 | let area = Rect::new(0, 0, 106, 39); |
| 323 | let ui = codewhale_palette::UNDERWATER_UI_THEME; |
| 324 | for depth in [ColorDepth::TrueColor, ColorDepth::Ansi256] { |
| 325 | let caps = codewhale_ratatui::Caps { |
| 326 | depth, |
| 327 | ascii: false, |
| 328 | appearance: codewhale_ratatui::detect::Appearance::Unknown, |
| 329 | }; |
| 330 | let draw = |phase, clock, animate, context, completion| { |
| 331 | let mut column = idle(area) |
| 332 | .with_directional_light(true) |
| 333 | .with_paint_caps(Some(caps)); |
| 334 | column.phase = phase; |
| 335 | column.elapsed_ms = clock; |
| 336 | column.animated = animate; |
| 337 | column.presence = if animate { 1000 } else { 0 }; |
| 338 | column.context_percent = context; |
| 339 | column.completion_elapsed_ms = completion; |
| 340 | let mut buffer = Buffer::empty(area); |
| 341 | buffer.set_style(area, Style::default().bg(ui.surface_bg)); |
| 342 | column.paint_matching_native(area, &mut buffer, ui.surface_bg, &ui, &[]); |
| 343 | buffer |
| 344 | }; |
| 345 | let home = draw(Idle, 0, false, 0, None); |
| 346 | let work = draw(Working, 6_000, true, 0, None); |
| 347 | let still_work = draw(Working, 0, false, 0, None); |
| 348 | let approval = draw(Approval, 0, false, 0, None); |
| 349 | let failed = draw(Failed, 0, false, 0, None); |
| 350 | let at_depth = draw(Idle, 0, false, 90, None); |
| 351 | let settle = draw(Done, 0, true, 0, Some(320)); |
| 352 | for field in [&work, &still_work, &approval, &failed, &at_depth, &settle] { |
| 353 | assert_ne!( |
| 354 | field, &home, |
| 355 | "phase and context cues reach the painted grid at {depth:?}" |
| 356 | ); |
| 357 | } |
| 358 | assert_ne!(work, draw(Working, 9_000, true, 0, None)); |
| 359 | assert_ne!(approval, failed); |
| 360 | assert_eq!(approval, draw(Approval, 9_000, false, 0, None)); |
| 361 | assert_eq!(failed, draw(Failed, 9_000, false, 0, None)); |
| 362 | assert_eq!( |
| 363 | draw(Working, 0, false, 0, None), |
| 364 | draw(Working, 9_000, false, 0, None) |
| 365 | ); |
| 366 | assert_eq!(draw(Done, 0, false, 0, None), home); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | #[test] |
| 371 | fn a_bright_shaft_preserves_prose_ink_across_the_whole_line() { |
| 372 | let area = Rect::new(0, 0, 106, 39); |
| 373 | let ui = codewhale_palette::UNDERWATER_UI_THEME; |
| 374 | let facts = OceanPaintFacts::new(ui.surface_bg); |
| 375 | for depth in [ColorDepth::TrueColor, ColorDepth::Ansi256] { |
| 376 | let mut text = Buffer::empty(area); |
| 377 | text.set_style(area, Style::new().bg(ui.surface_bg)); |
| 378 | for x in 0..area.width { |
| 379 | text[(x, 1)] |
| 380 | .set_char('A') |
| 381 | .set_fg(codewhale_palette::TEXT_SOFT); |
| 382 | } |
| 383 | let project = |cell: &Cell| { |
| 384 | crate::tui::color_compat::project_ocean_ink(cell, cell.bg, depth, &ui) |
| 385 | }; |
| 386 | let intended = project(&text[(0, 1)]); |
| 387 | paint(idle(area), area, &mut text, depth, &ui, &facts); |
| 388 | for x in 0..area.width { |
| 389 | let cell = &text[(x, 1)]; |
| 390 | assert_eq!(project(cell), intended, "ink changed at x={x} on {depth:?}"); |
| 391 | assert!(contrast_ratio(intended, cell.bg).unwrap() >= 4.5); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | #[test] |
| 397 | fn settled_conversation_water_stays_continuous_under_prose_and_tool_output() { |
| 398 | let area = Rect::new(2, 3, 120, 35); |
| 399 | let ui = codewhale_palette::UNDERWATER_UI_THEME; |
| 400 | let facts = OceanPaintFacts::new(ui.surface_bg); |
| 401 | for depth in [ColorDepth::TrueColor, ColorDepth::Ansi256] { |
| 402 | let mut column = idle(area).with_session_depth(1.0); |
| 403 | column.animated = true; |
| 404 | column.presence = 1000; |
| 405 | column.phase = super::super::ShellPhase::Working; |
| 406 | column.elapsed_ms = 7_000; |
| 407 | let mut open = Buffer::empty(area); |
| 408 | open.set_style(area, Style::new().bg(ui.surface_bg)); |
| 409 | let mut text = open.clone(); |
| 410 | let inks = [ui.text_body, ui.text_muted, ui.text_hint, ui.warning]; |
| 411 | for y in area.top()..area.bottom() { |
| 412 | for x in area.left()..area.right() { |
| 413 | text[(x, y)] |
| 414 | .set_char('x') |
| 415 | .set_fg(inks[usize::from(x) % inks.len()]); |
| 416 | } |
| 417 | } |
| 418 | paint(column, area, &mut text, depth, &ui, &facts); |
| 419 | paint(column, area, &mut open, depth, &ui, &facts); |
| 420 | for (letter, water) in text.content.iter().zip(&open.content) { |
| 421 | assert_eq!(letter.bg, water.bg); |
| 422 | let ink = |
| 423 | crate::tui::color_compat::project_ocean_ink(letter, letter.bg, depth, &ui); |
| 424 | let ratio = contrast_ratio(ink, letter.bg).unwrap(); |
| 425 | assert!( |
| 426 | ratio >= 4.5, |
| 427 | "{depth:?}: {ink:?} on {:?} is {ratio}", |
| 428 | letter.bg |
| 429 | ); |
| 430 | } |
| 431 | assert!( |
| 432 | open.content |
| 433 | .iter() |
| 434 | .any(|cell| cell.bg != open.content[0].bg) |
| 435 | ); |
| 436 | let mut surf = Buffer::empty(area); |
| 437 | surf.set_style(area, Style::new().bg(ui.surface_bg)); |
| 438 | paint(idle(area), area, &mut surf, depth, &ui, &facts); |
| 439 | let peak = |buf: &Buffer| { |
| 440 | buf.content |
| 441 | .iter() |
| 442 | .filter_map(|cell| codewhale_ratatui::color::relative_luminance(cell.bg)) |
| 443 | .fold(0.0_f32, f32::max) |
| 444 | }; |
| 445 | assert!(peak(&open) < peak(&surf)); |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | #[test] |
| 450 | fn braille_uses_the_open_water_background_at_both_color_depths() { |
| 451 | let area = Rect::new(0, 0, 110, 43); |
| 452 | let at = (86, 1); |
| 453 | let ui = codewhale_palette::UNDERWATER_UI_THEME; |
| 454 | let facts = OceanPaintFacts::new(ui.surface_bg); |
| 455 | for depth in [ColorDepth::TrueColor, ColorDepth::Ansi256] { |
| 456 | let mut blank = Buffer::empty(area); |
| 457 | blank.set_style(area, Style::new().bg(ui.surface_bg)); |
| 458 | let mut logo = blank.clone(); |
| 459 | paint(idle(area), area, &mut blank, depth, &ui, &facts); |
| 460 | let water = blank[at].bg; |
| 461 | assert_ne!(water, ui.surface_bg); |
| 462 | logo[at].set_char('⠿').set_fg(water); |
| 463 | paint(idle(area), area, &mut logo, depth, &ui, &facts); |
| 464 | assert_eq!(logo[at].bg, water); |
| 465 | assert_eq!(logo[at].symbol(), "⠿"); |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 |