diff options
Diffstat (limited to 'tiny_skia')
| -rw-r--r-- | tiny_skia/src/backend.rs | 43 | ||||
| -rw-r--r-- | tiny_skia/src/geometry.rs | 46 | 
2 files changed, 72 insertions, 17 deletions
| diff --git a/tiny_skia/src/backend.rs b/tiny_skia/src/backend.rs index 39bed555..9d0fc527 100644 --- a/tiny_skia/src/backend.rs +++ b/tiny_skia/src/backend.rs @@ -1,4 +1,5 @@  use crate::core::text; +use crate::core::Gradient;  use crate::core::{Background, Color, Font, Point, Rectangle, Size, Vector};  use crate::graphics::backend;  use crate::graphics::{Primitive, Viewport}; @@ -184,6 +185,48 @@ impl Backend {                                      *color,                                  ))                              } +                            Background::Gradient(Gradient::Linear(linear)) => { +                                let (start, end) = +                                    linear.angle.to_distance(bounds); + +                                let stops: Vec<tiny_skia::GradientStop> = +                                    linear +                                        .stops +                                        .into_iter() +                                        .flatten() +                                        .map(|stop| { +                                            tiny_skia::GradientStop::new( +                                                stop.offset, +                                                tiny_skia::Color::from_rgba( +                                                    stop.color.b, +                                                    stop.color.g, +                                                    stop.color.r, +                                                    stop.color.a, +                                                ) +                                                .expect("Create color"), +                                            ) +                                        }) +                                        .collect(); + +                                tiny_skia::LinearGradient::new( +                                    tiny_skia::Point { +                                        x: start.x, +                                        y: start.y, +                                    }, +                                    tiny_skia::Point { x: end.x, y: end.y }, +                                    if stops.is_empty() { +                                        vec![tiny_skia::GradientStop::new( +                                            0.0, +                                            tiny_skia::Color::BLACK, +                                        )] +                                    } else { +                                        stops +                                    }, +                                    tiny_skia::SpreadMode::Pad, +                                    tiny_skia::Transform::identity(), +                                ) +                                .expect("Create linear gradient") +                            }                          },                          anti_alias: true,                          ..tiny_skia::Paint::default() diff --git a/tiny_skia/src/geometry.rs b/tiny_skia/src/geometry.rs index a1fd7b60..ee347c73 100644 --- a/tiny_skia/src/geometry.rs +++ b/tiny_skia/src/geometry.rs @@ -1,8 +1,8 @@ -use crate::core::Gradient;  use crate::core::{Point, Rectangle, Size, Vector};  use crate::graphics::geometry::fill::{self, Fill};  use crate::graphics::geometry::stroke::{self, Stroke};  use crate::graphics::geometry::{Path, Style, Text}; +use crate::graphics::Gradient;  use crate::graphics::Primitive;  pub struct Frame { @@ -231,18 +231,11 @@ pub fn into_paint(style: Style) -> tiny_skia::Paint<'static> {                      .expect("Create color"),              ),              Style::Gradient(gradient) => match gradient { -                Gradient::Linear(linear) => tiny_skia::LinearGradient::new( -                    tiny_skia::Point { -                        x: linear.start.x, -                        y: linear.start.y, -                    }, -                    tiny_skia::Point { -                        x: linear.end.x, -                        y: linear.end.y, -                    }, -                    linear -                        .color_stops +                Gradient::Linear(linear) => { +                    let stops: Vec<tiny_skia::GradientStop> = linear +                        .stops                          .into_iter() +                        .flatten()                          .map(|stop| {                              tiny_skia::GradientStop::new(                                  stop.offset, @@ -255,11 +248,30 @@ pub fn into_paint(style: Style) -> tiny_skia::Paint<'static> {                                  .expect("Create color"),                              )                          }) -                        .collect(), -                    tiny_skia::SpreadMode::Pad, -                    tiny_skia::Transform::identity(), -                ) -                .expect("Create linear gradient"), +                        .collect(); + +                    tiny_skia::LinearGradient::new( +                        tiny_skia::Point { +                            x: linear.start.x, +                            y: linear.start.y, +                        }, +                        tiny_skia::Point { +                            x: linear.end.x, +                            y: linear.end.y, +                        }, +                        if stops.is_empty() { +                            vec![tiny_skia::GradientStop::new( +                                0.0, +                                tiny_skia::Color::BLACK, +                            )] +                        } else { +                            stops +                        }, +                        tiny_skia::SpreadMode::Pad, +                        tiny_skia::Transform::identity(), +                    ) +                    .expect("Create linear gradient") +                }              },          },          anti_alias: true, | 
