cotis-wgpu 0.1.0-alpha

Desktop wgpu renderer backend for Cotis
Documentation
//! Color conversion between cotis and wgpu representations.
//!
//! Cotis [`cotis_defaults::colors::Color`] stores each channel as `f32` in the `0..=255`
//! range. This module normalizes channels to `0.0..=1.0` for GPU use.
//!
//! **Alpha handling differs by consumer:**
//!
//! - [`cotis_color_to_ui_color`] — RGB only; used by the UI geometry pipeline, which forces
//!   opaque fragments in the WGSL shader.
//! - [`cotis_color_to_wgpu`] — full RGBA; suitable for swapchain clear colors and
//!   [`crate::renderer::CotisWgpuRenderer::set_background`].
//! - Text rendering converts cotis colors to glyphon `Color` with rounded `u8` channels,
//!   preserving alpha.

use cotis_defaults::colors::Color;

use crate::pipeline::UIColor;

/// Converts a cotis RGBA color (0..255 per channel) into normalized GPU color components.
///
/// Alpha is omitted because the UI geometry shader outputs opaque fragments.
///
/// # Examples
///
/// ```
/// use cotis_defaults::colors::Color;
/// use cotis_wgpu::color::cotis_color_to_ui_color;
///
/// let ui = cotis_color_to_ui_color(Color::rgba(255.0, 255.0, 255.0, 255.0));
/// assert!((ui.r - 1.0).abs() < f32::EPSILON);
/// assert!((ui.g - 1.0).abs() < f32::EPSILON);
/// assert!((ui.b - 1.0).abs() < f32::EPSILON);
/// ```
pub fn cotis_color_to_ui_color(color: Color) -> UIColor {
    UIColor {
        r: color.r / 255.0,
        g: color.g / 255.0,
        b: color.b / 255.0,
    }
}

/// Converts a cotis RGBA color into a wgpu clear/present color (0.0..=1.0 per channel).
///
/// Use this when setting the swapchain background via
/// [`crate::renderer::CotisWgpuRenderer::set_background`] or during renderer construction.
pub fn cotis_color_to_wgpu(color: Color) -> wgpu::Color {
    wgpu::Color {
        r: (color.r / 255.0) as f64,
        g: (color.g / 255.0) as f64,
        b: (color.b / 255.0) as f64,
        a: (color.a / 255.0) as f64,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn converts_cotis_white_to_unit_rgb() {
        let ui = cotis_color_to_ui_color(Color::rgba(255.0, 255.0, 255.0, 255.0));
        assert!((ui.r - 1.0).abs() < f32::EPSILON);
        assert!((ui.g - 1.0).abs() < f32::EPSILON);
        assert!((ui.b - 1.0).abs() < f32::EPSILON);
    }
}