MATLAB: How does the ‘Erasemode’ property with ‘Xor’ value interact with the patches and background in MATLAB

MATLAB

I have a figure which contains two patches of different colors with the axis in a specific background color. When these two patches are overlaid on each other with 'Erasemode' property set to 'Xor', i observe a new color at the intersection of the two patches. I wish to understand the arithmetic behind the computation of this color and how the background is taken into consideration.
Please find the attached file 'trial.m'.

Best Answer

In the example script, trial.m, when the Red and Green patch intersect, the following arithmetic is used to compute the resultant color:
1. A bit-based XOR operation happens on the RGB values of Red and Background resulting in say 'xr'
2. Similar to step 1, a bit-based XOR operation happens on the RGB values of Green and Background resulting in say 'xg'
3. Finally a bit-based XOR operation happens on 'xg','xr' and background resulting in the intersection color. Please note that the 3 color can be XORed in any order to obtain the final intersection color.
The attached M-script 'TMW_sample.m' clearly shows how these patches interact with background during XOR erasemode operation.