use super::*;
#[must_use]
pub fn concentric_radius(outer: f32, inset: f32) -> f32 {
if outer >= RADIUS_PILL {
RADIUS_PILL
} else {
(outer - inset).max(2.0)
}
}
pub(crate) const fn srgb_channel_to_linear(channel: u8) -> f32 {
let char_item: f32 = channel as f32 / 255.0;
if char_item <= 0.04045 {
return char_item / 12.92;
}
let pos_x: f32 = (char_item + 0.055) / 1.055;
let x2: f32 = pos_x * pos_x;
let mut pos_y: f32 = 0.75;
let mut step: usize = 0;
while step < 12 {
let y4: f32 = pos_y * pos_y * pos_y * pos_y;
pos_y = (4.0 * pos_y + x2 / y4) * 0.2;
step += 1;
}
x2 * pos_y
}
#[must_use]
pub const fn relative_luminance_rgb(red_val: u8, green_val: u8, blue_val: u8) -> f32 {
0.2126 * srgb_channel_to_linear(red_val)
+ 0.7152 * srgb_channel_to_linear(green_val)
+ 0.0722 * srgb_channel_to_linear(blue_val)
}
#[must_use]
pub const fn contrast_ratio_hex(lhs: u32, rhs: u32) -> f32 {
let l1: f32 = relative_luminance_rgb(
((lhs >> 16) & 0xFF) as u8,
((lhs >> 8) & 0xFF) as u8,
(lhs & 0xFF) as u8,
);
let l2: f32 = relative_luminance_rgb(
((rhs >> 16) & 0xFF) as u8,
((rhs >> 8) & 0xFF) as u8,
(rhs & 0xFF) as u8,
);
if l1 >= l2 {
(l1 + 0.05) / (l2 + 0.05)
} else {
(l2 + 0.05) / (l1 + 0.05)
}
}
#[must_use]
pub fn relative_luminance(color: Color) -> f32 {
relative_luminance_rgb(color.get_red(), color.get_green(), color.get_blue())
}
#[must_use]
pub fn blend_over(fg: Color, bg: Color) -> Color {
let alpha_val: f32 = fg.get_alpha() as f32 / 255.0;
let inv_a: f32 = 1.0 - alpha_val;
let red_val: u8 = (fg.get_red() as f32 * alpha_val + bg.get_red() as f32 * inv_a)
.round()
.clamp(0.0, 255.0) as u8;
let green_val: u8 = (fg.get_green() as f32 * alpha_val + bg.get_green() as f32 * inv_a)
.round()
.clamp(0.0, 255.0) as u8;
let blue_val: u8 = (fg.get_blue() as f32 * alpha_val + bg.get_blue() as f32 * inv_a)
.round()
.clamp(0.0, 255.0) as u8;
Color::rgba(red_val, green_val, blue_val, 255)
}
#[must_use]
pub const fn mix_rgb_const(lhs: Color, rhs: Color, rhs_weight: u8) -> Color {
let lhs_rgba: [u8; 4] = lhs.to_rgba_bytes();
let rhs_rgba: [u8; 4] = rhs.to_rgba_bytes();
let w_rhs: u32 = rhs_weight as u32;
let w_lhs: u32 = 255_u32.saturating_sub(w_rhs);
let red: u8 = (((lhs_rgba[0] as u32) * w_lhs + (rhs_rgba[0] as u32) * w_rhs + 127) / 255) as u8;
let green: u8 =
(((lhs_rgba[1] as u32) * w_lhs + (rhs_rgba[1] as u32) * w_rhs + 127) / 255) as u8;
let blue: u8 =
(((lhs_rgba[2] as u32) * w_lhs + (rhs_rgba[2] as u32) * w_rhs + 127) / 255) as u8;
Color::rgba(red, green, blue, 255)
}
#[must_use]
pub fn contrast_ratio(fg: Color, bg: Color) -> f32 {
let opaque_bg: Color = bg.with_alpha(255);
let l1: f32 = relative_luminance(blend_over(fg, opaque_bg));
let l2: f32 = relative_luminance(opaque_bg);
let (hi, lo): (f32, f32) = if l1 >= l2 { (l1, l2) } else { (l2, l1) };
(hi + 0.05) / (lo + 0.05)
}
fn reduced_motion_signal() -> Signal<bool> {
REDUCED_MOTION_SIG.with(|slot: &RefCell<Option<Signal<bool>>>| {
if let Ok(guard) = slot.try_borrow()
&& let Some(sig) = *guard
&& untracked(|| sig.try_get().is_some())
{
return sig;
}
let fresh: Signal<bool> = Signal::new(false);
if let Ok(mut guard) = slot.try_borrow_mut() {
*guard = Some(fresh);
}
fresh
})
}
#[must_use]
pub fn use_reduced_motion() -> Signal<bool> {
reduced_motion_signal()
}
#[must_use]
pub fn is_reduced_motion() -> bool {
REDUCED_MOTION_SIG.with(|slot: &RefCell<Option<Signal<bool>>>| {
let current: Option<Signal<bool>> = slot
.try_borrow()
.ok()
.and_then(|guard: Ref<'_, Option<Signal<bool>>>| *guard);
current
.and_then(|sig: Signal<bool>| untracked(|| sig.try_get()))
.unwrap_or(false)
})
}
pub fn set_reduced_motion(enabled: bool) {
let _: bool = reduced_motion_signal().set_if_changed(enabled);
}
#[must_use]
pub fn motion_duration(base_secs: f32) -> f32 {
if is_reduced_motion() {
MOTION_REDUCED_SECS
} else {
base_secs
}
}