♻️ Normalize opacity in fills to u8

This commit is contained in:
Belén Albeza 2025-04-29 15:10:05 +02:00
parent 875e019d4f
commit 81f18ad7f4
2 changed files with 8 additions and 16 deletions

View file

@ -7,7 +7,7 @@ use crate::uuid::Uuid;
pub struct Gradient { pub struct Gradient {
start: (f32, f32), start: (f32, f32),
end: (f32, f32), end: (f32, f32),
opacity: f32, opacity: u8,
width: f32, width: f32,
colors: Vec<Color>, colors: Vec<Color>,
offsets: Vec<f32>, offsets: Vec<f32>,
@ -17,7 +17,7 @@ impl Gradient {
pub fn new( pub fn new(
start: (f32, f32), start: (f32, f32),
end: (f32, f32), end: (f32, f32),
opacity: f32, opacity: u8,
width: f32, width: f32,
stops: &[(Color, f32)], stops: &[(Color, f32)],
) -> Self { ) -> Self {
@ -143,7 +143,7 @@ impl Fill {
Self::LinearGradient(gradient) => { Self::LinearGradient(gradient) => {
let mut p = skia::Paint::default(); let mut p = skia::Paint::default();
p.set_shader(gradient.to_linear_shader(rect)); p.set_shader(gradient.to_linear_shader(rect));
p.set_alpha((gradient.opacity * 255.) as u8); p.set_alpha(gradient.opacity);
p.set_style(skia::PaintStyle::Fill); p.set_style(skia::PaintStyle::Fill);
p.set_anti_alias(anti_alias); p.set_anti_alias(anti_alias);
p.set_blend_mode(skia::BlendMode::SrcOver); p.set_blend_mode(skia::BlendMode::SrcOver);
@ -152,7 +152,7 @@ impl Fill {
Self::RadialGradient(gradient) => { Self::RadialGradient(gradient) => {
let mut p = skia::Paint::default(); let mut p = skia::Paint::default();
p.set_shader(gradient.to_radial_shader(rect)); p.set_shader(gradient.to_radial_shader(rect));
p.set_alpha((gradient.opacity * 255.) as u8); p.set_alpha(gradient.opacity);
p.set_style(skia::PaintStyle::Fill); p.set_style(skia::PaintStyle::Fill);
p.set_anti_alias(anti_alias); p.set_anti_alias(anti_alias);
p.set_blend_mode(skia::BlendMode::SrcOver); p.set_blend_mode(skia::BlendMode::SrcOver);

View file

@ -67,7 +67,7 @@ const RAW_GRADIENT_DATA_SIZE: usize =
#[derive(Debug)] #[derive(Debug)]
#[repr(C)] #[repr(C)]
pub struct RawGradientData { struct RawGradientData {
start_x: f32, start_x: f32,
start_y: f32, start_y: f32,
end_x: f32, end_x: f32,
@ -111,21 +111,13 @@ impl RawGradientData {
pub fn end(&self) -> (f32, f32) { pub fn end(&self) -> (f32, f32) {
(self.end_x, self.end_y) (self.end_x, self.end_y)
} }
pub fn opacity(&self) -> f32 {
self.opacity
}
pub fn width(&self) -> f32 {
self.width
}
} }
pub const RAW_STOP_DATA_SIZE: usize = 8; pub const RAW_STOP_DATA_SIZE: usize = 8;
#[derive(Debug)] #[derive(Debug)]
#[repr(C)] #[repr(C)]
pub struct RawStopData { struct RawStopData {
color: u32, color: u32,
offset: f32, offset: f32,
} }
@ -173,8 +165,8 @@ impl From<RawGradientData> for Gradient {
Gradient::new( Gradient::new(
raw_gradient.start(), raw_gradient.start(),
raw_gradient.end(), raw_gradient.end(),
raw_gradient.opacity(), (raw_gradient.opacity * 255.) as u8,
raw_gradient.width(), raw_gradient.width,
&stops, &stops,
) )
} }