euv-ui 0.51.0

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::*;

/// Returns a cached zero-allocation `&'static ImageBuffer` for the canonical 96x96 transparent RGBA8 Euv brand logo.
///
/// # Returns
///
/// - `&'static ImageBuffer` - Process-wide cached RGBA8 brand logo reference.
#[must_use]
pub fn euv_brand_logo_image_ref() -> &'static ImageBuffer {
    EUV_BRAND_LOGO_CACHE.get_or_init(decode_euv_brand_logo)
}

/// Decodes and caches the canonical 96x96 transparent RGBA8 Euv brand logo image derived from `https://devhub.vip/pages/docs/img/euv.png`.
///
/// # Returns
///
/// - `ImageBuffer` - Zero-copy shared handle to the cached RGBA8 brand logo image buffer.
#[must_use]
pub fn euv_brand_logo_image() -> ImageBuffer {
    euv_brand_logo_image_ref().clone()
}

/// Evaluates the Catmull-Rom cubic interpolation kernel at distance `t` for crisp high-DPI image resampling.
///
/// # Arguments
///
/// - `f32` - Normalized sample offset from tap center.
///
/// # Returns
///
/// - `f32` - Cubic reconstruction weight.
fn catmull_rom_weight(offset: f32) -> f32 {
    let abs_t: f32 = offset.abs();
    if abs_t < 1.0 {
        (1.5 * abs_t - 2.5) * abs_t * abs_t + 1.0
    } else if abs_t < 2.0 {
        ((-0.5 * abs_t + 2.5) * abs_t - 4.0) * abs_t + 2.0
    } else {
        0.0
    }
}

