PIXELBANKv9.1.0
Menu

Composite a foreground image over a background using an alpha matte.

Alpha compositing is the fundamental operation for combining images with transparency. The compositing equation blends foreground and background based on the alpha (opacity) value:

C=α⋅F+(1−α)⋅BC = \alpha \cdot F + (1 - \alpha) \cdot B

where:

  • CC is the composited output color
  • FF is the foreground color
  • BB is the background color
  • α\alpha is the alpha/opacity value in [0, 1]

For each RGB channel:

  • α=1\alpha = 1: fully foreground (opaque)
  • α=0\alpha = 0: fully background (transparent)
  • α=0.5\alpha = 0.5: 50% blend of both

This operation is applied independently to each color channel.

Example:

Input:
composite((255, 0, 0), (0, 0, 255), 0.5)
Output:
(128, 0, 128)
Reasoning:

Compositing red foreground over blue background at 50% opacity:

  • R: 0.5 × 255 + 0.5 × 0 = 127.5 → 128
  • G: 0.5 × 0 + 0.5 × 0 = 0 → 0
  • B: 0.5 × 0 + 0.5 × 255 = 127.5 → 128 Result: purple (128, 0, 128)

Constraints:

  • foreground: RGB tuple of integers [0, 255]
  • background: RGB tuple of integers [0, 255]
  • alpha: float in range [0, 1]
  • Return composited RGB as tuple of integers (rounded)
solution.py

Test Results

0/0
Run code to see test results.