### Blend Modes Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Examples of creating different blend modes in Peniko. ```rust use peniko::{BlendMode, Mix, Compose}; // Standard alpha blending let blend = BlendMode::default(); // Normal + SrcOver // Multiply (darken) let blend = BlendMode::new(Mix::Multiply, Compose::SrcOver); // Screen (lighten) let blend = BlendMode::new(Mix::Screen, Compose::SrcOver); // Others: Overlay, Darken, Lighten, ColorDodge, ColorBurn, etc. ``` -------------------------------- ### Builder Pattern Example Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Illustrates the fluent builder API for creating gradients. ```rust let gradient = Gradient::new_linear((0, 0), (100, 100)) .with_stops([red, blue]) .with_extend(Extend::Repeat) .with_interpolation_cs(ColorSpaceTag::OkLab); ``` -------------------------------- ### Accept Styles Flexibly Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Example of a function accepting various style types. ```rust use peniko::StyleRef; fn render(style: impl Into) { let style_ref = style.into(); // ... } // Now callers can pass: render(Fill::NonZero); // Fill render(&my_stroke); // &Stroke render(&my_style); // &Style ``` -------------------------------- ### Common Mistakes: Ignoring Alpha Overflow Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Example demonstrating alpha overflow. ```rust brush.with_alpha(1.5) // ❌ Saturates to 1.0 ``` -------------------------------- ### Accept Brushes Flexibly Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Example of a function accepting various brush types. ```rust use peniko::BrushRef; fn fill(shape: &Path, brush: impl Into) { let brush_ref = brush.into(); // ... } // Now callers can pass: fill(&path, &my_gradient); // &Gradient fill(&path, &my_image_brush); // &ImageBrush fill(&path, &my_brush); // &Brush fill(&path, css::RED); // Color (From) ``` -------------------------------- ### EvenOdd Fill Rule Example Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Example of using the EvenOdd fill rule for shapes with holes. ```rust use peniko::{Style, Fill}; // For a shape with intentional holes let style = Style::Fill(Fill::EvenOdd); ``` -------------------------------- ### Type Conversions Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Demonstrates type conversions using the `From` trait. ```rust use peniko::Brush; use color::palette::css; // Color to Brush let brush: Brush = css::RED.into(); // Gradient to Brush let gradient = Gradient::new_linear((0, 0), (100, 100)); let brush: Brush = gradient.into(); // Fill to Style use peniko::{Style, Fill}; let style: Style = Fill::NonZero.into(); // Stroke to Style use kurbo::Stroke; let style: Style = Stroke::new(2.0).into(); ``` -------------------------------- ### Debugging Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Demonstrates how to use the Debug trait for Peniko types. ```rust println!("{:?}", brush); println!("{:?}", gradient); println!("{:?}", blend_mode); ``` -------------------------------- ### Add to Cargo.toml Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Add Peniko to your Cargo.toml file. ```toml [dependencies] peniko = "0.6" ``` -------------------------------- ### Common Mistakes: Use Generic References Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Example of a flexible type signature using generic references. ```rust fn fill(brush: impl Into) { } ``` -------------------------------- ### Common Mistakes: Over-specifying Types Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Example of overly restrictive type signatures. ```rust fn fill(brush: &Brush) { } // Too restrictive fn fill(brush: &Gradient) { } // Inflexible ``` -------------------------------- ### Make a Brush Transparent Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Create a transparent version of a brush. ```rust let opaque = Brush::Solid(css::RED); let transparent = opaque.with_alpha(0.5); ``` -------------------------------- ### Create an Image Brush Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Create an image brush from pixel data. ```rust use peniko::{ImageBrush, ImageData, ImageFormat, ImageAlphaType}; let image = ImageData { data: my_pixel_blob, format: ImageFormat::Rgba8, alpha_type: ImageAlphaType::Alpha, width: 100, height: 100, }; let brush = ImageBrush::new(image) .with_extend(Extend::Repeat); ``` -------------------------------- ### Common Mistakes: Validate Alpha Range Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Example of validating alpha range. ```rust brush.with_alpha(0.5) // ✅ Valid ``` -------------------------------- ### Reference Types Usage Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Shows how to use generic references for BrushRef and StyleRef. ```rust fn paint(brush: impl Into) { /* ... */ } fn apply(style: impl Into) { /* ... */ } ``` -------------------------------- ### Control Image Quality Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Set the quality for image scaling. ```rust use peniko::ImageQuality; let brush = ImageBrush::new(image) .with_quality(ImageQuality::High); ``` -------------------------------- ### Create a Radial Gradient Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Create a radial gradient brush. ```rust let gradient = Gradient::new_radial((50.0, 50.0), 50.0) .with_stops([css::WHITE, css::BLACK]); ``` -------------------------------- ### Create Custom Color Stops Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Create a gradient with custom color stops. ```rust use peniko::ColorStop; let stops = [ ColorStop { offset: 0.0, color: red }, ColorStop { offset: 0.3, color: green }, ColorStop { offset: 1.0, color: blue }, ]; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops(stops); ``` -------------------------------- ### Create a Linear Gradient Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Create a linear gradient brush with specified stops. ```rust use peniko::Gradient; use color::palette::css; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([css::RED, css::BLUE]); let brush = Brush::Gradient(gradient); ``` -------------------------------- ### Add to Cargo.toml with serialization support Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Add Peniko to your Cargo.toml file with serialization support. ```toml [dependencies] peniko = { version = "0.6", features = ["serde"] } ``` -------------------------------- ### Apply a Fill Style Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Apply a fill style to a shape. ```rust use peniko::{Style, Fill}; let style = Style::Fill(Fill::NonZero); ``` -------------------------------- ### Use Different Color Spaces Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Specify the color space for gradient interpolation. ```rust use color::ColorSpaceTag; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([color1, color2]) .with_interpolation_cs(ColorSpaceTag::OkLab); // Perceptually uniform ``` -------------------------------- ### StyleRef Examples Source: https://github.com/linebender/peniko/blob/main/_autodocs/style.md Examples of creating and using StyleRef. ```rust use peniko::{Style, StyleRef, Fill}; use kurbo::Stroke; // From Fill let fill_ref: StyleRef = Fill::NonZero.into(); // From &Stroke let stroke = Stroke::new(2.0); let stroke_ref: StyleRef = (&stroke).into(); // From &Style let style = Style::Fill(Fill::EvenOdd); let style_ref: StyleRef = (&style).into(); // Using in a function that accepts impl Into fn render_with_style(style: impl Into) { let s = style.into(); match s { StyleRef::Fill(fill) => println!("Filling with {:?}", fill), StyleRef::Stroke(stroke) => println!("Stroking with width {}", stroke.stroke_width), } } render_with_style(Fill::NonZero); render_with_style(&stroke); ``` -------------------------------- ### Create a Solid Color Brush Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Create a solid color brush using a CSS color. ```rust use peniko::Brush; use color::palette::css; let brush = Brush::Solid(css::RED); ``` -------------------------------- ### Apply a Stroke Style Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Apply a stroke style to a shape. ```rust use peniko::Style; use kurbo::Stroke; let style = Style::Stroke(Stroke::new(2.0)); ``` -------------------------------- ### Repeat Texture Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Configure an image brush to repeat its texture. ```rust use peniko::Extend; let brush = ImageBrush::new(image) .with_extend(Extend::Repeat); ``` -------------------------------- ### Style Examples Source: https://github.com/linebender/peniko/blob/main/_autodocs/style.md Examples of creating Style enums. ```rust use peniko::{Style, Fill}; use kurbo::Stroke; // Create a filled style with non-zero fill rule let fill_style = Style::Fill(Fill::NonZero); // Create from Fill directly let fill_style_2 = Style::from(Fill::EvenOdd); // Create a stroked style let stroke = Stroke::new(2.0); let stroke_style = Style::Stroke(stroke); // Create from Stroke directly let stroke_style_2 = Style::from(Stroke::new(1.5)); ``` -------------------------------- ### Create Evenly-Spaced Color Stops Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Create a gradient with evenly spaced color stops. ```rust let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([css::RED, css::GREEN, css::BLUE]); // Automatically spaced at 0.0, 0.5, 1.0 ``` -------------------------------- ### Brush Examples Source: https://github.com/linebender/peniko/blob/main/_autodocs/brush.md Illustrates the creation of Solid and Gradient brushes, including conversion using the From trait. ```rust use peniko::{Brush, Gradient, Color}; use kurbo::Point; // Create solid color brush let red = Color::from_rgba8(255, 0, 0, 255); let solid = Brush::Solid(red); // Create from color using From trait let blue = Color::from_rgba8(0, 0, 255, 255); let from_color: Brush = blue.into(); // Create gradient brush let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)); let gradient_brush = Brush::Gradient(gradient); ``` -------------------------------- ### Fill Examples Source: https://github.com/linebender/peniko/blob/main/_autodocs/style.md Examples of using Fill enum. ```rust use peniko::Fill; // Default non-zero fill rule let fill = Fill::default(); assert_eq!(fill, Fill::NonZero); // Explicit even-odd for creating holes let even_odd = Fill::EvenOdd; // Creating hourglass-like shapes // With NonZero, the center is filled // With EvenOdd, the center is transparent let hourglass_fill = Fill::EvenOdd; ``` -------------------------------- ### Gradient Examples Source: https://github.com/linebender/peniko/blob/main/_autodocs/gradient.md Demonstrates creating linear, radial, and sweep gradients with various configurations. ```rust use peniko::{Gradient, Extend}; use color::palette::css; // Create a linear gradient with color stops let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([css::RED, css::YELLOW, css::GREEN]) .with_extend(Extend::Repeat); // Create a radial gradient let radial = Gradient::new_radial((50.0, 50.0), 50.0) .with_stops([css::WHITE, css::BLACK]); // Create a sweep gradient let sweep = Gradient::new_sweep((50.0, 50.0), 0.0, std::f32::consts::PI * 2.0) .with_stops([css::RED, css::GREEN, css::BLUE]); ``` -------------------------------- ### ImageData example Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Example of creating an ImageData instance. ```rust use peniko::{ImageData, ImageFormat, ImageAlphaType}; let width = 100u32; let height = 100u32; let pixels: Vec = vec![0; (width * height * 4) as usize]; let data = Blob::new(pixels); let image = ImageData { data, format: ImageFormat::Rgba8, alpha_type: ImageAlphaType::Alpha, width, height, }; ``` -------------------------------- ### ImageSampler::new method example Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Example of creating a new ImageSampler with default values. ```rust use peniko::ImageSampler; let sampler = ImageSampler::new(); ``` -------------------------------- ### ImageFormat::size_in_bytes method example Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Example of using the size_in_bytes method. ```rust use peniko::ImageFormat; let format = ImageFormat::Rgba8; let size = format.size_in_bytes(100, 100).unwrap(); assert_eq!(size, 40000); // 100 * 100 * 4 bytes ``` -------------------------------- ### Brush::with_alpha Method Example Source: https://github.com/linebender/peniko/blob/main/_autodocs/brush.md Demonstrates how to create a new Brush with a modified alpha component. ```rust use peniko::{Brush, Color}; let color = Color::from_rgba8(255, 0, 0, 255); // Red let brush = Brush::Solid(color); let transparent = brush.with_alpha(0.5); // 50% transparent ``` -------------------------------- ### Extend Mode Selection - Repeat Mode Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of using the Repeat extend mode for tiling patterns. ```rust Extend::Repeat ``` -------------------------------- ### Brush::multiply_alpha Method Example Source: https://github.com/linebender/peniko/blob/main/_autodocs/brush.md Shows how to create a new Brush with its alpha component multiplied by a given factor. ```rust use peniko::{Brush, Color}; let color = Color::from_rgba8(255, 0, 0, 128); // Red, 50% transparent let brush = Brush::Solid(color); let dimmer = brush.multiply_alpha(0.5); // 25% transparent ``` -------------------------------- ### Image Brush Sampling - Alpha Blending with Images Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of applying alpha transparency to an image brush. ```rust let brush = ImageBrush::new(image) .with_alpha(0.5); // 50% transparent ``` -------------------------------- ### Extend Mode Selection - Pad Mode Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of using the Pad extend mode, which is the default. ```rust use peniko::Extend; Extend::Pad ``` -------------------------------- ### ImageSampler::with_extend method example Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Builder method for setting the image extend mode in both directions. ```rust use peniko::{ImageSampler, Extend}; let sampler = ImageSampler::new() .with_extend(Extend::Repeat); ``` -------------------------------- ### ImageBrush with Extend, Quality, and Alpha Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Example demonstrating chaining builder methods on ImageBrush. ```rust use peniko::{ImageBrush, Extend, ImageQuality}; let brush = ImageBrush::new(image) .with_extend(Extend::Repeat) .with_quality(ImageQuality::High) .with_alpha(0.8); ``` -------------------------------- ### Extend Enum Example Source: https://github.com/linebender/peniko/blob/main/_autodocs/brush.md Shows how to set the extend behavior for a Gradient brush. ```rust use peniko::{Gradient, Extend}; use kurbo::Point; let gradient = Gradient::new_linear((0.0, 0.0), (50.0, 50.0)) .with_extend(Extend::Repeat); ``` -------------------------------- ### Mix enum examples Source: https://github.com/linebender/peniko/blob/main/_autodocs/blend.md Examples of using the Mix enum. ```rust use peniko::Mix; // Use multiply blending let multiply = Mix::Multiply; // Use normal blending let normal = Mix::Normal; ``` -------------------------------- ### ImageSampler with Extend, Quality, and Alpha Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Example demonstrating chaining multiple builder methods on ImageSampler. ```rust use peniko::{ImageSampler, Extend, ImageQuality}; let sampler = ImageSampler::new() .with_extend(Extend::Repeat) .with_quality(ImageQuality::High) .with_alpha(0.5); ``` -------------------------------- ### Blend Mode Examples Source: https://github.com/linebender/peniko/blob/main/_autodocs/blend.md Demonstrates the usage of `Compose` enum for blend modes in Peniko. ```rust use peniko::Compose; // Source over destination (standard alpha compositing) let src_over = Compose::SrcOver; // Clear the destination let clear = Compose::Clear; ``` -------------------------------- ### Linear Gradient Source: https://github.com/linebender/peniko/blob/main/_autodocs/INDEX.md Example of creating a linear gradient with specified start and end points and color stops. ```rust let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([css::RED, css::BLUE]); ``` -------------------------------- ### Blend Mode Usage - Screen Blending Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of using Screen blend mode for lightening effects. ```rust let blend = BlendMode::new(Mix::Screen, Compose::SrcOver); ``` -------------------------------- ### Solid Color Brush Source: https://github.com/linebender/peniko/blob/main/_autodocs/INDEX.md Example of creating a solid color brush. ```rust use peniko::Brush; use color::palette::css; let brush = Brush::Solid(css::RED); ``` -------------------------------- ### Extend Mode Selection - Reflect Mode Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of using the Reflect extend mode for mirroring patterns. ```rust Extend::Reflect ``` -------------------------------- ### BrushRef::to_owned Method Example Source: https://github.com/linebender/peniko/blob/main/_autodocs/brush.md Demonstrates converting a BrushRef to an owned Brush. ```rust use peniko::{Brush, BrushRef, Gradient}; use kurbo::Point; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)); let brush = Brush::Gradient(gradient); let brush_ref: BrushRef = (&brush).into(); let owned = brush_ref.to_owned(); ``` -------------------------------- ### Image Brush Sampling - High-Quality Downsampling Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of using high quality setting for scaling images down. ```rust use peniko::{ImageBrush, ImageQuality}; let brush = ImageBrush::new(image) .with_quality(ImageQuality::High); ``` -------------------------------- ### ImageSampler with Quality Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Example of creating an ImageSampler and setting its quality to High. ```rust use peniko::{ImageSampler, ImageQuality}; let sampler = ImageSampler::new() .with_quality(ImageQuality::High); ``` -------------------------------- ### ImageBrush Creation with ImageData Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Example of creating an ImageBrush with ImageData and a default ImageSampler. ```rust use peniko::{ImageBrush, ImageData, ImageFormat, ImageAlphaType}; let image = ImageData { data: /* ... */, format: ImageFormat::Rgba8, alpha_type: ImageAlphaType::Alpha, width: 100, height: 100, }; let brush = ImageBrush::new(image); ``` -------------------------------- ### Fill Style Source: https://github.com/linebender/peniko/blob/main/_autodocs/INDEX.md Example of defining a fill style using the NonZero fill rule. ```rust use peniko::{Style, Fill}; let style = Style::Fill(Fill::NonZero); ``` -------------------------------- ### ImageSampler::with_x_extend method example Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Builder method for setting the image extend mode in the horizontal direction. ```rust use peniko::{ImageSampler, Extend}; let sampler = ImageSampler::new() .with_x_extend(Extend::Repeat); ``` -------------------------------- ### Controlling Color Interpolation - Matching CSS Color Module Level 4 Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of setting premultiplied alpha for web compatibility, which is the default. ```rust use peniko::{Gradient, InterpolationAlphaSpace}; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([color1, color2]) .with_interpolation_alpha_space(InterpolationAlphaSpace::Premultiplied); // Default, matches CSS Color Module Level 4 § 12.3 ``` -------------------------------- ### Common Stroke Creation Source: https://github.com/linebender/peniko/blob/main/_autodocs/style.md Examples of creating `Stroke` instances. ```rust use kurbo::Stroke; // Basic stroke with width let stroke = Stroke::new(2.0); // Stroke with all defaults let default_stroke = Stroke::new(1.0); ``` -------------------------------- ### Blend Mode Source: https://github.com/linebender/peniko/blob/main/_autodocs/INDEX.md Example of creating a blend mode with specified mix and composition operations. ```rust use peniko::{BlendMode, Mix, Compose}; let blend = BlendMode::new(Mix::Multiply, Compose::SrcOver); ``` -------------------------------- ### Checking Destructive Blend Modes Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of checking if a blend mode is destructive for optimization purposes. ```rust if blend.is_destructive() { // Cannot skip transparent paint // Handle optimization constraints } else { // Can apply certain optimizations } ``` -------------------------------- ### Controlling Color Interpolation - Matching HTML Canvas Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of setting unpremultiplied alpha for HTML canvas compatibility. ```rust let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([color1, color2]) .with_interpolation_alpha_space(InterpolationAlphaSpace::Unpremultiplied); // Matches HTML canvas behavior ``` -------------------------------- ### Gradient new_linear Constructor Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Creates a new linear gradient for the specified start and end points. ```rust pub fn Gradient::new_linear(start: impl Into, end: impl Into) -> Self ``` -------------------------------- ### Gradient new_two_point_radial Constructor Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Creates a new two-point radial gradient with different start and end circles. ```rust pub fn Gradient::new_two_point_radial( start_center: impl Into, start_radius: f32, end_center: impl Into, end_radius: f32, ) -> Self ``` -------------------------------- ### Fill Rule Selection - Even-Odd Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Example of using the Even-Odd fill rule to create holes in shapes. ```rust use peniko::{Style, Fill}; // To create a hole in the middle let style = Style::Fill(Fill::EvenOdd); ``` -------------------------------- ### Multiply Alpha Source: https://github.com/linebender/peniko/blob/main/_autodocs/quickstart.md Multiply the alpha value of a brush. ```rust let brush = Brush::Solid(css::RED.with_alpha(0.8)); let dimmer = brush.multiply_alpha(0.5); // Now 0.4 alpha ``` -------------------------------- ### Building Gradients with the Fluent API - Good Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Demonstrates using chained builder methods for readable gradient construction. ```rust use peniko::Gradient; use color::palette::css; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([css::RED, css::YELLOW, css::GREEN]) .with_extend(Extend::Repeat) .with_quality(ImageQuality::High); ``` -------------------------------- ### ColorStops Inline Storage Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Illustrates how ColorStops uses SmallVec for inline storage, minimizing heap allocations for gradients with up to 4 stops. ```rust use peniko::ColorStops; // No heap allocation for gradients with ≤4 stops let stops = [color1, color2, color3]; let gradient = Gradient::new_linear((0, 0), (100, 100)) .with_stops(stops); ``` -------------------------------- ### Image Brush Sampling - Texture Tiling Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Shows how to use repeat extend mode for repeating textures. ```rust use peniko::{ImageBrush, Extend}; let brush = ImageBrush::new(image) .with_extend(Extend::Repeat); ``` -------------------------------- ### Create Blend Mode Source: https://github.com/linebender/peniko/blob/main/_autodocs/README.md Demonstrates how to create a new BlendMode with Mix::Multiply and Compose::SrcOver, and how to check if it's destructive. ```rust use peniko::{BlendMode, Mix, Compose}; let multiply_blend = BlendMode::new(Mix::Multiply, Compose::SrcOver); // Check if destructive if multiply_blend.is_destructive() { // Handle optimization constraints } ``` -------------------------------- ### Accepting Brushes in Method Signatures - Good Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Demonstrates accepting a generic BrushRef for maximum flexibility. ```rust use peniko::BrushRef; fn fill_shape(shape: &Path, brush: impl Into) { let brush_ref = brush.into(); // Use brush_ref... } // Can call with: fill_shape(path, &my_gradient); fill_shape(path, &my_image_brush); fill_shape(path, solid_color); fill_shape(path, &my_brush); ``` -------------------------------- ### Accepting Styles in Method Signatures - Good Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Shows accepting a generic StyleRef for flexible style application. ```rust use peniko::StyleRef; fn apply_style(style: impl Into) { let style_ref = style.into(); match style_ref { StyleRef::Fill(fill) => { /* ... */ }, StyleRef::Stroke(stroke) => { /* ... */ }, } } // Can call with: apply_style(Fill::NonZero); apply_style(&my_stroke); apply_style(&my_style); ``` -------------------------------- ### Controlling Color Interpolation - Choosing Color Spaces Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Demonstrates setting different color spaces for interpolation, including sRGB, OkLab, and Display P3. ```rust use color::ColorSpaceTag; // sRGB (default, fast, perceptually incorrect) let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_interpolation_cs(ColorSpaceTag::Srgb); // OkLab (perceptually uniform, slower) let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_interpolation_cs(ColorSpaceTag::OkLab); // Display P3 (wider gamut) let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_interpolation_cs(ColorSpaceTag::DisplayP3); ``` -------------------------------- ### Accepting Brushes in Method Signatures - Bad Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Illustrates a less flexible approach requiring cloning or conversion. ```rust fn fill_shape(shape: &Path, brush: &Brush) { // Requires cloning/converting other types } ``` -------------------------------- ### Building Gradients with the Fluent API - Intermediate Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Shows an intermediate approach to gradient building by modifying fields directly. ```rust let mut gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)); gradient.stops = stops.into(); gradient.extend = Extend::Repeat; ``` -------------------------------- ### Setting Color Stops Source: https://github.com/linebender/peniko/blob/main/_autodocs/gradient.md Example of using `with_stops` to define the color stops for a gradient. ```rust use peniko::Gradient; use color::palette::css; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops([css::RED, css::GREEN, css::BLUE]); ``` -------------------------------- ### Create an Image Brush Source: https://github.com/linebender/peniko/blob/main/_autodocs/README.md This snippet demonstrates how to create an image brush, including setting extend behavior and alpha. ```rust use peniko::{ImageBrush, ImageData, ImageFormat, ImageAlphaType, Extend}; let image = ImageData { data: /* blob of pixel bytes */, format: ImageFormat::Rgba8, alpha_type: ImageAlphaType::Alpha, width: 100, height: 100, }; let brush = ImageBrush::new(image) .with_extend(Extend::Repeat) .with_alpha(0.8); ``` -------------------------------- ### RadialGradientPosition new Constructor Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Creates a new radial gradient position with start radius = 0. ```rust pub fn RadialGradientPosition::new(center: impl Into, radius: f32) -> Self ``` -------------------------------- ### Brush References Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Demonstrates how to use BrushRef to avoid allocations when passing brush references. ```rust use peniko::BrushRef; fn paint(brush: impl Into) { // No clone or allocation } ``` -------------------------------- ### Setting Interpolation Color Space Source: https://github.com/linebender/peniko/blob/main/_autodocs/gradient.md Example of using `with_interpolation_cs` to set the color space for gradient interpolation. ```rust use peniko::Gradient; use color::ColorSpaceTag; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_interpolation_cs(ColorSpaceTag::OkLab); ``` -------------------------------- ### ColorStops Constructor Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Constructs an empty collection of stops. ```rust pub fn ColorStops::new() -> Self ``` -------------------------------- ### Image Brush Sampling - Separated Extend Modes Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Demonstrates setting different extend modes for X and Y axes. ```rust let brush = ImageBrush::new(image) .with_x_extend(Extend::Repeat) .with_y_extend(Extend::Pad); ``` -------------------------------- ### ColorStop with_alpha Method Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Returns the color stop with the alpha component set to alpha. ```rust pub const fn with_alpha(self, alpha: f32) -> Self ``` -------------------------------- ### ImageBrush Constructor (ImageData) Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Creates a new ImageBrush for the specified ImageData with default ImageSampler. ```rust pub fn ImageBrush::new(image: ImageData) -> Self ``` -------------------------------- ### ImageSampler with_quality Method Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Builder method for setting the desired image quality hint. ```rust pub fn with_quality(mut self, quality: ImageQuality) -> Self ``` -------------------------------- ### Two-Point Radial Gradient Constructor Source: https://github.com/linebender/peniko/blob/main/_autodocs/gradient.md Creates a new two-point radial gradient defined by two circles (start and end). ```rust use peniko::Gradient; use kurbo::Point; let gradient = Gradient::new_two_point_radial( Point::new(25.0, 25.0), 10.0, Point::new(75.0, 75.0), 50.0 ); ``` -------------------------------- ### Usage Pattern Source: https://github.com/linebender/peniko/blob/main/_autodocs/image.md Demonstrates how to use image brushes, showing a function that accepts any image brush type and can be called with either an owned brush or image data. ```rust use peniko::ImageBrushRef; // Method accepting any image brush type without allocation fn apply_image(brush: impl Into) { let brush_ref = brush.into(); // Use brush_ref... } // Can call with: // apply_image(&my_image_brush); // apply_image(&my_image_data); ``` -------------------------------- ### Sweep Gradient Patterns - Sweep with Angle Offset Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Creating a sweep gradient that starts at a specific angle offset. ```rust use std::f32::consts::PI; let gradient = Gradient::new_sweep( (50.0, 50.0), PI / 4.0, // Start at 45 degrees PI / 4.0 + PI * 2.0 // Full rotation from that offset ) .with_stops([red, green, blue, red]); ``` -------------------------------- ### Set Fill Rule Source: https://github.com/linebender/peniko/blob/main/_autodocs/README.md This snippet shows how to set the fill rule for a style, using either NonZero or EvenOdd. ```rust use peniko::{Style, Fill}; let non_zero_fill = Style::Fill(Fill::NonZero); let even_odd_fill = Style::Fill(Fill::EvenOdd); ``` -------------------------------- ### Sweep Gradient Patterns - Full 360-Degree Sweep Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Creating a full 360-degree sweep gradient, returning to the start color for smoothness. ```rust use peniko::Gradient; use std::f32::consts::PI; let gradient = Gradient::new_sweep((50.0, 50.0), 0.0, PI * 2.0) .with_stops([red, green, blue, red]); // Complete rotation returns to start color for smoothness ``` -------------------------------- ### Fill Default Implementation Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Default implementation for Fill. ```rust impl Default for Fill { fn default() -> Self { Fill::NonZero } } ``` -------------------------------- ### Alpha Manipulation - Setting Full Opacity Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Creating an opaque brush from a potentially transparent one by setting alpha to 1.0. ```rust use peniko::Brush; let transparent_brush = Brush::Solid(color); let opaque = transparent_brush.with_alpha(1.0); ``` -------------------------------- ### StyleRef Usage Pattern Source: https://github.com/linebender/peniko/blob/main/_autodocs/style.md Demonstrates how to use StyleRef in a function that accepts any style type. ```rust use peniko::StyleRef; // Method accepting any style type without allocation fn apply_style(style: impl Into) { let style_ref = style.into(); // Use style_ref... } // Can call with: // apply_style(Fill::NonZero); // apply_style(&my_stroke); // apply_style(&my_style); ``` -------------------------------- ### Bytemuck Support Source: https://github.com/linebender/peniko/blob/main/_autodocs/symbol-index.md Enable bytemuck support with the following TOML configuration. ```toml peniko = { version = "0.6", features = ["bytemuck"] } ``` -------------------------------- ### Accepting Styles in Method Signatures - Bad Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Highlights a restrictive signature that cannot accept all style types directly. ```rust fn apply_style(style: &Style) { // Cannot accept Fill or &Stroke directly } ``` -------------------------------- ### Create a Radial Gradient Source: https://github.com/linebender/peniko/blob/main/_autodocs/README.md This snippet shows how to create a radial gradient with color stops. ```rust use peniko::Gradient; use color::palette::css; let gradient = Gradient::new_radial((50.0, 50.0), 50.0) .with_stops([css::WHITE, css::BLACK]); ``` -------------------------------- ### Cargo.toml Feature Enablement Source: https://github.com/linebender/peniko/blob/main/_autodocs/README.md Shows how to enable the 'serde' feature for serialization and deserialization support in Cargo.toml. ```toml peniko = { version = "0.6", features = ["serde"] } ``` -------------------------------- ### Extend Default Implementation Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Default implementation for Extend. ```rust impl Default for Extend { fn default() -> Self { Extend::Pad } } ``` -------------------------------- ### ImageSampler Constructor Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Creates a new ImageSampler with default values. ```rust pub fn ImageSampler::new() -> Self ``` -------------------------------- ### BlendMode Default Implementation Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Default implementation for BlendMode. ```rust impl Default for BlendMode { fn default() -> Self { Self::new(Mix::Normal, Compose::SrcOver) } } ``` -------------------------------- ### Blend Mode Usage - Standard Transparency Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Default blend mode for standard transparency (Normal, SrcOver). ```rust use peniko::BlendMode; let blend = BlendMode::default(); // Mix::Normal + Compose::SrcOver ``` -------------------------------- ### BlendMode Constructor Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Creates a new blend mode from color mixing and layer composition functions. ```rust pub const fn BlendMode::new(mix: Mix, compose: Compose) -> Self ``` -------------------------------- ### Brush Default Implementation Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Default implementation for Brush. ```rust impl Default for Brush { fn default() -> Self { Self::Solid(AlphaColor::::TRANSPARENT) } } ``` -------------------------------- ### Type Conversions - Gradient to Brush Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Converting a Peniko Gradient to a Brush. ```rust use peniko::{Brush, Gradient}; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)); let brush: Brush = gradient.into(); ``` -------------------------------- ### Type Conversions - Fill to Style Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Converting a Peniko Fill type to a Style. ```rust use peniko::{Style, Fill}; let style: Style = Fill::NonZero.into(); ``` -------------------------------- ### ImageBrush as_ref Method (ImageData) Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Converts an owned ImageBrush into a borrowed ImageBrushRef. ```rust pub fn as_ref(&'_ self) -> ImageBrushRef<'_> ``` -------------------------------- ### Error Handling - Image Size Calculation Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Handling potential errors when calculating image size in bytes. ```rust use peniko::ImageFormat; match format.size_in_bytes(width, height) { Some(bytes) => { /* valid dimensions */ }, None => { /* overflow in calculation */ }, } ``` -------------------------------- ### Gradient with_interpolation_cs Builder Method Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Builder method for setting the interpolation color space. ```rust pub const fn with_interpolation_cs(mut self, interpolation_cs: ColorSpaceTag) -> Self ``` -------------------------------- ### ImageQuality Default Implementation Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Default implementation for ImageQuality. ```rust impl Default for ImageQuality { fn default() -> Self { ImageQuality::Medium } } ``` -------------------------------- ### Gradient with_interpolation_alpha_space Builder Method Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Builder method for setting the interpolation alpha space. ```rust pub const fn with_interpolation_alpha_space( mut self, interpolation_alpha_space: InterpolationAlphaSpace, ) -> Self ``` -------------------------------- ### Color Stop Creation - From Explicit Stops Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Creating a linear gradient with explicitly defined color stops. ```rust use peniko::{Gradient, ColorStop}; let stops = [ ColorStop { offset: 0.0, color: red_dynamic }, ColorStop { offset: 0.5, color: green_dynamic }, ColorStop { offset: 1.0, color: blue_dynamic }, ]; let gradient = Gradient::new_linear((0.0, 0.0), (100.0, 100.0)) .with_stops(stops); ``` -------------------------------- ### Gradient with_extend Builder Method Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Builder method for setting the gradient extend mode. ```rust pub const fn with_extend(mut self, mode: Extend) -> Self ``` -------------------------------- ### Error Handling - Debug Assertions Source: https://github.com/linebender/peniko/blob/main/_autodocs/patterns.md Illustrating debug assertions used for catching invalid alpha inputs. ```rust use peniko::Brush; // These assertions fire in debug builds only: brush.with_alpha(f32::NAN); // ❌ Non-finite brush.with_alpha(-0.5); // ❌ Negative brush.multiply_alpha(f32::NAN); // ❌ Non-finite brush.multiply_alpha(-1.0); // ❌ Negative ``` -------------------------------- ### Gradient Default Implementation Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Default implementation for Gradient. ```rust impl Default for Gradient { fn default() -> Self { Self { kind: LinearGradientPosition { start: Point::default(), end: Point::default() }.into(), extend: Extend::default(), interpolation_cs: ColorSpaceTag::Srgb, hue_direction: HueDirection::default(), interpolation_alpha_space: InterpolationAlphaSpace::default(), stops: ColorStops::default(), } } } ``` -------------------------------- ### ImageSampler with_extend Method Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Builder method for setting the image extend mode in both directions. ```rust pub fn with_extend(mut self, mode: Extend) -> Self ``` -------------------------------- ### ImageSampler with_x_extend Method Source: https://github.com/linebender/peniko/blob/main/_autodocs/function-reference.md Builder method for setting the image extend mode in the horizontal direction. ```rust pub fn with_x_extend(mut self, mode: Extend) -> Self ```