/// Returns a crisp live-DPR RGBA8 `ImageBuffer` of the Euv brand logo sized to exact physical pixels `(ceil(logical_size * scale_factor))`, avoiding blurry fixed-96px upscaling on HiDPI displays.
///
/// # Arguments
///
/// - `f32` - Logical square edge length in CSS/point units.
/// - `f32` - Active display DPI scale multiplier.
///
/// # Returns
///
/// - `ImageBuffer` - Live-DPI square RGBA8 brand logo image buffer at exact target physical resolution.
#[must_use]
pub fn euv_brand_logo_image_for_scale(logical_size: f32, scale_factor: f32) -> ImageBuffer {
    let safe_logical: f64 = if logical_size.is_finite() && logical_size > 0.0 {
        f64::from(logical_size).max(1.0)
    } else {
        f64::from(EUV_BRAND_LOGO_SIZE)
    };
    let target_dim: u32 = compute_lossless_physical_dim(safe_logical, scale_factor)
        .clamp(16, EUV_BRAND_LOGO_MAX_RASTER_DIM);
    if target_dim == EUV_BRAND_LOGO_SIZE {
        return euv_brand_logo_image();
    }
    let cached_hit: Option<ImageBuffer> = EUV_BRAND_LOGO_SCALED_CACHE
        .try_with(
            |slot: &RefCell<Vec<(u32, ImageBuffer)>>| -> Option<ImageBuffer> {
                let guard: Ref<'_, Vec<(u32, ImageBuffer)>> = slot.borrow();
                guard
                    .iter()
                    .find(|(dim, _): &&(u32, ImageBuffer)| *dim == target_dim)
                    .map(|(_, buf): &(u32, ImageBuffer)| buf.clone())
            },
        )
        .ok()
        .flatten();
    if let Some(hit) = cached_hit {
        return hit;
    }
    let src: &'static ImageBuffer = euv_brand_logo_image_ref();
    let src_dim: usize = src.get_width() as usize;
    let src_pixels: &[u8] = src.get_pixels();
    let dst_dim: usize = target_dim as usize;
    let mut dst_pixels: Vec<u8> = vec![0; dst_dim.saturating_mul(dst_dim).saturating_mul(4)];
    let scale_step: f32 = (src_dim as f32) / (dst_dim as f32);
    let max_coord: isize = (src_dim.saturating_sub(1)) as isize;
    for dst_y in 0..dst_dim {
        let src_y: f32 = ((dst_y as f32) + 0.5) * scale_step - 0.5;
        let iy: isize = src_y.floor() as isize;
        let fy: f32 = src_y - (iy as f32);
        let wy: [f32; 4] = [
            catmull_rom_weight(fy + 1.0),
            catmull_rom_weight(fy),
            catmull_rom_weight(1.0 - fy),
            catmull_rom_weight(2.0 - fy),
        ];
        let sy_idx: [usize; 4] = [
            (iy - 1).clamp(0, max_coord) as usize,
            iy.clamp(0, max_coord) as usize,
            (iy + 1).clamp(0, max_coord) as usize,
            (iy + 2).clamp(0, max_coord) as usize,
        ];
        for dst_x in 0..dst_dim {
            let src_x: f32 = ((dst_x as f32) + 0.5) * scale_step - 0.5;
            let ix: isize = src_x.floor() as isize;
            let fx: f32 = src_x - (ix as f32);
            let wx: [f32; 4] = [
                catmull_rom_weight(fx + 1.0),
                catmull_rom_weight(fx),
                catmull_rom_weight(1.0 - fx),
                catmull_rom_weight(2.0 - fx),
            ];
            let sx_idx: [usize; 4] = [
                (ix - 1).clamp(0, max_coord) as usize,
                ix.clamp(0, max_coord) as usize,
                (ix + 1).clamp(0, max_coord) as usize,
                (ix + 2).clamp(0, max_coord) as usize,
            ];
            let mut acc_r: f32 = 0.0;
            let mut acc_g: f32 = 0.0;
            let mut acc_b: f32 = 0.0;
            let mut acc_a: f32 = 0.0;
            let mut w_sum: f32 = 0.0;
            for ky in 0..4 {
                let row_off: usize = sy_idx[ky] * src_dim;
                let weight_y: f32 = wy[ky];
                for kx in 0..4 {
                    let weight: f32 = weight_y * wx[kx];
                    let off: usize = (row_off + sx_idx[kx]) * 4;
                    let a_norm: f32 = (src_pixels[off + 3] as f32) / 255.0;
                    acc_r += (src_pixels[off] as f32) * a_norm * weight;
                    acc_g += (src_pixels[off + 1] as f32) * a_norm * weight;
                    acc_b += (src_pixels[off + 2] as f32) * a_norm * weight;
                    acc_a += a_norm * weight;
                    w_sum += weight;
                }
            }
            let inv_w: f32 = if w_sum.abs() > 1e-6 { 1.0 / w_sum } else { 1.0 };
            let alpha_out: f32 = (acc_a * inv_w).clamp(0.0, 1.0);
            let dst_off: usize = (dst_y * dst_dim + dst_x) * 4;
            if alpha_out > 1e-4 {
                let inv_a: f32 = inv_w / alpha_out;
                dst_pixels[dst_off] = (acc_r * inv_a).clamp(0.0, 255.0).round() as u8;
                dst_pixels[dst_off + 1] = (acc_g * inv_a).clamp(0.0, 255.0).round() as u8;
                dst_pixels[dst_off + 2] = (acc_b * inv_a).clamp(0.0, 255.0).round() as u8;
                dst_pixels[dst_off + 3] = (alpha_out * 255.0).clamp(0.0, 255.0).round() as u8;
            }
        }
    }
    let rendered: ImageBuffer = ImageBuffer::new(target_dim, target_dim, dst_pixels);
    let _: bool = EUV_BRAND_LOGO_SCALED_CACHE
        .try_with(|slot: &RefCell<Vec<(u32, ImageBuffer)>>| {
            if let Ok(mut guard) = slot.try_borrow_mut() {
                if guard.len() >= EUV_BRAND_LOGO_SCALED_CACHE_MAX {
                    let _: (u32, ImageBuffer) = guard.remove(0);
                }
                guard.push((target_dim, rendered.clone()));
            }
        })
        .is_ok();
    rendered
}

