euv-ui 0.49.4

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 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
}

/// 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(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)
}

/// 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_red(), color.get_green(), color.get_blue())
}

/// 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 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)
}

/// Linearly mixes two opaque RGB colors in `const` contexts using an 8-bit weight `rhs_weight` in `0..=255`.
///
/// # Arguments
///
/// - `Color` - Base color (`weight = 255 - rhs_weight`).
/// - `Color` - Target color (`weight = rhs_weight`).
/// - `u8` - Interpolation factor toward `rhs` (`0` = `lhs`, `255` = `rhs`).
///
/// # Returns
///
/// - `Color` - Opaque interpolated color.
#[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)
}

/// 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
    }
}