silk.color.rgba.
Red, green, blue, and alpha channels with straight or premultiplied alpha, over any number type.
When to use#
Use Rgba for a color whose channels are not yet weighted by alpha, as authored in CSS, SVG,
or a color picker. Use PremultipliedRgba for a color whose channels are already multiplied
by alpha, which is what blending, filtering, and GPU pipelines consume. premultiply and
unpremultiply convert between them.
Details#
Both types are Copy structs with public r, g, b, and a fields, generic over the
channel type T, usually f32 or f64 in [0, 1]. The types do not record which color space
or transfer function the channels use: a value can hold gamma-encoded sRGB or linear light. Keep
that choice consistent, and use silk.color.srgb to convert between the two encodings before
premultiplying. &a == &b compares every channel when T: Equal.
Interpolation and compositing are defined on PremultipliedRgba only, because interpolating
straight-alpha channels lets a transparent endpoint's color bleed into the result.
Gotchas#
unpremultiply of a fully transparent color returns zero channels: the color of a transparent
pixel is lost by premultiplication.
Examples#
Composite a translucent color over an opaque one#
import silk.color.rgba { Rgba }
pub fn main() -> i32 {
let red = Rgba<f64>.new(1.0, 0.0, 0.0, 0.5).premultiply()
let blue = Rgba<f64>.new(0.0, 0.0, 1.0, 1.0).premultiply()
let result = red.over(blue).unpremultiply()
if result.r != 0.5 || result.b != 0.5 || result.a != 1.0 {
return 1
}
return 0
}See also#
Use silk.color.color.Color to convert between color spaces, mix colors, and measure contrast.
Import as Rgba with import silk.color.rgba { Rgba }.
Public declarations: 2.
Rgba#
pub struct Rgba<T>A color with straight (not premultiplied) alpha, as red, green, blue, and alpha channels of type
T.
When to use#
Use Rgba for authored or stored colors, such as a CSS value or a gradient stop, and for any
color whose channels you edit directly. Call premultiply before compositing or
interpolating.
Details#
The fields are public. With a float T, channels are usually in [0, 1]; values outside it
represent colors outside the gamut of the space and survive arithmetic unchanged. The type does
not record the color space or transfer function of the channels. &a == &b compares every
channel when T: Equal.
Field r#
pub r: TThe red channel.
Field g#
pub g: TThe green channel.
Field b#
pub b: TThe blue channel.
Field a#
pub a: TThe opacity: 0 is fully transparent and 1 is fully opaque, for a float T.
Implementation Copy for Rgba<T>#
impl<T: Copy> Copy for Rgba<T>Implementation Rgba<T>#
impl<T> Rgba<T>Associated function new#
pub fn new(r: T, g: T, b: T, a: T) -> Rgba<T>Returns the straight-alpha color with channels r, g, b, and alpha a.
Method map#
pub fn map<U>(self: Self, transform: mut fn(T) -> U) -> Rgba<U>Returns a color whose four channels are transform applied to r, g, b, then a.
When to use#
Use map to change the channel type, such as from f64 to f32 for a GPU buffer.
Implementation Rgba<T>#
impl<T: Copy> Rgba<T>Method channels#
pub fn channels(self: Self) -> [T; 4]Returns the channels as the array [r, g, b, a].
Method withAlpha#
pub fn withAlpha(self: Self, a: T) -> Rgba<T>Returns the color with the same red, green, and blue channels and alpha a.
Implementation Rgba<T>#
impl<T: Copy + One> Rgba<T>Associated function opaque#
pub fn opaque(r: T, g: T, b: T) -> Rgba<T>Returns the fully opaque color with channels r, g, and b.
Implementation Rgba<T>#
impl<T: Zero> Rgba<T>Associated function transparent#
pub fn transparent() -> Rgba<T>Returns the fully transparent color whose four channels are zero.
Implementation Rgba<T>#
impl<T: Copy + Multiply<T, T>> Rgba<T>Method premultiply#
pub fn premultiply(self: Self) -> PremultipliedRgba<T>Returns the color with its red, green, and blue channels multiplied by alpha.
Details#
Alpha is copied unchanged. Premultiplication is exact in floating point for alpha 0 and 1.
For gamma-encoded channels, decode to linear light first when the result feeds linear-light
blending.
Implementation Equal for Rgba<T>#
impl<T: Equal> Equal for Rgba<T>Operation equals#
fn equals(left: &Self, right: &Self) -> boolPremultipliedRgba#
pub struct PremultipliedRgba<T>A color with premultiplied alpha: red, green, and blue channels of type T already multiplied
by the alpha channel.
When to use#
Use PremultipliedRgba for compositing, interpolation, filtering, and GPU uniforms. In this
form over and lerp are plain weighted sums, and a transparent color cannot tint its
neighbors. Call unpremultiply to recover straight channels.
Details#
The fields are public. For a valid color with a float T, each of r, g, and b is between
zero and a. The type does not record the color space or transfer function of the channels.
&a == &b compares every channel when T: Equal.
Field r#
pub r: TThe red channel multiplied by alpha.
Field g#
pub g: TThe green channel multiplied by alpha.
Field b#
pub b: TThe blue channel multiplied by alpha.
Field a#
pub a: TThe opacity: 0 is fully transparent and 1 is fully opaque, for a float T.
Implementation Copy for PremultipliedRgba<T>#
impl<T: Copy> Copy for PremultipliedRgba<T>Implementation PremultipliedRgba<T>#
impl<T> PremultipliedRgba<T>Associated function new#
pub fn new(r: T, g: T, b: T, a: T) -> PremultipliedRgba<T>Returns the premultiplied color with channels r, g, b, and alpha a.
Gotchas#
The channels are stored as given. Pass values already multiplied by a, or build the color
with Rgba.premultiply instead.
Method map#
pub fn map<U>(self: Self, transform: mut fn(T) -> U) -> PremultipliedRgba<U>Returns a color whose four channels are transform applied to r, g, b, then a.
When to use#
Use map to change the channel type, such as from f64 to f32 for a GPU buffer.
Implementation PremultipliedRgba<T>#
impl<T: Copy> PremultipliedRgba<T>Method channels#
pub fn channels(self: Self) -> [T; 4]Returns the channels as the array [r, g, b, a].
Implementation PremultipliedRgba<T>#
impl<T: Zero> PremultipliedRgba<T>Associated function transparent#
pub fn transparent() -> PremultipliedRgba<T>Returns the fully transparent color whose four channels are zero.
Implementation PremultipliedRgba<T>#
impl<T: Copy + Zero + Equal + Divide<T, T>> PremultipliedRgba<T>Method unpremultiply#
pub fn unpremultiply(self: Self) -> Rgba<T>Returns the straight-alpha color, dividing red, green, and blue by alpha.
Gotchas#
When alpha is zero the result is the transparent color with zero channels, because a transparent premultiplied color carries no hue.
Implementation PremultipliedRgba<T>#
impl<T: Copy + One + Add<T, T> + Subtract<T, T> + Multiply<T, T>> PremultipliedRgba<T>Method over#
pub fn over(self: Self, below: PremultipliedRgba<T>) -> PremultipliedRgba<T>Returns self composited over below with the Porter-Duff source-over operator.
Details#
Each channel is self + below * (1 - self.a), including alpha. Both colors must use the same
encoding; composite linear-light channels for physically correct results, or gamma-encoded
channels to match CSS and most browsers.
Method scale#
pub fn scale(self: Self, factor: T) -> PremultipliedRgba<T>Returns every channel multiplied by factor, which fades the color toward transparent.
When to use#
Use scale to apply a layer opacity or a coverage value to a premultiplied color.
Method lerp#
pub fn lerp(self: Self, other: PremultipliedRgba<T>, t: T) -> PremultipliedRgba<T>Returns self + (other - self) * t for every channel: self at t = 0 and other at t =
1.
Details#
Interpolating premultiplied channels keeps a transparent endpoint from tinting the result, as
CSS gradients and color-mix() require. The channels interpolate in whatever encoding they
hold.
Implementation Equal for PremultipliedRgba<T>#
impl<T: Equal> Equal for PremultipliedRgba<T>Operation equals#
fn equals(left: &Self, right: &Self) -> boolCategory: Color.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.