diff --git a/crates/app/src/player/library.rs b/crates/app/src/player/library.rs index a447aa8..a326c91 100644 --- a/crates/app/src/player/library.rs +++ b/crates/app/src/player/library.rs @@ -236,8 +236,10 @@ impl Player { .unwrap_or(0) } - pub(crate) fn set_viz_hue(&mut self, hue: u8, cx: &mut Context) { - self.set_viz_paint_with(cx, |p| engine::decode::with_viz_paint_hue(p, hue)); + pub(crate) fn set_viz_hs(&mut self, hue: u8, sat: u8, cx: &mut Context) { + self.set_viz_paint_with(cx, |p| { + engine::decode::with_viz_paint_sat(engine::decode::with_viz_paint_hue(p, hue), sat) + }); } pub(crate) fn set_viz_sat(&mut self, sat: u8, cx: &mut Context) { diff --git a/crates/app/src/ui/dock_stance.rs b/crates/app/src/ui/dock_stance.rs index 394a7dc..eb1ce3d 100644 --- a/crates/app/src/ui/dock_stance.rs +++ b/crates/app/src/ui/dock_stance.rs @@ -15,7 +15,7 @@ use crate::ui::hitmap; use crate::ui::stance::{Surface, is_focus_cycle_key, is_focus_exit_key, next_surface}; use crate::ui::type_scale::{self, Typeset, head, label, mono}; use crate::*; -use gpui::FontWeight; +use gpui::{FontWeight, PathBuilder, point}; /// Where the dock tab pick lives: one word beside the theme and the /// keybindings (`ui::theme::config_path`/`save`/`load` is the exact pattern @@ -1306,8 +1306,95 @@ fn viz_step( .child(glyph) } +fn viz_color_wheel(paint: u32, cx: &mut Context) -> impl IntoElement { + let hit = std::rc::Rc::new(std::cell::Cell::new(Bounds::default())); + let hit_click = hit.clone(); + div() + .id("dock-viz-hs") + .w(px(112.)) + .h(px(112.)) + .relative() + .cursor_pointer() + .children(hitmap::control("dock-viz-hs", "Colour wheel", true)) + .child(bounds_probe(hit)) + .child( + canvas( + |_, _, _| (), + move |bounds, _, window, _| { + let o = bounds.origin; + let (w, h) = (f32::from(bounds.size.width), f32::from(bounds.size.height)); + let cx = f32::from(o.x) + w / 2.; + let cy = f32::from(o.y) + h / 2.; + let radius = w.min(h) / 2. - 1.; + let wedges = 48; + for i in 0..wedges { + let a0 = (i as f32 / wedges as f32) * std::f32::consts::TAU; + let a1 = ((i + 1) as f32 / wedges as f32) * std::f32::consts::TAU; + // 0 at top, clockwise: screen y down, so angle 0 is -Y. + let hue = i as f32 / wedges as f32 * 360.; + let (r, g, b) = engine::decode::viz_hsv_rgb(hue, 1., 0.84); + let color = (u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b); + let p0 = point(px(cx), px(cy)); + let p1 = point( + px(cx + radius * a0.sin()), + px(cy - radius * a0.cos()), + ); + let p2 = point( + px(cx + radius * a1.sin()), + px(cy - radius * a1.cos()), + ); + let mut path = PathBuilder::fill(); + path.add_polygon(&[p0, p1, p2], false); + if let Ok(path) = path.build() { + window.paint_path(path, rgb(color)); + } + } + // Selection mark. + let hue = f32::from(engine::decode::viz_hue_ui(paint)) / 256. * std::f32::consts::TAU; + let sat = f32::from(engine::decode::viz_sat_ui(paint)) / 255.; + let mx = cx + radius * sat * hue.sin(); + let my = cy - radius * sat * hue.cos(); + let mut mark = PathBuilder::stroke(px(2.)); + let r = 4.; + mark.add_polygon( + &[ + point(px(mx - r), px(my)), + point(px(mx), px(my - r)), + point(px(mx + r), px(my)), + point(px(mx), px(my + r)), + ], + true, + ); + if let Ok(path) = mark.build() { + window.paint_path(path, rgb(INK1())); + } + }, + ) + .size_full(), + ) + .on_mouse_down( + MouseButton::Left, + cx.listener(move |this, event: &MouseDownEvent, _, cx| { + let b = hit_click.get(); + let w = f32::from(b.size.width).max(1.); + let h = f32::from(b.size.height).max(1.); + let dx = f32::from(event.position.x - b.origin.x) - w / 2.; + let dy = h / 2. - f32::from(event.position.y - b.origin.y); + let radius = w.min(h) / 2.; + let r = (dx * dx + dy * dy).sqrt() / radius; + if r > 1.08 { + return; + } + let sat = (r.clamp(0., 1.) * 255.).round() as u8; + let ang = dx.atan2(dy).to_degrees(); + let hue_deg = (ang + 360.) % 360.; + let hue = (hue_deg / 360. * 256.).round() as u16 as u8; + this.set_viz_hs(hue, sat.max(1), cx); + }), + ) +} + fn viz_paint_knobs(paint: u32, cx: &mut Context) -> impl IntoElement { - let hue = engine::decode::viz_hue_ui(paint); let sat = engine::decode::viz_sat_ui(paint); let strands = engine::decode::viz_strands_of(paint); let glow = engine::decode::viz_glow_of(paint); @@ -1319,11 +1406,10 @@ fn viz_paint_knobs(paint: u32, cx: &mut Context) -> impl IntoElement { .px(px(8.)) .child( div() - .id("dock-viz-hue") + .id("dock-viz-wheel") .flex() - .items_center() + .flex_col() .gap(px(4.)) - .h(px(CONTROL_H)) .child( div() .font(style.font.clone()) @@ -1331,37 +1417,7 @@ fn viz_paint_knobs(paint: u32, cx: &mut Context) -> impl IntoElement { .text_color(rgb(INK2())) .child("Colour"), ) - .child(viz_step("dock-viz-hue-minus", "−", cx, |this, cx| { - this.set_viz_hue(engine::decode::viz_hue_ui(this.viz_paint()).wrapping_sub(4), cx); - })) - .child( - div() - .id("dock-viz-hue-bar") - .flex_1() - .flex() - .h(px(14.)) - .children((0u8..64).map(|i| { - let h = i.saturating_mul(4); - let (r, g, b) = engine::decode::viz_ink_from_paint( - engine::decode::with_viz_paint_hue(paint, h), - ); - let color = (u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b); - div() - .id(("dock-viz-hue", u64::from(i))) - .flex_1() - .h_full() - .bg(rgb(color)) - .when(hue.abs_diff(h) < 3, |d| d.border_1().border_color(rgb(INK1()))) - .cursor_pointer() - .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { - this.set_viz_hue(h, cx); - })) - .into_any_element() - })), - ) - .child(viz_step("dock-viz-hue-plus", "+", cx, |this, cx| { - this.set_viz_hue(engine::decode::viz_hue_ui(this.viz_paint()).wrapping_add(4), cx); - })), + .child(viz_color_wheel(paint, cx)), ) .child( div() diff --git a/crates/engine/src/decode.rs b/crates/engine/src/decode.rs index ea0dc3b..a4aff5b 100644 --- a/crates/engine/src/decode.rs +++ b/crates/engine/src/decode.rs @@ -1505,6 +1505,10 @@ pub fn viz_sat_ui(paint: u32) -> u8 { } } +pub fn viz_hsv_rgb(h: f32, s: f32, v: f32) -> (u8, u8, u8) { + hsv_to_rgb(h, s, v) +} + fn hsv_to_rgb(h: f32, s: f32, v: f32) -> (u8, u8, u8) { let h = ((h % 360.0) + 360.0) % 360.0; let c = v * s; @@ -1595,28 +1599,6 @@ fn viz_span( } } -fn viz_smooth(lo: &mut [f32], hi: &mut [f32]) { - let n = lo.len(); - if n < 5 { - return; - } - let (src_lo, src_hi) = (lo.to_vec(), hi.to_vec()); - for i in 2..n - 2 { - lo[i] = (src_lo[i - 2] - + src_lo[i - 1] * 2.0 - + src_lo[i] * 3.0 - + src_lo[i + 1] * 2.0 - + src_lo[i + 2]) - / 9.0; - hi[i] = (src_hi[i - 2] - + src_hi[i - 1] * 2.0 - + src_hi[i] * 3.0 - + src_hi[i + 1] * 2.0 - + src_hi[i + 2]) - / 9.0; - } -} - fn viz_line( y: &mut [u8], u: &mut [u8], @@ -1700,36 +1682,43 @@ pub(crate) fn visualizer_i420( } else { VIZ_WINDOW_SECS }; - let first = (((t_secs - window / 2.0).max(0.0) * per_sec).floor() as usize) - .min(peaks.len() - 1); - let last = ((((t_secs + window / 2.0).min(duration) * per_sec).ceil() as usize) - .clamp(first + 1, peaks.len())) - .min(peaks.len()); - let span = (last - first) as f64; - let pair = |i: usize| peaks[i.min(peaks.len() - 1)]; - let signed = |i: usize| { - let (a, b) = pair(i); - if b.abs() >= a.abs() { - b - } else { - a + // Fixed window length, silence outside the file: shrinking the span + // to the samples that exist stretched a bump as it entered (2s) and + // Fast (0.2s) rarely hit the edge. Column x always maps to + // t - window/2 + x/w * window. + let origin = t_secs - window / 2.0; + let pair_at = |t: f64| -> (f32, f32) { + if !t.is_finite() || t < 0.0 || t > duration { + return (0.0, 0.0); } + let pos = (t * per_sec).clamp(0.0, (peaks.len() - 1) as f64); + let i = pos.floor() as usize; + let f = (pos - i as f64) as f32; + let (lo0, hi0) = peaks[i]; + let (lo1, hi1) = peaks[(i + 1).min(peaks.len() - 1)]; + ( + (lo0 + (lo1 - lo0) * f).clamp(-1.0, 0.0), + (hi0 + (hi1 - hi0) * f).clamp(0.0, 1.0), + ) + }; + let signed_at = |t: f64| { + let (a, b) = pair_at(t); + if b.abs() >= a.abs() { b } else { a } }; for col in 0..w { - let a = first + (col as f64 * span / w as f64).floor() as usize; - let b = (first + (((col + 1) as f64 * span / w as f64).ceil() as usize)).min(peaks.len()); - let b = b.max(a + 1); + let t0 = origin + col as f64 / w as f64 * window; + let t1 = origin + (col + 1) as f64 / w as f64 * window; let (mut mn, mut mx) = (0.0f32, 0.0f32); - for &(l, h) in &peaks[a.min(peaks.len() - 1)..b.min(peaks.len()).max(a.min(peaks.len() - 1) + 1)] { + let samples = 4; + for k in 0..samples { + let t = t0 + (t1 - t0) * (k as f64 + 0.5) / samples as f64; + let (l, h) = pair_at(t); mn = mn.min(l); mx = mx.max(h); } - lo[col] = mn.clamp(-1.0, 0.0); - hi[col] = mx.clamp(0.0, 1.0); - let pos = first as f64 + (col as f64 + 0.5) * span / w as f64; - let i = pos.floor() as usize; - let f = (pos - i as f64) as f32; - sig[col] = signed(i) + (signed(i.saturating_add(1)) - signed(i)) * f; + lo[col] = mn; + hi[col] = mx; + sig[col] = signed_at(origin + (col as f64 + 0.5) / w as f64 * window); } // Whole-file peak, not the window's: a 2s window that gains or loses // a loud hit used to rescale every column, so the same bump changed @@ -1745,8 +1734,6 @@ pub(crate) fn visualizer_i420( sig[i] /= peak; } } - viz_smooth(&mut lo, &mut hi); - viz_smooth(&mut lo, &mut hi); } match viz_style(flags) { @@ -1761,18 +1748,22 @@ pub(crate) fn visualizer_i420( let scale = 0.28 + 0.72 * frac; let wave = (x_phase + travel + s as f64 * 0.05).sin() as f32; let row = (center + half * env * scale * wave).round() as i32; + let glow = i32::from(viz_glow_of(paint)).max(1); viz_stamp( &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row, 1.0, bg_y, ink_y, ink_u, ink_v, ); - viz_stamp( - &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row - 1, 0.35, bg_y, - ink_y, ink_u, ink_v, - ); - viz_stamp( - &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row + 1, 0.35, bg_y, - ink_y, ink_u, ink_v, - ); + for dy in 1..=glow { + let fall = 0.5 / dy as f32; + viz_stamp( + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row - dy, fall, bg_y, + ink_y, ink_u, ink_v, + ); + viz_stamp( + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row + dy, fall, bg_y, + ink_y, ink_u, ink_v, + ); + } } } } @@ -1828,6 +1819,7 @@ pub(crate) fn visualizer_i420( _ => { // Timeline envelope: one filled body, tops then bottoms, after // the fold+smooth above. Default look. + let glow = i32::from(viz_glow_of(paint)).max(1); for col in 0..w { let top = (center - hi[col] * half).round() as i32; let bot = (center - lo[col] * half).round() as i32; @@ -1835,14 +1827,17 @@ pub(crate) fn visualizer_i420( &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top, bot, bg_y, ink_y, ink_u, ink_v, ); - viz_stamp( - &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top - 1, 0.4, bg_y, ink_y, - ink_u, ink_v, - ); - viz_stamp( - &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, bot + 1, 0.4, bg_y, ink_y, - ink_u, ink_v, - ); + for dy in 1..=glow { + let fall = 0.55 / dy as f32; + viz_stamp( + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top - dy, fall, bg_y, + ink_y, ink_u, ink_v, + ); + viz_stamp( + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, bot + dy, fall, bg_y, + ink_y, ink_u, ink_v, + ); + } } } }