euv-ui 0.48.10

Cross-platform UI component library and 17-scheme design system for the euv framework with pixel-consistent rendering across Web, Android, iOS, iPadOS, macOS, Windows, and Linux.
Documentation
use super::*;

/// Computes the concentric inner corner radius for a child inset `inset` px inside a container of radius `outer`.
///
/// # Arguments
///
/// - `f32` - Outer container corner radius in logical pixels.
/// - `f32` - Uniform inset between the container edge and the child in logical pixels.
///
/// # Returns
///
/// - `f32` - Inner radius `(outer - inset).max(2.0)` (full pills stay full pills).
#[must_use]
pub fn concentric_radius(outer: f32, inset: f32) -> f32 {
    if outer >= RADIUS_PILL {
        RADIUS_PILL
    } else {
        (outer - inset).max(2.0)
    }
}

/// Converts one 8-bit sRGB channel into linear-light intensity (WCAG 2.1) using pure `const fn` arithmetic.
///
/// # Arguments
///
/// - `u8` - sRGB channel value.
///
/// # Returns
///
/// - `f32` - Linear channel intensity in `[0.0, 1.0]`.
pub(crate) const fn srgb_channel_to_linear(channel: u8) -> f32 {
    let c: f32 = channel as f32 / 255.0;
    if c <= 0.04045 {
        return c / 12.92;
    }
    let x: f32 = (c + 0.055) / 1.055;
    let x2: f32 = x * x;
    let mut y: f32 = 0.75;
    let mut step: usize = 0;
    while step < 12 {
        let y4: f32 = y * y * y * y;
        y = (4.0 * y + x2 / y4) * 0.2;
        step += 1;
    }
    x2 * y
}

/// Computes the WCAG 2.1 relative luminance from 8-bit sRGB channels in `const` contexts.
///
/// # Arguments
///
/// - `u8` - Red channel.
/// - `u8` - Green channel.
/// - `u8` - Blue channel.
///
/// # Returns
///
/// - `f32` - Relative luminance in `[0.0, 1.0]`.
#[must_use]
pub const fn relative_luminance_rgb(r: u8, g: u8, b: u8) -> f32 {
    0.2126 * srgb_channel_to_linear(r)
        + 0.7152 * srgb_channel_to_linear(g)
        + 0.0722 * srgb_channel_to_linear(b)
}

/// Computes the WCAG 2.1 contrast ratio between two opaque 24-bit RGB hex colors in `const` contexts.
///
/// # Arguments
///
/// - `u32` - First `0xRRGGBB` color.
/// - `u32` - Second `0xRRGGBB` color.
///
/// # Returns
///
/// - `f32` - Contrast ratio in `[1.0, 21.0]`.
#[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)
    }
}

/// Computes the WCAG 2.1 relative luminance of an opaque color (alpha ignored).
///
/// # Arguments
///
/// - `Color` - Source color.
///
/// # Returns
///
/// - `f32` - Relative luminance in `[0.0, 1.0]`.
#[must_use]
pub fn relative_luminance(color: Color) -> f32 {
    relative_luminance_rgb(color.get_r(), color.get_g(), color.get_b())
}

/// Composites a (possibly translucent) foreground color over an opaque background (source-over).
///
/// # Arguments
///
/// - `Color` - Foreground color with alpha.
/// - `Color` - Background color (treated as opaque).
///
/// # Returns
///
/// - `Color` - Opaque composited color.
#[must_use]
pub fn blend_over(fg: Color, bg: Color) -> Color {
    let a: f32 = fg.get_a() as f32 / 255.0;
    let inv_a: f32 = 1.0 - a;
    let r: u8 = (fg.get_r() as f32 * a + bg.get_r() as f32 * inv_a)
        .round()
        .clamp(0.0, 255.0) as u8;
    let g: u8 = (fg.get_g() as f32 * a + bg.get_g() as f32 * inv_a)
        .round()
        .clamp(0.0, 255.0) as u8;
    let b: u8 = (fg.get_b() as f32 * a + bg.get_b() as f32 * inv_a)
        .round()
        .clamp(0.0, 255.0) as u8;
    Color::rgba(r, g, b, 255)
}

/// Computes the WCAG 2.1 contrast ratio between two colors (`1.0` = identical, `21.0` = black on white).
///
/// `fg` is alpha-composited over `bg` first, so translucent text is measured as it is actually perceived.
///
/// # Arguments
///
/// - `Color` - Foreground color.
/// - `Color` - Background color.
///
/// # Returns
///
/// - `f32` - Contrast ratio in `[1.0, 21.0]`.
#[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)
}

/// Returns the live shared Reduced Motion signal slot, lazily creating it when absent or disposed.
///
/// # Returns
///
/// - `Signal<bool>` - Shared thread-local Reduced Motion signal.
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
    })
}

/// Returns the global reactive Reduced Motion signal shared by every `euv-ui` transition.
///
/// Setting it to `true` collapses every unified motion duration to `MOTION_REDUCED_SECS`.
///
/// # Returns
///
/// - `Signal<bool>` - Shared reactive Reduced Motion toggle.
#[must_use]
pub fn use_reduced_motion() -> Signal<bool> {
    reduced_motion_signal()
}

/// Returns whether global Reduced Motion is enabled without creating a reactive dependency.
///
/// # Returns
///
/// - `bool` - `true` while Reduced Motion is enabled.
#[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)
    })
}

/// Enables or disables global Reduced Motion for every `euv-ui` transition.
///
/// # Arguments
///
/// - `bool` - `true` to collapse all unified motion durations to `MOTION_REDUCED_SECS`.
pub fn set_reduced_motion(enabled: bool) {
    let _: bool = reduced_motion_signal().set_if_changed(enabled);
}

/// Resolves a unified motion duration against the global Reduced Motion preference.
///
/// # Arguments
///
/// - `f32` - Base duration in seconds (`MOTION_MICRO_SECS`, `MOTION_STANDARD_SECS`, or `MOTION_EMPHASIZED_SECS`).
///
/// # Returns
///
/// - `f32` - `MOTION_REDUCED_SECS` while Reduced Motion is enabled, otherwise `base_secs`.
#[must_use]
pub fn motion_duration(base_secs: f32) -> f32 {
    if is_reduced_motion() {
        MOTION_REDUCED_SECS
    } else {
        base_secs
    }
}