/// Paints the Euv brand emblem resolution-independently using pure analytic vector `DrawCommand`s crisp at any size and DPI.
///
/// # Arguments
///
/// - `Rect` - Target logical bounds for the brand emblem.
/// - `&mut DisplayList` - Destination display list receiving analytic vector draw commands.
pub fn draw_euv_brand_logo(bounds: Rect, display_list: &mut DisplayList) {
    let side: f32 = bounds.get_width().min(bounds.get_height()).max(1.0);
    let ox: f32 = bounds.get_x() + (bounds.get_width() - side) * 0.5;
    let oy: f32 = bounds.get_y() + (bounds.get_height() - side) * 0.5;
    let square: Rect = Rect::new(ox, oy, side, side);
    let corner_r: f32 = side * 0.24;
    let bg_grad: LinearGradient =
        LinearGradient::new(135.0, Color::hex(0x6366F1), Color::hex(0x22D3EE));
    display_list.push(DrawCommand::LinearGradientRect {
        rect: square,
        gradient: bg_grad,
        radius: CornerRadius::all(corner_r),
    });
    let ring_inset: f32 = side * 0.04;
    let ring_rect: Rect = Rect::new(
        ox + ring_inset,
        oy + ring_inset,
        (side - ring_inset * 2.0).max(1.0),
        (side - ring_inset * 2.0).max(1.0),
    );
    display_list.push(DrawCommand::FillRoundedRect {
        rect: ring_rect,
        color: COLOR_TRANSPARENT,
        radius: CornerRadius::all((corner_r - ring_inset * 0.5).max(1.0)),
        border: Border::new((side * 0.025).max(1.0), Color::rgba(255, 255, 255, 64)),
    });
    let stroke_w: f32 = (side * 0.095).max(1.5);
    let bar_h: f32 = stroke_w;
    let bar_r: CornerRadius = CornerRadius::all(bar_h * 0.5);
    let left_x: f32 = ox + side * 0.25;
    let full_w: f32 = side * 0.50;
    let mid_w: f32 = side * 0.38;
    let top_y: f32 = oy + side * 0.26;
    let mid_y: f32 = oy + (side - bar_h) * 0.5;
    let bot_y: f32 = oy + side * 0.74 - bar_h;
    display_list.push(DrawCommand::FillRoundedRect {
        rect: Rect::new(left_x, top_y, full_w, bar_h),
        color: COLOR_WHITE,
        radius: bar_r,
        border: Border::default(),
    });
    display_list.push(DrawCommand::FillRoundedRect {
        rect: Rect::new(left_x, mid_y, mid_w, bar_h),
        color: Color::rgba(255, 255, 255, 236),
        radius: bar_r,
        border: Border::default(),
    });
    display_list.push(DrawCommand::FillRoundedRect {
        rect: Rect::new(left_x, bot_y, full_w, bar_h),
        color: COLOR_WHITE,
        radius: bar_r,
        border: Border::default(),
    });
    display_list.push(DrawCommand::Circle {
        center: Point::new(ox + side * 0.72, oy + side * 0.50),
        radius: (side * 0.065).max(1.5),
        color: Color::hex(0xE0F2FE),
    });
}

/// Decodes the embedded palette-indexed RLE brand logo payload into a flat RGBA8 `ImageBuffer`.
///
/// # Returns
///
/// - `ImageBuffer` - Decoded 96x96 RGBA8 pixel buffer.
fn decode_euv_brand_logo() -> ImageBuffer {
    let pixel_count: usize =
        (EUV_BRAND_LOGO_SIZE as usize).saturating_mul(EUV_BRAND_LOGO_SIZE as usize);
    let mut pixels: Vec<u8> = Vec::with_capacity(pixel_count.saturating_mul(4));
    let mut cursor: usize = 0;
    let total_bytes: usize = EUV_BRAND_LOGO_RLE.len();
    while cursor < total_bytes {
        let header: u8 = EUV_BRAND_LOGO_RLE[cursor];
        cursor += 1;
        if (header & EUV_BRAND_LOGO_RLE_RUN_FLAG) != 0 {
            if cursor >= total_bytes {
                break;
            }
            let count: usize =
                usize::from(header & EUV_BRAND_LOGO_RLE_RUN_MASK) + EUV_BRAND_LOGO_RLE_RUN_MIN;
            let palette_idx: usize = usize::from(EUV_BRAND_LOGO_RLE[cursor]);
            cursor += 1;
            let rgba: [u8; 4] = EUV_BRAND_LOGO_PALETTE[palette_idx];
            for _ in 0..count {
                pixels.extend_from_slice(&rgba);
            }
        } else {
            let count: usize = usize::from(header) + 1;
            let end: usize = cursor.saturating_add(count).min(total_bytes);
            for &idx in &EUV_BRAND_LOGO_RLE[cursor..end] {
                let rgba: [u8; 4] = EUV_BRAND_LOGO_PALETTE[usize::from(idx)];
                pixels.extend_from_slice(&rgba);
            }
            cursor = end;
        }
    }
    pixels.resize(pixel_count.saturating_mul(4), 0);
    ImageBuffer::new(EUV_BRAND_LOGO_SIZE, EUV_BRAND_LOGO_SIZE, pixels)
}