Implement the functionality for the blend_pixel method in the GenericImage trait to handle alpha blending of pixels correctly. This method should blend the specified pixel's color with the existing pixel at the designated coordinates (x, y) by considering the alpha channel of both pixels involved in the blending process.
Specific Requirements:
Alpha Blending Calculation:
When blending two pixels, the resulting pixel's color (RGBA) should be computed based on the alpha values of both the existing pixel and the pixel to be blended.
The formula for blending should follow standard alpha compositing rules:
Let C1 be the color of the existing pixel, A1 its alpha, and C2 be the color of the incoming pixel, A2 its alpha.
The resulting color C and alpha A should be computed as:
text
A = A1 + A2 * (1 - A1)
C = (C1 * A1 + C2 * A2 * (1 - A1)) / A
Ensure to handle cases where the resulting alpha could be zero to avoid division by zero.
Input Validation:
The method must ensure that the (x, y) coordinates are within the bounds of the image before attempting to blend. If they are out of bounds, the method should panic as per the existing design of the trait.
Testing:
Create and pass tests that confirm the blending behavior matches expected outcomes. Your tests should include:
Blending of a fully opaque pixel with a transparent pixel.
Blending of two semi-transparent pixels.
Effects of color changes when blending with variations in alpha values.
Implement the blend_pixel method within the /app/ directory under the appropriate trait implementation for GenericImage.