Skip to main content

BlendableSurface

Struct BlendableSurface 

Source
pub struct BlendableSurface<'s, S: AsRef<[[f32; 4]]> + AsMut<[[f32; 4]]>> { /* private fields */ }
Expand description

A surface that allows you to blend pixels rather than copying them onto each other.

The underlying Surface must use [f32; 4] pixels, such as crate::surface::Rgba32Surface

use blittle::{BlendMode, BlendableSurface, PositionI, Rgba32Surface, Size};

// Define a [f32; 4] surface.
let mut bottom = Rgba32Surface::new_filled(Size { width: 256, height: 256 }, [0.1, 0.3, 0.5, 0.9]);

// We will blend this surface on top of `bottom`.
let mut top = Rgba32Surface::new_filled(Size { width: 64, height: 120 }, [0.3, 0.95, 0.1, 1.]);
// Set the top surface's position relative to the bottom surface.
top.set_position(PositionI::new(1, 5), &bottom);
// Feed the top layer into a BlendableSurface.
let top = BlendableSurface::new(top);

// Blend.
let blend_mode = BlendMode::Overlay;
let alpha = 0.5;
top.blend(blend_mode, alpha, &mut bottom).unwrap();

A BlendableSurface can be locked or unlocked. A locked BlendableSurface usually blends faster but can’t mutate its underlying surface pixel buffer. When initially locked, a BlendableSurface caches the position of every non-transparent pixel. In the above example, locking top would be make blending slightly slower because the surface is filled with a uniform color. Locking is useful when some pixels are transparent and some are not.

Implementations§

Source§

impl<'s, S: AsRef<[[f32; 4]]> + AsMut<[[f32; 4]]>> BlendableSurface<'s, S>

Source

pub fn new(surface: Surface<'s, S, [f32; 4]>) -> Self

Source

pub const fn surface(&self) -> &Surface<'s, S, [f32; 4]>

Returns a reference to the underlying surface.

Source

pub const fn surface_mut( &mut self, ) -> Result<&mut Surface<'s, S, [f32; 4]>, Error>

Returns a mutable reference of the surface. Returns an error if the masked surface is locked.

Source

pub fn blend<B: AsRef<[[f32; 4]]> + AsMut<[[f32; 4]]>>( &self, blend_mode: BlendMode, alpha: f32, other: &mut Surface<'s, B, [f32; 4]>, ) -> Result<(), Error>

Blend into other using the blend_mode and an alpha transparency value (0-1).

The alpha value is applied to each pixel. A value of 0 means that other’s pixels will be the original (non-blended) pixels. A value of 1 means that other’s pixels will be the blended pixels. A value of 0.5 means that other’s pixels will be halfway between the original and blended pixels.

Source

pub fn lock(&mut self)

Available on crate feature std only.

Lock the surface, optimizing blit speed while preventing pixel manipulation.

Source

pub const fn is_locked(&self) -> bool

Available on crate feature std only.

Returns true if the surface is locked.

Source

pub fn unlock(&mut self)

Available on crate feature std only.

Unlock the surface. Blit speed will be unoptimized while pixel manipulation will be permitted.

Auto Trait Implementations§

§

impl<'s, S> Freeze for BlendableSurface<'s, S>
where S: Freeze,

§

impl<'s, S> RefUnwindSafe for BlendableSurface<'s, S>
where S: RefUnwindSafe,

§

impl<'s, S> Send for BlendableSurface<'s, S>
where S: Send,

§

impl<'s, S> Sync for BlendableSurface<'s, S>
where S: Sync,

§

impl<'s, S> Unpin for BlendableSurface<'s, S>
where S: Unpin,

§

impl<'s, S> UnsafeUnpin for BlendableSurface<'s, S>
where S: UnsafeUnpin,

§

impl<'s, S> UnwindSafe for BlendableSurface<'s, S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

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

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

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

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more