patches and overlapping alpha values

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

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.
I want both circles to be transparent. I plot circle B then circle A. I tried making circle A a white circle with no alpha value. Circle A is the same blue as circle B (only not transparent – there are grid lines on the axes and they do not show up behind circle A while they show up behind circle B).
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.
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).
right now my code looks like this:
theta=0:pi/100:2*pi;
x=cos(theta);
x2=5*x;
y=sin(theta);
y2=5*y;
hold on
grid on
% create the blue circle
patch(x2,y2,[0,0,1],'facealpha',.35)
% create a white circle on top of the blue
patch(x,y,[1,1,1])
%fill in the white circle with red
patch(x,y,[1,0,0],'facealpha',.35)
I am still getting the purple circle in the blue circle >.< Is there a way I can plot both circles then fill in the color?
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.
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.
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.
I ended up find a color that would simulate the light blue and red I was looking for, then plotted grid lines using Xtick and Ytick to get the effect I was looking for. Thanks to everyone for their advice and help.

Sign in to comment.

Answers (1)

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

Asked:

on 1 Jun 2012

Answered:

on 19 Jun 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!