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