I am trying to emulate a similar feature to that of RMXP's "blend_type" for the Sprite class, and I can't seem to get it quite right. I was curious, does anyone know the calculation that is performed to do the addition and subtraction blends on an image? I'm using C#, and have been getting familiar with the ColorMatrix class, have created functions for altering brightness, saturation, color blending, opacity, color shearing, contrast, etc., but fail at this seemingly simple function.
I am using for creating a small utility for RMXP, and it is kinda important that it is accurate to the method that RMXP uses. This is basically the final thing to implement, and I can finish up pretty quickly after that.
Here's a description of how PhotoShop does it...
Add and Subtract blending modes
The Add and Subtract blending modes are available only for the Apply Image and Calculations commands.
Add
Adds the pixel values in two channels. This is a good way to combine non-overlapping images in two channels.
Because higher pixel values represent lighter colors, adding channels with overlapping pixels lightens the image. Black areas in both channels remain black (0 + 0 = 0). White in either channel results in white (255 + any value = 255 or greater).
Add mode divides the sum of the pixel values by the Scale amount, and then adds the Offset value to the sum. For example, to find the average of the pixels in two channels, add them, divide by 2, and enter no Offset value.
The Scale factor may be any number between 1.000 and 2.000. Entering a higher Scale value darkens the image.
The Offset value lets you lighten or darken the pixels in the destination channel by any brightness value between +255 and –255. Negative values darken the image; positive values lighten the image.
Subtract
Subtracts the pixel values in the source channel from the corresponding pixels in the target channel. As with Add mode, the result is then divided by the Scale factor and added to the Offset value.
The Scale factor may be any number between 1.000 and 2.000. The Offset value lets you lighten or darken the pixels in the destination channel by any brightness value between +255 and –255.
I can only assume that RM applies it's own "Scaling Factor" & "Offset" to keep the combinations from getting too light or dark.
Try a couple tests with primary colors to determine the factors. (e.g. make a 50% pure red map, and add a 50% pure green sprite to it with both ADD & SUBTRACT modes. Capture the resulting image & analyze the pixels.)
It's fairly simple :
Add :
buffer_pixel.red += sprite_pixel.red * sprite_pixel.alpha / 255 * sprite.opacity / 255
buffer_pixel.green += sprite_pixel.green * sprite_pixel.alpha / 255 * sprite.opacity / 255
buffer_pixel.blue += sprite_pixel.blue * sprite_pixel.alpha / 255 * sprite.opacity / 255
And results should be maintened between 0 and 255.
Substract :
same with -=
Blending is not something we can apply to the bitmap, we use it when merging all the sprites for the display.
Anyway I don't know wich library you use but usally you have builtin functions for that.
wouldn't the subtract be the same as the add (with the sign changed)???
i.e. r = destColor.R - (int)(srcColor.R * alpha);
if your srcColor.A is 255, then alpha = 1. (1 - alpha) = 0
I don't see the prototype for a GDI+ COLOR object, but isn't color usually returned as (r, g, b, a) ??
and I still don't see any "scaling". The inherent problem with just adding or subtracting is you end up with too many values
less than 0, or greater than 255. Clamp only compresses 'out of limit' values to 0 or 255.
If you have a pixel that's 50% green, and you add 50% green to it, should you get 100% green?
Or, should you get 50% MORE green, resulting in 75% green?