HSV wheel, real glow, fixed-length window
Browse files at this commit20ec2e759d33aa97cf19c37fbc22857f520c31edParent: 7ebfc44- feat
- HSV wheel, real glow, fixed-length window
3 changed files +154 -101
- crates/app/src/player/library.rs +4 -2
- crates/app/src/ui/dock_stance.rs +92 -36
- crates/engine/src/decode.rs +58 -63
crates/app/src/player/library.rsmodified+4 -2
| @@ -236,8 +236,10 @@ impl Player { | ||
| 236 | 236 | .unwrap_or(0) |
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - pub(crate) fn set_viz_hue(&mut self, hue: u8, cx: &mut Context<Self>) { | |
| 240 | - self.set_viz_paint_with(cx, |p| engine::decode::with_viz_paint_hue(p, hue)); | |
| 239 | + pub(crate) fn set_viz_hs(&mut self, hue: u8, sat: u8, cx: &mut Context<Self>) { | |
| 240 | + self.set_viz_paint_with(cx, |p| { | |
| 241 | + engine::decode::with_viz_paint_sat(engine::decode::with_viz_paint_hue(p, hue), sat) | |
| 242 | + }); | |
| 241 | 243 | } |
| 242 | 244 | |
| 243 | 245 | pub(crate) fn set_viz_sat(&mut self, sat: u8, cx: &mut Context<Self>) { |
crates/app/src/ui/dock_stance.rsmodified+92 -36
| @@ -15,7 +15,7 @@ use crate::ui::hitmap; | ||
| 15 | 15 | use crate::ui::stance::{Surface, is_focus_cycle_key, is_focus_exit_key, next_surface}; |
| 16 | 16 | use crate::ui::type_scale::{self, Typeset, head, label, mono}; |
| 17 | 17 | use crate::*; |
| 18 | -use gpui::FontWeight; | |
| 18 | +use gpui::{FontWeight, PathBuilder, point}; | |
| 19 | 19 | |
| 20 | 20 | /// Where the dock tab pick lives: one word beside the theme and the |
| 21 | 21 | /// keybindings (`ui::theme::config_path`/`save`/`load` is the exact pattern |
| @@ -1306,8 +1306,95 @@ fn viz_step( | ||
| 1306 | 1306 | .child(glyph) |
| 1307 | 1307 | } |
| 1308 | 1308 | |
| 1309 | +fn viz_color_wheel(paint: u32, cx: &mut Context<Player>) -> impl IntoElement { | |
| 1310 | + let hit = std::rc::Rc::new(std::cell::Cell::new(Bounds::default())); | |
| 1311 | + let hit_click = hit.clone(); | |
| 1312 | + div() | |
| 1313 | + .id("dock-viz-hs") | |
| 1314 | + .w(px(112.)) | |
| 1315 | + .h(px(112.)) | |
| 1316 | + .relative() | |
| 1317 | + .cursor_pointer() | |
| 1318 | + .children(hitmap::control("dock-viz-hs", "Colour wheel", true)) | |
| 1319 | + .child(bounds_probe(hit)) | |
| 1320 | + .child( | |
| 1321 | + canvas( | |
| 1322 | + |_, _, _| (), | |
| 1323 | + move |bounds, _, window, _| { | |
| 1324 | + let o = bounds.origin; | |
| 1325 | + let (w, h) = (f32::from(bounds.size.width), f32::from(bounds.size.height)); | |
| 1326 | + let cx = f32::from(o.x) + w / 2.; | |
| 1327 | + let cy = f32::from(o.y) + h / 2.; | |
| 1328 | + let radius = w.min(h) / 2. - 1.; | |
| 1329 | + let wedges = 48; | |
| 1330 | + for i in 0..wedges { | |
| 1331 | + let a0 = (i as f32 / wedges as f32) * std::f32::consts::TAU; | |
| 1332 | + let a1 = ((i + 1) as f32 / wedges as f32) * std::f32::consts::TAU; | |
| 1333 | + // 0 at top, clockwise: screen y down, so angle 0 is -Y. | |
| 1334 | + let hue = i as f32 / wedges as f32 * 360.; | |
| 1335 | + let (r, g, b) = engine::decode::viz_hsv_rgb(hue, 1., 0.84); | |
| 1336 | + let color = (u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b); | |
| 1337 | + let p0 = point(px(cx), px(cy)); | |
| 1338 | + let p1 = point( | |
| 1339 | + px(cx + radius * a0.sin()), | |
| 1340 | + px(cy - radius * a0.cos()), | |
| 1341 | + ); | |
| 1342 | + let p2 = point( | |
| 1343 | + px(cx + radius * a1.sin()), | |
| 1344 | + px(cy - radius * a1.cos()), | |
| 1345 | + ); | |
| 1346 | + let mut path = PathBuilder::fill(); | |
| 1347 | + path.add_polygon(&[p0, p1, p2], false); | |
| 1348 | + if let Ok(path) = path.build() { | |
| 1349 | + window.paint_path(path, rgb(color)); | |
| 1350 | + } | |
| 1351 | + } | |
| 1352 | + // Selection mark. | |
| 1353 | + let hue = f32::from(engine::decode::viz_hue_ui(paint)) / 256. * std::f32::consts::TAU; | |
| 1354 | + let sat = f32::from(engine::decode::viz_sat_ui(paint)) / 255.; | |
| 1355 | + let mx = cx + radius * sat * hue.sin(); | |
| 1356 | + let my = cy - radius * sat * hue.cos(); | |
| 1357 | + let mut mark = PathBuilder::stroke(px(2.)); | |
| 1358 | + let r = 4.; | |
| 1359 | + mark.add_polygon( | |
| 1360 | + &[ | |
| 1361 | + point(px(mx - r), px(my)), | |
| 1362 | + point(px(mx), px(my - r)), | |
| 1363 | + point(px(mx + r), px(my)), | |
| 1364 | + point(px(mx), px(my + r)), | |
| 1365 | + ], | |
| 1366 | + true, | |
| 1367 | + ); | |
| 1368 | + if let Ok(path) = mark.build() { | |
| 1369 | + window.paint_path(path, rgb(INK1())); | |
| 1370 | + } | |
| 1371 | + }, | |
| 1372 | + ) | |
| 1373 | + .size_full(), | |
| 1374 | + ) | |
| 1375 | + .on_mouse_down( | |
| 1376 | + MouseButton::Left, | |
| 1377 | + cx.listener(move |this, event: &MouseDownEvent, _, cx| { | |
| 1378 | + let b = hit_click.get(); | |
| 1379 | + let w = f32::from(b.size.width).max(1.); | |
| 1380 | + let h = f32::from(b.size.height).max(1.); | |
| 1381 | + let dx = f32::from(event.position.x - b.origin.x) - w / 2.; | |
| 1382 | + let dy = h / 2. - f32::from(event.position.y - b.origin.y); | |
| 1383 | + let radius = w.min(h) / 2.; | |
| 1384 | + let r = (dx * dx + dy * dy).sqrt() / radius; | |
| 1385 | + if r > 1.08 { | |
| 1386 | + return; | |
| 1387 | + } | |
| 1388 | + let sat = (r.clamp(0., 1.) * 255.).round() as u8; | |
| 1389 | + let ang = dx.atan2(dy).to_degrees(); | |
| 1390 | + let hue_deg = (ang + 360.) % 360.; | |
| 1391 | + let hue = (hue_deg / 360. * 256.).round() as u16 as u8; | |
| 1392 | + this.set_viz_hs(hue, sat.max(1), cx); | |
| 1393 | + }), | |
| 1394 | + ) | |
| 1395 | +} | |
| 1396 | + | |
| 1309 | 1397 | fn viz_paint_knobs(paint: u32, cx: &mut Context<Player>) -> impl IntoElement { |
| 1310 | - let hue = engine::decode::viz_hue_ui(paint); | |
| 1311 | 1398 | let sat = engine::decode::viz_sat_ui(paint); |
| 1312 | 1399 | let strands = engine::decode::viz_strands_of(paint); |
| 1313 | 1400 | let glow = engine::decode::viz_glow_of(paint); |
| @@ -1319,11 +1406,10 @@ fn viz_paint_knobs(paint: u32, cx: &mut Context<Player>) -> impl IntoElement { | ||
| 1319 | 1406 | .px(px(8.)) |
| 1320 | 1407 | .child( |
| 1321 | 1408 | div() |
| 1322 | - .id("dock-viz-hue") | |
| 1409 | + .id("dock-viz-wheel") | |
| 1323 | 1410 | .flex() |
| 1324 | - .items_center() | |
| 1411 | + .flex_col() | |
| 1325 | 1412 | .gap(px(4.)) |
| 1326 | - .h(px(CONTROL_H)) | |
| 1327 | 1413 | .child( |
| 1328 | 1414 | div() |
| 1329 | 1415 | .font(style.font.clone()) |
| @@ -1331,37 +1417,7 @@ fn viz_paint_knobs(paint: u32, cx: &mut Context<Player>) -> impl IntoElement { | ||
| 1331 | 1417 | .text_color(rgb(INK2())) |
| 1332 | 1418 | .child("Colour"), |
| 1333 | 1419 | ) |
| 1334 | - .child(viz_step("dock-viz-hue-minus", "−", cx, |this, cx| { | |
| 1335 | - this.set_viz_hue(engine::decode::viz_hue_ui(this.viz_paint()).wrapping_sub(4), cx); | |
| 1336 | - })) | |
| 1337 | - .child( | |
| 1338 | - div() | |
| 1339 | - .id("dock-viz-hue-bar") | |
| 1340 | - .flex_1() | |
| 1341 | - .flex() | |
| 1342 | - .h(px(14.)) | |
| 1343 | - .children((0u8..64).map(|i| { | |
| 1344 | - let h = i.saturating_mul(4); | |
| 1345 | - let (r, g, b) = engine::decode::viz_ink_from_paint( | |
| 1346 | - engine::decode::with_viz_paint_hue(paint, h), | |
| 1347 | - ); | |
| 1348 | - let color = (u32::from(r) << 16) | (u32::from(g) << 8) | u32::from(b); | |
| 1349 | - div() | |
| 1350 | - .id(("dock-viz-hue", u64::from(i))) | |
| 1351 | - .flex_1() | |
| 1352 | - .h_full() | |
| 1353 | - .bg(rgb(color)) | |
| 1354 | - .when(hue.abs_diff(h) < 3, |d| d.border_1().border_color(rgb(INK1()))) | |
| 1355 | - .cursor_pointer() | |
| 1356 | - .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | |
| 1357 | - this.set_viz_hue(h, cx); | |
| 1358 | - })) | |
| 1359 | - .into_any_element() | |
| 1360 | - })), | |
| 1361 | - ) | |
| 1362 | - .child(viz_step("dock-viz-hue-plus", "+", cx, |this, cx| { | |
| 1363 | - this.set_viz_hue(engine::decode::viz_hue_ui(this.viz_paint()).wrapping_add(4), cx); | |
| 1364 | - })), | |
| 1420 | + .child(viz_color_wheel(paint, cx)), | |
| 1365 | 1421 | ) |
| 1366 | 1422 | .child( |
| 1367 | 1423 | div() |
crates/engine/src/decode.rsmodified+58 -63
| @@ -1505,6 +1505,10 @@ pub fn viz_sat_ui(paint: u32) -> u8 { | ||
| 1505 | 1505 | } |
| 1506 | 1506 | } |
| 1507 | 1507 | |
| 1508 | +pub fn viz_hsv_rgb(h: f32, s: f32, v: f32) -> (u8, u8, u8) { | |
| 1509 | + hsv_to_rgb(h, s, v) | |
| 1510 | +} | |
| 1511 | + | |
| 1508 | 1512 | fn hsv_to_rgb(h: f32, s: f32, v: f32) -> (u8, u8, u8) { |
| 1509 | 1513 | let h = ((h % 360.0) + 360.0) % 360.0; |
| 1510 | 1514 | let c = v * s; |
| @@ -1595,28 +1599,6 @@ fn viz_span( | ||
| 1595 | 1599 | } |
| 1596 | 1600 | } |
| 1597 | 1601 | |
| 1598 | -fn viz_smooth(lo: &mut [f32], hi: &mut [f32]) { | |
| 1599 | - let n = lo.len(); | |
| 1600 | - if n < 5 { | |
| 1601 | - return; | |
| 1602 | - } | |
| 1603 | - let (src_lo, src_hi) = (lo.to_vec(), hi.to_vec()); | |
| 1604 | - for i in 2..n - 2 { | |
| 1605 | - lo[i] = (src_lo[i - 2] | |
| 1606 | - + src_lo[i - 1] * 2.0 | |
| 1607 | - + src_lo[i] * 3.0 | |
| 1608 | - + src_lo[i + 1] * 2.0 | |
| 1609 | - + src_lo[i + 2]) | |
| 1610 | - / 9.0; | |
| 1611 | - hi[i] = (src_hi[i - 2] | |
| 1612 | - + src_hi[i - 1] * 2.0 | |
| 1613 | - + src_hi[i] * 3.0 | |
| 1614 | - + src_hi[i + 1] * 2.0 | |
| 1615 | - + src_hi[i + 2]) | |
| 1616 | - / 9.0; | |
| 1617 | - } | |
| 1618 | -} | |
| 1619 | - | |
| 1620 | 1602 | fn viz_line( |
| 1621 | 1603 | y: &mut [u8], |
| 1622 | 1604 | u: &mut [u8], |
| @@ -1700,36 +1682,43 @@ pub(crate) fn visualizer_i420( | ||
| 1700 | 1682 | } else { |
| 1701 | 1683 | VIZ_WINDOW_SECS |
| 1702 | 1684 | }; |
| 1703 | - let first = (((t_secs - window / 2.0).max(0.0) * per_sec).floor() as usize) | |
| 1704 | - .min(peaks.len() - 1); | |
| 1705 | - let last = ((((t_secs + window / 2.0).min(duration) * per_sec).ceil() as usize) | |
| 1706 | - .clamp(first + 1, peaks.len())) | |
| 1707 | - .min(peaks.len()); | |
| 1708 | - let span = (last - first) as f64; | |
| 1709 | - let pair = |i: usize| peaks[i.min(peaks.len() - 1)]; | |
| 1710 | - let signed = |i: usize| { | |
| 1711 | - let (a, b) = pair(i); | |
| 1712 | - if b.abs() >= a.abs() { | |
| 1713 | - b | |
| 1714 | - } else { | |
| 1715 | - a | |
| 1685 | + // Fixed window length, silence outside the file: shrinking the span | |
| 1686 | + // to the samples that exist stretched a bump as it entered (2s) and | |
| 1687 | + // Fast (0.2s) rarely hit the edge. Column x always maps to | |
| 1688 | + // t - window/2 + x/w * window. | |
| 1689 | + let origin = t_secs - window / 2.0; | |
| 1690 | + let pair_at = |t: f64| -> (f32, f32) { | |
| 1691 | + if !t.is_finite() || t < 0.0 || t > duration { | |
| 1692 | + return (0.0, 0.0); | |
| 1716 | 1693 | } |
| 1694 | + let pos = (t * per_sec).clamp(0.0, (peaks.len() - 1) as f64); | |
| 1695 | + let i = pos.floor() as usize; | |
| 1696 | + let f = (pos - i as f64) as f32; | |
| 1697 | + let (lo0, hi0) = peaks[i]; | |
| 1698 | + let (lo1, hi1) = peaks[(i + 1).min(peaks.len() - 1)]; | |
| 1699 | + ( | |
| 1700 | + (lo0 + (lo1 - lo0) * f).clamp(-1.0, 0.0), | |
| 1701 | + (hi0 + (hi1 - hi0) * f).clamp(0.0, 1.0), | |
| 1702 | + ) | |
| 1703 | + }; | |
| 1704 | + let signed_at = |t: f64| { | |
| 1705 | + let (a, b) = pair_at(t); | |
| 1706 | + if b.abs() >= a.abs() { b } else { a } | |
| 1717 | 1707 | }; |
| 1718 | 1708 | for col in 0..w { |
| 1719 | - let a = first + (col as f64 * span / w as f64).floor() as usize; | |
| 1720 | - let b = (first + (((col + 1) as f64 * span / w as f64).ceil() as usize)).min(peaks.len()); | |
| 1721 | - let b = b.max(a + 1); | |
| 1709 | + let t0 = origin + col as f64 / w as f64 * window; | |
| 1710 | + let t1 = origin + (col + 1) as f64 / w as f64 * window; | |
| 1722 | 1711 | let (mut mn, mut mx) = (0.0f32, 0.0f32); |
| 1723 | - for &(l, h) in &peaks[a.min(peaks.len() - 1)..b.min(peaks.len()).max(a.min(peaks.len() - 1) + 1)] { | |
| 1712 | + let samples = 4; | |
| 1713 | + for k in 0..samples { | |
| 1714 | + let t = t0 + (t1 - t0) * (k as f64 + 0.5) / samples as f64; | |
| 1715 | + let (l, h) = pair_at(t); | |
| 1724 | 1716 | mn = mn.min(l); |
| 1725 | 1717 | mx = mx.max(h); |
| 1726 | 1718 | } |
| 1727 | - lo[col] = mn.clamp(-1.0, 0.0); | |
| 1728 | - hi[col] = mx.clamp(0.0, 1.0); | |
| 1729 | - let pos = first as f64 + (col as f64 + 0.5) * span / w as f64; | |
| 1730 | - let i = pos.floor() as usize; | |
| 1731 | - let f = (pos - i as f64) as f32; | |
| 1732 | - sig[col] = signed(i) + (signed(i.saturating_add(1)) - signed(i)) * f; | |
| 1719 | + lo[col] = mn; | |
| 1720 | + hi[col] = mx; | |
| 1721 | + sig[col] = signed_at(origin + (col as f64 + 0.5) / w as f64 * window); | |
| 1733 | 1722 | } |
| 1734 | 1723 | // Whole-file peak, not the window's: a 2s window that gains or loses |
| 1735 | 1724 | // a loud hit used to rescale every column, so the same bump changed |
| @@ -1745,8 +1734,6 @@ pub(crate) fn visualizer_i420( | ||
| 1745 | 1734 | sig[i] /= peak; |
| 1746 | 1735 | } |
| 1747 | 1736 | } |
| 1748 | - viz_smooth(&mut lo, &mut hi); | |
| 1749 | - viz_smooth(&mut lo, &mut hi); | |
| 1750 | 1737 | } |
| 1751 | 1738 | |
| 1752 | 1739 | match viz_style(flags) { |
| @@ -1761,18 +1748,22 @@ pub(crate) fn visualizer_i420( | ||
| 1761 | 1748 | let scale = 0.28 + 0.72 * frac; |
| 1762 | 1749 | let wave = (x_phase + travel + s as f64 * 0.05).sin() as f32; |
| 1763 | 1750 | let row = (center + half * env * scale * wave).round() as i32; |
| 1751 | + let glow = i32::from(viz_glow_of(paint)).max(1); | |
| 1764 | 1752 | viz_stamp( |
| 1765 | 1753 | &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row, 1.0, bg_y, ink_y, |
| 1766 | 1754 | ink_u, ink_v, |
| 1767 | 1755 | ); |
| 1768 | - viz_stamp( | |
| 1769 | - &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row - 1, 0.35, bg_y, | |
| 1770 | - ink_y, ink_u, ink_v, | |
| 1771 | - ); | |
| 1772 | - viz_stamp( | |
| 1773 | - &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row + 1, 0.35, bg_y, | |
| 1774 | - ink_y, ink_u, ink_v, | |
| 1775 | - ); | |
| 1756 | + for dy in 1..=glow { | |
| 1757 | + let fall = 0.5 / dy as f32; | |
| 1758 | + viz_stamp( | |
| 1759 | + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row - dy, fall, bg_y, | |
| 1760 | + ink_y, ink_u, ink_v, | |
| 1761 | + ); | |
| 1762 | + viz_stamp( | |
| 1763 | + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, row + dy, fall, bg_y, | |
| 1764 | + ink_y, ink_u, ink_v, | |
| 1765 | + ); | |
| 1766 | + } | |
| 1776 | 1767 | } |
| 1777 | 1768 | } |
| 1778 | 1769 | } |
| @@ -1828,6 +1819,7 @@ pub(crate) fn visualizer_i420( | ||
| 1828 | 1819 | _ => { |
| 1829 | 1820 | // Timeline envelope: one filled body, tops then bottoms, after |
| 1830 | 1821 | // the fold+smooth above. Default look. |
| 1822 | + let glow = i32::from(viz_glow_of(paint)).max(1); | |
| 1831 | 1823 | for col in 0..w { |
| 1832 | 1824 | let top = (center - hi[col] * half).round() as i32; |
| 1833 | 1825 | let bot = (center - lo[col] * half).round() as i32; |
| @@ -1835,14 +1827,17 @@ pub(crate) fn visualizer_i420( | ||
| 1835 | 1827 | &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top, bot, bg_y, ink_y, ink_u, |
| 1836 | 1828 | ink_v, |
| 1837 | 1829 | ); |
| 1838 | - viz_stamp( | |
| 1839 | - &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top - 1, 0.4, bg_y, ink_y, | |
| 1840 | - ink_u, ink_v, | |
| 1841 | - ); | |
| 1842 | - viz_stamp( | |
| 1843 | - &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, bot + 1, 0.4, bg_y, ink_y, | |
| 1844 | - ink_u, ink_v, | |
| 1845 | - ); | |
| 1830 | + for dy in 1..=glow { | |
| 1831 | + let fall = 0.55 / dy as f32; | |
| 1832 | + viz_stamp( | |
| 1833 | + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, top - dy, fall, bg_y, | |
| 1834 | + ink_y, ink_u, ink_v, | |
| 1835 | + ); | |
| 1836 | + viz_stamp( | |
| 1837 | + &mut y, &mut u, &mut v, w, h, cw, ch, col as i32, bot + dy, fall, bg_y, | |
| 1838 | + ink_y, ink_u, ink_v, | |
| 1839 | + ); | |
| 1840 | + } | |
| 1846 | 1841 | } |
| 1847 | 1842 | } |
| 1848 | 1843 | } |