• Apply a solid background color to an image.

    Creates a new canvas with the specified background color and composites the original image on top. Useful for replacing transparent backgrounds with solid colors.

    Parameters

    • imageCanvas: HTMLCanvasElement

      The image canvas (can have transparent areas)

    • color: [number, number, number, number]

      RGBA color values [red, green, blue, alpha] (0-255 range)

    Returns HTMLCanvasElement

    HTMLCanvasElement with the specified background color

    Throws

    When canvas context creation fails

    Example

    // Apply white background
    const withWhiteBg = applyBackgroundColor(cutout, [255, 255, 255, 255]);

    // Apply red background with 50% opacity
    const withRedBg = applyBackgroundColor(cutout, [255, 0, 0, 128]);

    // Apply transparent background (no change)
    const withTransparentBg = applyBackgroundColor(cutout, [0, 0, 0, 0]);