use crate::clap::plugin::NamClapMainThread;
use crate::clap::plugin::debug_assert_main_thread;
use clack_extensions::track_info::{PluginTrackInfo, PluginTrackInfoImpl};
use std::sync::atomic::Ordering;
impl<'a> PluginTrackInfoImpl for NamClapMainThread<'a> {
fn changed(&self) {
debug_assert_main_thread(&self.host);
if let Some(track_info_ext) = self
.host
.get_extension::<clack_extensions::track_info::HostTrackInfo>()
{
let mut buffer = clack_extensions::track_info::TrackInfoBuffer::new();
let mut host_mut = unsafe { self.host.with_arbitrary_lifetime() };
if let Some(info) = track_info_ext.get(&mut host_mut, &mut buffer) {
if let Some(color) = info.color() {
let packed = pack_argb(color.alpha, color.red, color.green, color.blue);
self.shared
.cold
.track_accent_color
.store(packed, Ordering::Relaxed);
} else {
self.shared
.cold
.track_accent_color
.store(0, Ordering::Relaxed);
}
}
}
}
}
pub fn pack_argb(alpha: u8, red: u8, green: u8, blue: u8) -> u32 {
((alpha as u32) << 24) | ((red as u32) << 16) | ((green as u32) << 8) | (blue as u32)
}
pub type NamPluginTrackInfo = PluginTrackInfo;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pack_argb_representative_colors() {
assert_eq!(pack_argb(0xFF, 0xFF, 0xFF, 0xFF), 0xFFFF_FFFF);
assert_eq!(pack_argb(0xFF, 0x00, 0x00, 0x00), 0xFF00_0000);
assert_eq!(pack_argb(0xFF, 0xFF, 0x00, 0x00), 0xFFFF_0000);
assert_eq!(pack_argb(0xFF, 0x00, 0xFF, 0x00), 0xFF00_FF00);
assert_eq!(pack_argb(0xFF, 0x5E, 0x81, 0xAC), 0xFF5E_81AC);
}
#[test]
fn test_pack_argb_zero_is_sentinel() {
assert_eq!(pack_argb(0, 0, 0, 0), 0);
}
#[test]
fn test_pack_argb_roundtrip() {
let colors = [
(0xFFu8, 0x00u8, 0xD4u8, 0xAAu8), (0xFF, 0x5E, 0x81, 0xAC), (0xFF, 0xF5, 0xA6, 0x23), ];
for (alpha, red, green, blue) in colors {
let packed = pack_argb(alpha, red, green, blue);
assert_eq!((packed >> 24) as u8, alpha, "alpha mismatch");
assert_eq!(((packed >> 16) & 0xFF) as u8, red, "red mismatch");
assert_eq!(((packed >> 8) & 0xFF) as u8, green, "green mismatch");
assert_eq!((packed & 0xFF) as u8, blue, "blue mismatch");
}
}
}