pub fn render(size: u32) -> Vec<u8> {
const SS: u32 = 4; let mut rgba = vec![0u8; (size * size * 4) as usize];
let s = 64.0 / size as f32; for y in 0..size {
for x in 0..size {
let mut acc = [0.0f32; 4];
for sy in 0..SS {
for sx in 0..SS {
let px = (x as f32 + (sx as f32 + 0.5) / SS as f32) * s;
let py = (y as f32 + (sy as f32 + 0.5) / SS as f32) * s;
let c = sample(px, py);
acc[0] += c[0] * c[3];
acc[1] += c[1] * c[3];
acc[2] += c[2] * c[3];
acc[3] += c[3];
}
}
let n = (SS * SS) as f32;
let a = acc[3] / n;
let i = ((y * size + x) * 4) as usize;
if a > 0.0 {
rgba[i] = (acc[0] / acc[3] * 255.0).round() as u8;
rgba[i + 1] = (acc[1] / acc[3] * 255.0).round() as u8;
rgba[i + 2] = (acc[2] / acc[3] * 255.0).round() as u8;
}
rgba[i + 3] = (a * 255.0).round() as u8;
}
}
rgba
}
fn sample(px: f32, py: f32) -> [f32; 4] {
let r = 14.0;
let qx = (px - 32.0).abs() - (32.0 - r);
let qy = (py - 32.0).abs() - (32.0 - r);
let d = (qx.max(0.0).powi(2) + qy.max(0.0).powi(2)).sqrt() + qx.max(qy).min(0.0) - r;
if d > 0.0 {
return [0.0; 4];
}
let t = py / 64.0;
let top = [0.110, 0.137, 0.165];
let bottom = [0.047, 0.055, 0.067];
let mut c =
[top[0] + (bottom[0] - top[0]) * t, top[1] + (bottom[1] - top[1]) * t, top[2] + (bottom[2] - top[2]) * t];
if d > -1.5 {
c = [c[0] + 0.06, c[1] + 0.07, c[2] + 0.08];
}
let seg = |ax: f32, ay: f32, bx: f32, by: f32| {
let (vx, vy) = (bx - ax, by - ay);
let k = (((px - ax) * vx + (py - ay) * vy) / (vx * vx + vy * vy)).clamp(0.0, 1.0);
((px - ax - vx * k).powi(2) + (py - ay - vy * k).powi(2)).sqrt()
};
let chevron = seg(17.0, 21.0, 29.0, 32.0).min(seg(29.0, 32.0, 17.0, 43.0));
let bar = seg(34.0, 43.0, 46.0, 43.0);
let accent = [0.322, 0.714, 0.800];
if chevron < 3.6 || bar < 3.6 {
c = accent;
}
[c[0], c[1], c[2], 1.0]
}