Documentation
¶
Overview ¶
Package blend implements advanced separable and non-separable blend modes.
This file implements advanced blend modes beyond Porter-Duff compositing, following the W3C Compositing and Blending Level 1 specification.
Separable blend modes operate on each color channel independently. Non-separable blend modes require color space conversions (HSL/HSV).
References:
- W3C Compositing and Blending Level 1: https://www.w3.org/TR/compositing-1/
- PDF Blend Modes: Addendum (ISO 32000-1:2008)
Package blend provides color blending operations.
Package blend implements layer system for isolated drawing surfaces with compositing.
Package blend implements Porter-Duff compositing operators and blend modes.
All blend operations work with premultiplied alpha values in the range 0-255. This follows the WebGPU and modern graphics conventions for efficient compositing.
References:
- Porter-Duff: "Compositing Digital Images" (1984)
- W3C Compositing and Blending Level 1: https://www.w3.org/TR/compositing-1/
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BlendFunc ¶ added in v0.3.0
BlendFunc is the signature for blend operations. All values are premultiplied alpha, 0-255. Parameters:
- sr, sg, sb, sa: source color (red, green, blue, alpha)
- dr, dg, db, da: destination color (red, green, blue, alpha)
Returns: resulting color (r, g, b, a) after blending.
func GetBlendFunc ¶ added in v0.3.0
GetBlendFunc returns the blend function for the given mode. Returns blendSourceOver for unknown modes.
type BlendMode ¶ added in v0.3.0
type BlendMode uint8
BlendMode represents a Porter-Duff compositing operation.
const ( // Separable blend modes BlendMultiply BlendMode = iota + 14 // Result: S * D BlendScreen // Result: 1 - (1-S)*(1-D) BlendOverlay // HardLight with swapped layers BlendDarken // min(S, D) BlendLighten // max(S, D) BlendColorDodge // D / (1 - S) BlendColorBurn // 1 - (1 - D) / S BlendHardLight // Multiply or Screen depending on source BlendSoftLight // Soft version of HardLight BlendDifference // |S - D| BlendExclusion // S + D - 2*S*D // Non-separable blend modes (optional) BlendHue // Hue of source, saturation and luminosity of backdrop BlendSaturation // Saturation of source, hue and luminosity of backdrop BlendColor // Hue and saturation of source, luminosity of backdrop BlendLuminosity // Luminosity of source, hue and saturation of backdrop )
Advanced separable blend modes (extend BlendMode enum)
const ( // Porter-Duff modes (standard compositing operators) BlendClear BlendMode = iota // Result: 0 (clear destination) BlendSource // Result: S (replace with source) BlendDestination // Result: D (keep destination) BlendSourceOver // Result: S + D*(1-Sa) [default] BlendDestinationOver // Result: S*(1-Da) + D BlendSourceIn // Result: S*Da BlendDestinationIn // Result: D*Sa BlendSourceOut // Result: S*(1-Da) BlendDestinationOut // Result: D*(1-Sa) BlendSourceAtop // Result: S*Da + D*(1-Sa) BlendDestinationAtop // Result: S*(1-Da) + D*Sa BlendXor // Result: S*(1-Da) + D*(1-Sa) BlendPlus // Result: S + D (clamped to 255) BlendModulate // Result: S*D (multiply) )
type Layer ¶ added in v0.3.0
type Layer struct {
// contains filtered or unexported fields
}
Layer represents an isolated drawing surface with blend mode and opacity.
Layer provides a buffer for drawing operations that can be composited onto a parent layer using a specific blend mode and opacity. Layers form a stack, allowing for nested compositing operations.
Thread safety: Layer is not safe for concurrent access. External synchronization is required if multiple goroutines access the same layer.
func NewLayer ¶ added in v0.3.0
func NewLayer(blendMode BlendMode, opacity float64, bounds Bounds, pool *image.Pool) (*Layer, error)
NewLayer creates a new layer with the specified blend mode, opacity, and bounds. The buffer is allocated from the pool for efficient memory reuse. Opacity must be in range [0.0, 1.0] where 0 is fully transparent and 1 is fully opaque.
func (*Layer) SetOpacity ¶ added in v0.3.0
SetOpacity sets the layer's opacity, clamped to [0.0, 1.0].
type LayerStack ¶ added in v0.3.0
type LayerStack struct {
// contains filtered or unexported fields
}
LayerStack manages a stack of layers for nested compositing operations.
LayerStack provides push/pop semantics for creating temporary drawing surfaces that can be composited back onto the parent layer. The base layer represents the final output surface.
Thread safety: LayerStack is not safe for concurrent access. External synchronization is required if multiple goroutines access the same stack.
func NewLayerStack ¶ added in v0.3.0
func NewLayerStack(base *image.ImageBuf, pool *image.Pool) *LayerStack
NewLayerStack creates a new layer stack with the given base image buffer. The base buffer represents the final output surface. If pool is nil, the default pool is used for layer allocation.
func (*LayerStack) Clear ¶ added in v0.3.0
func (s *LayerStack) Clear()
Clear pops all layers and returns their buffers to the pool.
func (*LayerStack) Current ¶ added in v0.3.0
func (s *LayerStack) Current() *image.ImageBuf
Current returns the current drawing target (top layer or base). Returns the base image if the stack is empty.
func (*LayerStack) CurrentBlendMode ¶ added in v0.3.0
func (s *LayerStack) CurrentBlendMode() BlendMode
CurrentBlendMode returns the blend mode of the current layer. Returns BlendSourceOver if the stack is empty.
func (*LayerStack) Depth ¶ added in v0.3.0
func (s *LayerStack) Depth() int
Depth returns the number of layers in the stack (not including base).
func (*LayerStack) Pop ¶ added in v0.3.0
func (s *LayerStack) Pop() *image.ImageBuf
Pop removes and composites the top layer onto the parent layer or base. Returns the composited buffer (either parent layer or base). Returns nil if the stack is empty.
type Mode ¶
type Mode int
Mode represents a blending mode.
const ( // ModeSourceOver is the default alpha blending mode. ModeSourceOver Mode = iota // ModeSourceCopy replaces the destination with the source. ModeSourceCopy // ModeDestinationOver draws destination over source. ModeDestinationOver // ModeDestinationIn keeps destination where source is opaque. ModeDestinationIn // ModeDestinationOut keeps destination where source is transparent. ModeDestinationOut )