#![allow(
clippy::cast_precision_loss,
clippy::cast_possible_truncation,
clippy::cast_sign_loss
)]
mod common;
use thorvg::{ColorSpace, EngineOption, Paint, Thorvg};
fn main() {
let engine = Thorvg::init(0).unwrap();
let (w, h) = (800u32, 300u32);
let mut buffer = vec![0u32; (w * h) as usize];
let mut canvas = engine.sw_canvas(EngineOption::Default).unwrap();
unsafe { canvas.set_target(&mut buffer, w, w, h, ColorSpace::ABGR8888) }.unwrap();
for row in 0..(h / 20) {
for col in 0..(w / 20) {
let mut sq = engine.shape();
sq.append_rect(
col as f32 * 20.0,
row as f32 * 20.0,
20.0,
20.0,
0.0,
0.0,
true,
)
.unwrap();
let gray = if (row + col) % 2 == 0 { 200 } else { 230 };
sq.set_fill_color(gray, gray, gray, 255).unwrap();
canvas.push(sq).unwrap();
}
}
for i in 0..10 {
let mut circle = engine.shape();
circle
.append_circle(60.0 + i as f32 * 75.0, 150.0, 30.0, 30.0, true)
.unwrap();
circle.set_fill_color(0, 100, 255, 255).unwrap();
let opacity = 255 - (i * 25) as u8;
circle.set_opacity(opacity).unwrap();
canvas.push(circle).unwrap();
}
let colors: &[(u8, u8, u8)] = &[(255, 0, 0), (0, 255, 0), (0, 0, 255)];
for (i, &(r, g, b)) in colors.iter().enumerate() {
let mut rect = engine.shape();
rect.append_rect(150.0 + i as f32 * 60.0, 50.0, 120.0, 80.0, 10.0, 10.0, true)
.unwrap();
rect.set_fill_color(r, g, b, 150).unwrap();
canvas.push(rect).unwrap();
}
canvas.draw(true).unwrap();
canvas.sync().unwrap();
common::save_png("opacity.png", &buffer, w, h);
}