alpha-blend 0.3.0

Alpha blending and compositing
Documentation
# alpha-blend

Alpha blending and compositing in (optionally) zero-dependency Rust.

[![Test](https://github.com/crates-lurey-io/alpha-blend/actions/workflows/test.yml/badge.svg)](https://github.com/crates-lurey-io/alpha-blend/actions/workflows/test.yml)
[![Docs](https://github.com/crates-lurey-io/alpha-blend/actions/workflows/docs.yml/badge.svg)](https://github.com/crates-lurey-io/alpha-blend/actions/workflows/docs.yml)
[![Crates.io Version](https://img.shields.io/crates/v/alpha-blend)](https://crates.io/crates/alpha-blend)
[![codecov](https://codecov.io/gh/crates-lurey-io/alpha-blend/graph/badge.svg?token=Z3VUWA3WYY)](https://codecov.io/gh/crates-lurey-io/alpha-blend)

There are three pieces:

- A `CompositeMode` decides **how much** of the source and destination survive. These are the
  Porter-Duff operators.
- A `BlendMode` decides **how** their colors are mixed. These are the W3C separable blend modes.
- A `Composite` pairs the two. Either half can be used on its own, in which case the other
  takes its default.

```rust
use alpha_blend::{BlendMode, Composite, CompositeMode, RgbaBlend, rgba::U8x4Rgba};

let src = U8x4Rgba::new(255, 128, 0, 128);
let dst = U8x4Rgba::new(0, 64, 255, 255);

// Plain source-over.
let out = CompositeMode::SourceOver.apply(src, dst);

// Multiply, composited source-over.
let out = Composite::new(BlendMode::Multiply, CompositeMode::SourceOver).apply(src, dst);
```

Everything is generic over the `Channel` trait, so `u8` and `f32` pixels go through the same
code at their own precision. Integer channels round to nearest, once per operation, from a
widened intermediate: `scale(255, 255) == 255`, and repeated compositing does not drift darker.

## Examples

```sh
cargo run --example porter-duff --features bytemuck
```

A blue square and a red square, each 50% transparent, overlapping in the middle.

### Compositing operators

| | | |
|---|---|---|
| `Clear`<br>![]examples/out/composite_Clear.png | `Source`<br>![]examples/out/composite_Source.png | `Destination`<br>![]examples/out/composite_Destination.png |
| `SourceOver`<br>![]examples/out/composite_SourceOver.png | `DestinationOver`<br>![]examples/out/composite_DestinationOver.png | `SourceIn`<br>![]examples/out/composite_SourceIn.png |
| `DestinationIn`<br>![]examples/out/composite_DestinationIn.png | `SourceOut`<br>![]examples/out/composite_SourceOut.png | `DestinationOut`<br>![]examples/out/composite_DestinationOut.png |
| `SourceAtop`<br>![]examples/out/composite_SourceAtop.png | `DestinationAtop`<br>![]examples/out/composite_DestinationAtop.png | `Xor`<br>![]examples/out/composite_Xor.png |
| `Plus`<br>![]examples/out/composite_Plus.png | | |

### Blend modes

| | | |
|---|---|---|
| `Normal`<br>![]examples/out/blend_Normal.png | `Multiply`<br>![]examples/out/blend_Multiply.png | `Screen`<br>![]examples/out/blend_Screen.png |
| `Overlay`<br>![]examples/out/blend_Overlay.png | `Darken`<br>![]examples/out/blend_Darken.png | `Lighten`<br>![]examples/out/blend_Lighten.png |
| `ColorDodge`<br>![]examples/out/blend_ColorDodge.png | `ColorBurn`<br>![]examples/out/blend_ColorBurn.png | `HardLight`<br>![]examples/out/blend_HardLight.png |
| `SoftLight`<br>![]examples/out/blend_SoftLight.png | `Difference`<br>![]examples/out/blend_Difference.png | `Exclusion`<br>![]examples/out/blend_Exclusion.png |

The four non-separable modes (`Hue`, `Saturation`, `Color`, `Luminosity`) are not implemented;
they operate on the whole RGB triple rather than one channel at a time.

## Contributing

This project uses [`just`][] to run commands the same way as the CI:

- `cargo just check` to check formatting and lints.
- `cargo just coverage` to generate and preview code coverage.
- `cargo just doc` to generate and preview docs.
- `cargo just test` to run tests.

[`just`]: https://crates.io/crates/just

For a full list of commands, see the [`Justfile`](./Justfile).