rocketsplash-rt 0.2.2

Runtime library for loading and rendering Rocketsplash assets (.rst, .rsf)
Documentation
// <FILE>crates/rocketsplash-rt/src/render/fnc_apply_color.rs</FILE>
// <DESC>Apply solid or gradient colors to render buffer</DESC>
// <VERS>VERSION: 1.0.0</VERS>
// <WCTX>Runtime library implementation</WCTX>
// <CLOG>Add apply_color for render buffers</CLOG>

use rocketsplash_formats::Rgb;

use crate::{gradient_color, ColorFill, RenderBuffer};

pub fn apply_color(buffer: &mut RenderBuffer, fill: &ColorFill) {
    match fill {
        ColorFill::Solid(color) => apply_solid(buffer, *color),
        ColorFill::Gradient {
            start,
            end,
            vertical,
        } => apply_gradient(buffer, *start, *end, *vertical),
    }
}

fn apply_solid(buffer: &mut RenderBuffer, color: Rgb) {
    for cell in &mut buffer.cells {
        if cell.ch != ' ' || cell.opacity > 0 {
            cell.fg = Some(color);
        }
    }
}

fn apply_gradient(buffer: &mut RenderBuffer, start: Rgb, end: Rgb, vertical: bool) {
    if buffer.width == 0 || buffer.height == 0 {
        return;
    }
    for y in 0..buffer.height {
        for x in 0..buffer.width {
            let idx = y * buffer.width + x;
            let cell = &mut buffer.cells[idx];
            if cell.ch == ' ' && cell.opacity == 0 {
                continue;
            }
            let t = if vertical {
                if buffer.height <= 1 {
                    0.0
                } else {
                    y as f32 / (buffer.height.saturating_sub(1)) as f32
                }
            } else if buffer.width <= 1 {
                0.0
            } else {
                x as f32 / (buffer.width.saturating_sub(1)) as f32
            };
            cell.fg = Some(gradient_color(start, end, t));
        }
    }
}

// <FILE>crates/rocketsplash-rt/src/render/fnc_apply_color.rs</FILE>
// <VERS>END OF VERSION: 1.0.0</VERS>