Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-14 15:42:36 -06:00
commit 975f693ae9
69 files changed
+7703

No files matched your search

+79
View File
@@ -0,0 +1,79 @@
#! Contains painters, which give the color each pixel is painted: a color, or a
#! gradient's, from a table of 256 of them, premultiplied, along it, as far
#! along it as the pixel is, spread past its ends.
import std::math;
module malunal::image::svg;
# What paints: a color, or a gradient, whose colors a table of 256 holds,
# premultiplied, along it.
enum paint_by: u8 {
color,
linear,
radial,
}
enum spread: u8 {
pad,
reflect,
repeat,
}
struct painter {
var by: paint_by;
var color: f32[4]; #! Premultiplied, already scaled by the opacity.
var table: f32[1024]; #! A gradient's colors, premultiplied, already scaled by the opacity.
var inverse: matrix; #! From the canvas to the gradient's own space.
var spread: spread;
var x1: f64; #! A linear gradient's ends, or a radial one's center and focus.
var y1: f64;
var x2: f64;
var y2: f64;
var r: f64;
}
def painter::solid(paint: &color, opacity: f64): painter {
val alpha := (paint.a * opacity) as f32;
return painter { color = [paint.r as f32 * alpha, paint.g as f32 * alpha, paint.b as f32 * alpha, alpha] };
}
# The premultiplied color at the center of a pixel.
def painter::at(&this, x: u64, y: u64): f32[4] {
if this.by == paint_by::color {
return this.color;
}
val here := this.inverse.apply(point { x = x as f64 + 0.5, y = y as f64 + 0.5 });
var t: f64 = 0;
if this.by == paint_by::linear {
val dx := this.x2 - this.x1;
val dy := this.y2 - this.y1;
val length := dx * dx + dy * dy;
t = length == 0 ? 1 : ((here.x - this.x1) * dx + (here.y - this.y1) * dy) / length;
} else {
# Along the line from the focus through the pixel, as far as the circle is.
val dx := here.x - this.x2;
val dy := here.y - this.y2;
val ex := this.x2 - this.x1;
val ey := this.y2 - this.y1;
val a := dx * dx + dy * dy;
if a == 0 {
t = 0;
} else {
val b := ex * dx + ey * dy;
val c := ex * ex + ey * ey - this.r * this.r;
val reach := (-b + math::sqrt(math::max(0.0, b * b - a * c))) / a;
t = reach <= 0 ? 1 : 1 / reach;
}
}
if this.spread == spread::repeat {
t = t - math::floor(t);
} else if this.spread == spread::reflect {
t = t % 2;
t = t < 0 ? t + 2 : t;
t = t > 1 ? 2 - t : t;
}
t = t < 0 ? 0 : (t > 1 ? 1 : t);
val slot := (t * 255 + 0.5) as u64 * 4;
return [this.table[slot], this.table[slot + 1], this.table[slot + 2], this.table[slot + 3]];
}