♻️ Refactor blend mode and fills into their own submodules

This commit is contained in:
Belén Albeza 2024-11-29 11:46:45 +01:00
parent 2d4281bdf2
commit 00ab9ad3f0
5 changed files with 116 additions and 109 deletions

View file

@ -0,0 +1,80 @@
use skia_safe as skia;
use super::Color;
use crate::math;
#[derive(Debug, Clone, PartialEq)]
pub struct Gradient {
colors: Vec<Color>,
offsets: Vec<f32>,
opacity: f32,
start: (f32, f32),
end: (f32, f32),
}
impl Gradient {
pub fn add_stop(&mut self, color: Color, offset: f32) {
self.colors.push(color);
self.offsets.push(offset);
}
fn to_shader(&self, rect: &math::Rect) -> skia::Shader {
let start = (
rect.left + self.start.0 * rect.width(),
rect.top + self.start.1 * rect.height(),
);
let end = (
rect.left + self.end.0 * rect.width(),
rect.top + self.end.1 * rect.height(),
);
let shader = skia::shader::Shader::linear_gradient(
(start, end),
self.colors.as_slice(),
self.offsets.as_slice(),
skia::TileMode::Clamp,
None,
None,
)
.unwrap();
shader
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum Fill {
Solid(Color),
LinearGradient(Gradient),
}
impl Fill {
pub fn new_linear_gradient(start: (f32, f32), end: (f32, f32), opacity: f32) -> Self {
Self::LinearGradient(Gradient {
start,
end,
opacity,
colors: vec![],
offsets: vec![],
})
}
pub fn to_paint(&self, rect: &math::Rect) -> skia::Paint {
match self {
Self::Solid(color) => {
let mut p = skia::Paint::default();
p.set_color(*color);
p.set_style(skia::PaintStyle::Fill);
p.set_anti_alias(true);
p.set_blend_mode(skia::BlendMode::SrcOver);
p
}
Self::LinearGradient(gradient) => {
let mut p = skia::Paint::default();
p.set_shader(gradient.to_shader(&rect));
p.set_alpha((gradient.opacity * 255.) as u8);
p.set_style(skia::PaintStyle::Fill);
p.set_blend_mode(skia::BlendMode::SrcOver);
p
}
}
}
}