feat(slint): immersive Spectrum bar colors derived from album art (negative fallback)
The bars used a fixed cyan->purple default. Now they take contrast colors from the album artwork, with a negative-fallback for dark covers (a near-black Metallica album would vanish against the black background -> invert so the bars pop). - immersive.rs: spectrum_colors() extracts the most saturated sample as primary + a hue-distinct secondary (channel-rotated if none distinct); if the cover is too dark/desaturated it uses the NEGATIVE of the average. ensure_visible_on_black() inverts a too-dark color and lifts a dim one so both stops read on black. - ImmersiveState gains spectrum-primary / spectrum-secondary (defaults cyan->purple until art resolves); set in playback.rs from the same decoded artwork as glow-color / bg-image. - SpectrumPanel binds the bar gradient to those (base = primary, tip = secondary). cc
This commit is contained in:
4 files changed
+104
-3
No files matched your search
@@ -53,6 +53,97 @@ pub fn glow_color(pixels: &[u8], width: u32, height: u32) -> Color {
|
||||
Color::from_argb_u8(0x59, best.0, best.1, best.2)
|
||||
}
|
||||
|
||||
/// Two vivid bar colors for the Spectrum visualizer, derived from the artwork
|
||||
/// and guaranteed to read against a BLACK background. The bars sit on solid
|
||||
/// black, so a dark/desaturated cover (e.g. a near-black Metallica album) would
|
||||
/// vanish; in that case we fall back to the NEGATIVE (inverted) color so the
|
||||
/// bars pop. Returns (primary at the base, secondary at the tip).
|
||||
pub fn spectrum_colors(pixels: &[u8], width: u32, height: u32) -> (Color, Color) {
|
||||
let default = (
|
||||
Color::from_rgb_u8(0, 220, 200),
|
||||
Color::from_rgb_u8(150, 50, 255),
|
||||
);
|
||||
let Some(src) = RgbaImage::from_raw(width, height, pixels.to_vec()) else {
|
||||
return default;
|
||||
};
|
||||
let tiny = imageops::resize(&src, 8, 8, imageops::FilterType::Triangle);
|
||||
|
||||
// HSL-ish saturation (same measure as glow_color).
|
||||
let sat = |r: f32, g: f32, b: f32| -> f32 {
|
||||
let max = r.max(g).max(b);
|
||||
let min = r.min(g).min(b);
|
||||
if (max - min).abs() < f32::EPSILON {
|
||||
0.0
|
||||
} else if (max + min) / 2.0 > 127.0 {
|
||||
(max - min) / (510.0 - max - min).max(1.0)
|
||||
} else {
|
||||
(max - min) / (max + min).max(1.0)
|
||||
}
|
||||
};
|
||||
|
||||
let mut cands: Vec<(f32, (u8, u8, u8))> = Vec::new();
|
||||
let (mut ar, mut ag, mut ab, mut n) = (0.0f32, 0.0f32, 0.0f32, 0.0f32);
|
||||
for px in tiny.pixels() {
|
||||
let (r, g, b) = (px[0] as f32, px[1] as f32, px[2] as f32);
|
||||
ar += r;
|
||||
ag += g;
|
||||
ab += b;
|
||||
n += 1.0;
|
||||
cands.push((sat(r, g, b), (px[0], px[1], px[2])));
|
||||
}
|
||||
cands.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));
|
||||
let n = n.max(1.0);
|
||||
let avg = ((ar / n) as u8, (ag / n) as u8, (ab / n) as u8);
|
||||
|
||||
let hue_diff = |a: (u8, u8, u8), b: (u8, u8, u8)| -> i32 {
|
||||
(a.0 as i32 - b.0 as i32).abs()
|
||||
+ (a.1 as i32 - b.1 as i32).abs()
|
||||
+ (a.2 as i32 - b.2 as i32).abs()
|
||||
};
|
||||
|
||||
// Primary = most saturated usable sample; else the NEGATIVE of the average
|
||||
// (handles black/grey covers -> bright, visible bars).
|
||||
let top = cands.first().copied().unwrap_or((0.0, avg));
|
||||
let (primary, secondary) = if top.0 > 0.12 {
|
||||
let prim = top.1;
|
||||
let sec = cands
|
||||
.iter()
|
||||
.skip(1)
|
||||
.find(|c| hue_diff(c.1, prim) > 90)
|
||||
.map(|c| c.1)
|
||||
.unwrap_or((prim.1, prim.2, prim.0)); // channel-rotate for a distinct stop
|
||||
(prim, sec)
|
||||
} else {
|
||||
let neg = (255 - avg.0, 255 - avg.1, 255 - avg.2);
|
||||
(neg, (neg.2, neg.0, neg.1))
|
||||
};
|
||||
|
||||
(
|
||||
ensure_visible_on_black(primary),
|
||||
ensure_visible_on_black(secondary),
|
||||
)
|
||||
}
|
||||
|
||||
/// Guarantee a color reads against black: if it is too dark, invert it
|
||||
/// (negative); if it is still dim, lift it toward a visible brightness.
|
||||
fn ensure_visible_on_black((r, g, b): (u8, u8, u8)) -> Color {
|
||||
let luma = |r: u8, g: u8, b: u8| 0.299 * r as f32 + 0.587 * g as f32 + 0.114 * b as f32;
|
||||
let (mut r, mut g, mut b) = (r, g, b);
|
||||
if luma(r, g, b) < 70.0 {
|
||||
r = 255 - r;
|
||||
g = 255 - g;
|
||||
b = 255 - b;
|
||||
}
|
||||
let l = luma(r, g, b);
|
||||
if l > 0.0 && l < 110.0 {
|
||||
let k = 110.0 / l;
|
||||
r = (r as f32 * k).min(255.0) as u8;
|
||||
g = (g as f32 * k).min(255.0) as u8;
|
||||
b = (b as f32 * k).min(255.0) as u8;
|
||||
}
|
||||
Color::from_rgb_u8(r, g, b)
|
||||
}
|
||||
|
||||
fn color_adjust(mut img: RgbaImage) -> RgbaImage {
|
||||
let mut min_r = 255.0f32;
|
||||
let mut min_g = 255.0f32;
|
||||
|
||||
@@ -1199,6 +1199,11 @@ fn load_now_playing_artwork_large(weak: slint::Weak<AppWindow>, art: qbz_models:
|
||||
}
|
||||
win.global::<ImmersiveState>()
|
||||
.set_glow_color(crate::immersive::glow_color(&pixels, w, h));
|
||||
let (spec_primary, spec_secondary) =
|
||||
crate::immersive::spectrum_colors(&pixels, w, h);
|
||||
win.global::<ImmersiveState>().set_spectrum_primary(spec_primary);
|
||||
win.global::<ImmersiveState>()
|
||||
.set_spectrum_secondary(spec_secondary);
|
||||
win.global::<NowPlayingState>().set_artwork_large(img);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -53,7 +53,7 @@
|
||||
// "Recursion detected" panic.
|
||||
|
||||
import { QualityBadgeFull } from "../primitives/QualityBadgeFull.slint";
|
||||
import { NowPlayingState, VisualizerState } from "../state.slint";
|
||||
import { NowPlayingState, VisualizerState, ImmersiveState } from "../state.slint";
|
||||
|
||||
// One mirrored pair of bars for a single active source bin. Draws the LEFT
|
||||
// column at `x-left` and the RIGHT (mirror) column at `x-right`, both from the
|
||||
@@ -94,7 +94,7 @@ component BarPair inherits Rectangle {
|
||||
y: root.base-y - self.height;
|
||||
// cyan at base -> purple at the tip (Svelte default). If it looks
|
||||
// inverted, swap the two stops.
|
||||
background: @linear-gradient(0deg, #00dcc8 0%, #9632ff 100%);
|
||||
background: @linear-gradient(0deg, ImmersiveState.spectrum-primary 0%, ImmersiveState.spectrum-secondary 100%);
|
||||
}
|
||||
|
||||
// RIGHT (mirror) bar — same source bin.
|
||||
@@ -103,7 +103,7 @@ component BarPair inherits Rectangle {
|
||||
width: root.bar-width;
|
||||
height: root.bar-height;
|
||||
y: root.base-y - self.height;
|
||||
background: @linear-gradient(0deg, #00dcc8 0%, #9632ff 100%);
|
||||
background: @linear-gradient(0deg, ImmersiveState.spectrum-primary 0%, ImmersiveState.spectrum-secondary 100%);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2972,6 +2972,11 @@ export global ImmersiveState {
|
||||
// does NOT match Tauri's focus-tab string order (coverflow first). Product
|
||||
// default + persistence ("remember last view") is a later (header) session.
|
||||
in-out property <int> mode: 0;
|
||||
// Spectrum bar gradient, derived from the album art (contrast colors;
|
||||
// negative fallback for dark covers so the bars read against the black bg).
|
||||
// Defaults = the Svelte cyan -> purple until the art resolves.
|
||||
in property <color> spectrum-primary: #00dcc8;
|
||||
in property <color> spectrum-secondary: #9632ff;
|
||||
}
|
||||
|
||||
// Now-playing / transport state. The Rust playback poll loop pushes the
|
||||
|
||||
Reference in new issue
Block a user