pub struct Paint<'a> {
pub shader: Shader<'a>,
pub blend_mode: BlendMode,
pub anti_alias: bool,
pub force_hq_pipeline: bool,
}
Expand description
Controls how a shape should be painted.
Fields§
§shader: Shader<'a>
A paint shader.
Default: black color
blend_mode: BlendMode
Paint blending mode.
Default: SourceOver
anti_alias: bool
Enables anti-aliased painting.
Default: false
force_hq_pipeline: bool
Forces the high quality/precision rendering pipeline.
tiny-skia
, just like Skia, has two rendering pipelines:
one uses f32
and another one uses u16
. u16
one is usually way faster,
but less precise. Which can lead to slight differences.
By default, tiny-skia
will choose the pipeline automatically,
depending on a blending mode and other parameters.
But you can force the high quality one using this flag.
This feature is especially useful during testing.
Unlike high quality pipeline, the low quality one doesn’t support all rendering stages, therefore we cannot force it like hq one.
Default: false
Implementations§
source§impl<'a> Paint<'a>
impl<'a> Paint<'a>
sourcepub fn set_color_rgba8(&mut self, r: u8, g: u8, b: u8, a: u8)
pub fn set_color_rgba8(&mut self, r: u8, g: u8, b: u8, a: u8)
Sets a paint source to a solid color.
self.shader = Shader::SolidColor(Color::from_rgba8(50, 127, 150, 200));
shorthand.
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn crate_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 20.0);
pb.line_to(20.0, 20.0);
pb.line_to(10.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(20, 20).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
fn main() {
let mut pb = PathBuilder::new();
pb.move_to(50.0, 100.0);
pb.cubic_to(130.0, 20.0, 390.0, 120.0, 450.0, 30.0);
let path = pb.finish().unwrap();
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pixmap = Pixmap::new(500, 500).unwrap();
let mut transform = Transform::identity();
for i in 0..20 {
let mut stroke = Stroke::default();
stroke.width = 2.0 - (i as f32 / 10.0);
pixmap.stroke_path(&path, &paint, &stroke, transform, None);
transform = transform.pre_translate(0.0, 20.0);
}
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
fn main() {
let clip_path = {
let mut pb = PathBuilder::new();
pb.push_circle(250.0, 250.0, 200.0);
pb.push_circle(250.0, 250.0, 100.0);
pb.finish().unwrap()
};
let clip_path = clip_path
.transform(Transform::from_row(1.0, -0.3, 0.0, 1.0, 0.0, 75.0))
.unwrap();
let mut clip_mask = ClipMask::new();
clip_mask.set_path(500, 500, &clip_path, FillRule::EvenOdd, true);
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.fill_rect(
Rect::from_xywh(0.0, 0.0, 500.0, 500.0).unwrap(),
&paint,
Transform::identity(),
Some(&clip_mask),
);
pixmap.save_png("image.png").unwrap();
}
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
fn create_triangle() -> Pixmap {
let mut paint = Paint::default();
paint.set_color_rgba8(50, 127, 150, 200);
paint.anti_alias = true;
let mut pb = PathBuilder::new();
pb.move_to(0.0, 200.0);
pb.line_to(200.0, 200.0);
pb.line_to(100.0, 0.0);
pb.close();
let path = pb.finish().unwrap();
let mut pixmap = Pixmap::new(200, 200).unwrap();
pixmap.fill_path(
&path,
&paint,
FillRule::Winding,
Transform::identity(),
None,
);
let path = PathBuilder::from_rect(Rect::from_ltrb(0.0, 0.0, 200.0, 200.0).unwrap());
let stroke = Stroke::default();
paint.set_color_rgba8(200, 0, 0, 220);
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None); // TODO: stroke_rect
pixmap
}
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() {
let mut paint = Paint::default();
paint.set_color_rgba8(0, 127, 0, 200);
paint.anti_alias = true;
let path = {
let mut pb = PathBuilder::new();
const RADIUS: f32 = 250.0;
const CENTER: f32 = 250.0;
pb.move_to(CENTER + RADIUS, CENTER);
for i in 1..8 {
let a = 2.6927937 * i as f32;
pb.line_to(CENTER + RADIUS * a.cos(), CENTER + RADIUS * a.sin());
}
pb.finish().unwrap()
};
let mut stroke = Stroke::default();
stroke.width = 6.0;
stroke.line_cap = LineCap::Round;
stroke.dash = StrokeDash::new(vec![20.0, 40.0], 0.0);
let mut pixmap = Pixmap::new(500, 500).unwrap();
pixmap.stroke_path(&path, &paint, &stroke, Transform::identity(), None);
pixmap.save_png("image.png").unwrap();
}
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
fn main() {
let mut paint1 = Paint::default();
paint1.set_color_rgba8(50, 127, 150, 200);
paint1.anti_alias = true;
let mut paint2 = Paint::default();
paint2.set_color_rgba8(220, 140, 75, 180);
let path1 = {
let mut pb = PathBuilder::new();
pb.move_to(60.0, 60.0);
pb.line_to(160.0, 940.0);
pb.cubic_to(380.0, 840.0, 660.0, 800.0, 940.0, 800.0);
pb.cubic_to(740.0, 460.0, 440.0, 160.0, 60.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let path2 = {
let mut pb = PathBuilder::new();
pb.move_to(940.0, 60.0);
pb.line_to(840.0, 940.0);
pb.cubic_to(620.0, 840.0, 340.0, 800.0, 60.0, 800.0);
pb.cubic_to(260.0, 460.0, 560.0, 160.0, 940.0, 60.0);
pb.close();
pb.finish().unwrap()
};
let mut pixmap = Pixmap::new(1000, 1000).unwrap();
pixmap.fill_path(
&path1,
&paint1,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.fill_path(
&path2,
&paint2,
FillRule::Winding,
Transform::identity(),
None,
);
pixmap.save_png("image.png").unwrap();
}
sourcepub fn is_solid_color(&self) -> bool
pub fn is_solid_color(&self) -> bool
Checks that the paint source is a solid color.