Skip to main content

Gradient

Trait Gradient 

Source
pub trait Gradient: Any + Debug {
    // Required methods
    fn colors(&self) -> &[Color];
    fn stops(&self) -> Option<&[f64]>;
    fn transform(&self) -> Option<&Rc<dyn GradientTransform>>;
    fn create_shader(
        &self,
        rect: Rect,
        text_direction: Option<TextDirection>,
    ) -> Shader;
    fn scale(&self, factor: f64) -> GradientRef;
    fn with_opacity(&self, opacity: f64) -> GradientRef;
    fn as_any(&self) -> &(dyn Any + 'static);

    // Provided methods
    fn from_color(&self, color: Color) -> GradientRef { ... }
    fn lerp_from(
        &self,
        a: Option<&(dyn Gradient + 'static)>,
        t: f64,
    ) -> Option<GradientRef> { ... }
    fn lerp_to(
        &self,
        b: Option<&(dyn Gradient + 'static)>,
        t: f64,
    ) -> Option<GradientRef> { ... }
    fn eq_gradient(&self, other: &(dyn Gradient + 'static)) -> bool { ... }
}
Expand description

A 2D gradient.

This is an interface that allows LinearGradient, RadialGradient, and SweepGradient classes to be used interchangeably in BoxDecorations.

Required Methods§

Source

fn colors(&self) -> &[Color]

The colors the gradient should obtain at each of the stops.

If stops is non-null, this list must have the same length as stops.

This list must have at least two colors in it (otherwise, it’s not a gradient!).

Source

fn stops(&self) -> Option<&[f64]>

A list of values from 0.0 to 1.0 that denote fractions along the gradient.

If non-null, this list must have the same length as colors.

If the first value is not 0.0, then a stop with position 0.0 and a color equal to the first color in colors is implied.

If the last value is not 1.0, then a stop with position 1.0 and a color equal to the last color in colors is implied.

The values in the stops list must be in ascending order. If a value in the stops list is less than an earlier value in the list, then its value is assumed to equal the previous value.

If stops is null, then a set of uniformly distributed stops is implied, with the first stop at 0.0 and the last stop at 1.0.

Source

fn transform(&self) -> Option<&Rc<dyn GradientTransform>>

The transform, if any, to apply to the gradient.

This transform is in addition to any other transformations applied to the canvas, but does not add any transformations to the canvas.

Source

fn create_shader( &self, rect: Rect, text_direction: Option<TextDirection>, ) -> Shader

Creates a Shader for this gradient to fill the given rect.

If the gradient’s configuration is text-direction-dependent, for example it uses AlignmentDirectional objects instead of Alignment objects, then the text_direction argument must not be null.

The shader’s transform will be resolved from the transform of this gradient.

Source

fn scale(&self, factor: f64) -> GradientRef

Returns a new gradient with its properties scaled by the given factor.

A factor of 0.0 (or less) should result in a variant of the gradient that is invisible; any two factors epsilon apart should be unnoticeably different from each other at first glance. From this it follows that scaling a gradient with values from 1.0 to 0.0 over time should cause the gradient to smoothly disappear.

Typically this is the same as interpolating from null (with lerp).

Source

fn with_opacity(&self, opacity: f64) -> GradientRef

Returns a new Gradient with each color set to the given opacity.

Source

fn as_any(&self) -> &(dyn Any + 'static)

Downcast support for is / as in Dart.

Provided Methods§

Source

fn from_color(&self, color: Color) -> GradientRef

Returns a copy of this gradient with all of its colors replaced by the given color.

The geometry of the gradient (such as its stops and the subclass-specific positioning) is preserved, so the result paints as a uniform color. This is useful to represent a solid color as a gradient, for example when interpolating between a color and a gradient with lerp.

Subclasses should override this method to preserve their own geometry. The base implementation returns a LinearGradient; this is sufficient because a gradient with uniform colors paints as a solid color regardless of its geometry.

Source

fn lerp_from( &self, a: Option<&(dyn Gradient + 'static)>, t: f64, ) -> Option<GradientRef>

Linearly interpolates from another Gradient to this.

When implementing this method in subclasses, return None if this class cannot interpolate from a. In that case, lerp will try a’s lerp_to method instead.

If a is None, this must not return None. The base class implements this by deferring to scale.

Instead of calling this directly, use lerp.

Source

fn lerp_to( &self, b: Option<&(dyn Gradient + 'static)>, t: f64, ) -> Option<GradientRef>

Linearly interpolates from this to another Gradient.

This is called if b’s lerp_to did not know how to handle this class.

When implementing this method in subclasses, return None if this class cannot interpolate from b. In that case, lerp will apply a default behavior instead.

If b is None, this must not return None. The base class implements this by deferring to scale.

Instead of calling this directly, use lerp.

Source

fn eq_gradient(&self, other: &(dyn Gradient + 'static)) -> bool

Field equality. Dart’s default == is identity; subclasses that override == implement this.

Trait Implementations§

Source§

impl PartialEq for dyn Gradient

Source§

fn eq(&self, other: &(dyn Gradient + 'static)) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§