patches and overlapping alpha values
Show older comments
Hello,
I wrote code that creates two patch objects which overlap each other. Basically, two circles, one (circle A) at the center of the other (circle B). Both have a face value of .35. Circle A is red and circle B is blue. However, when I set’ facealpha’ property while using the patch command, the colors add up, meaning that circle B is the alpha blue that I want, but circle A is an alpha purple (the alpha red and alpha blue added together). Is there a way to get circle A looking red again? My original plan is to "cut out" circle A then patch circle B, followed by patching circle A.
9 Comments
Ryan
on 1 Jun 2012
Are you asking how to only change the facealpha for circle B and not circle A? If you adjust both alpha values, then by the nature of transparency, the red circle will always seem purple as long as the blue is behind it. You could create a white circle the same size as circle A (or whatever background color you're using) and place it behind circle A to give the appearance of two separate semi-transparent images.
Matthew
on 1 Jun 2012
Walter Roberson
on 1 Jun 2012
Transparency _always_ adds colors, by definition. If there is something blue (whether it is transparent or not) behind the transparent red circle, then the graphics is going to make that area purple.
Ryan
on 2 Jun 2012
A red transparent circle on an equal sized solid white circle both on a large transparent blue circle should generate what I believe you're asking for. You could also try subtracting the red circle area from the blue circle so you don't get the color wheel action (aka red + blue = purple).
Matthew
on 5 Jun 2012
Walter Roberson
on 5 Jun 2012
If the two overlap each other and you are using transparency then by definition you must get color mixing. If that isn't what you want, then do not use transparency or else do not allow the two to overlap.
Oleg Komarov
on 5 Jun 2012
I think the easiest is to NOT use transparency and pick the color shade such that is has the same tone as the transparent one.
Ryan
on 5 Jun 2012
The blue element is being placed on top of the other two elements. if you view just the blue circle and the non-transparent white circle, you'll note it still has a blue hue. I am unsure of how to order patches or if it is even possible.
Matthew
on 6 Jun 2012
Answers (1)
Ayush
on 19 Jun 2025
Hi Matthew,
When two semi-transparent patches overlap, the colors blend according to the alpha compositing rules. In your case, resulting in purple where the red (Circle A) overlaps with the blue (Circle B).
To keep Circle A red (without the purple blending effect), you'll need to make sure that Circle B does not draw underneath Circle A at all, which means effectively cutting a hole in Circle B.
Here's how you can do it in MATLAB:
You can use polygon clipping to subtract the area of Circle A from Circle B before patching them. This creates a donut-shaped Circle B that surrounds but does not underlie Circle A.
Here is a MATLAB code for your reference:
r1 = 1; % Radius of circle A (inner red)
r2 = 2; % Radius of circle B (outer blue)
center = [0, 0];
theta = linspace(0, 2*pi, 100);
% Define circle A (red) and circle B (blue)
xA = center(1) + r1 * cos(theta);
yA = center(2) + r1 * sin(theta);
polyA = polyshape(xA, yA);
xB = center(1) + r2 * cos(theta);
yB = center(2) + r2 * sin(theta);
polyB = polyshape(xB, yB);
% Now, subtract A from B to make a ring (annulus)
ring = subtract(polyB, polyA);
% Now, plot the ring (circle B with a hole)
p1 = plot(ring, 'FaceColor', 'blue', 'FaceAlpha', 0.35);
hold on;
% finally, plot circle A on top
p2 = plot(polyA, 'FaceColor', 'red', 'FaceAlpha', 0.35);
axis equal;
You can read more about "polyshape" function here:https://www.mathworks.com/help/matlab/ref/polyshape.html
Hope it helps!
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!