Overlapping coloured circles - RGB circles

Hi!
I want to overlap three coloured circles, red, green, blue, in order to form the RGB palette, like below:
So far, i have the following code and result
k = 0 : 0.001 : 2*pi;
x = cos(k)+2.3; y = sin(k)+2;
z = cos(k)+3; w = sin(k)+1;
t = cos(k)+1.5; u = sin(k)+1;
fill(x,y, 'r', EdgeColor='none'),;
hold on
fill(z,w, 'b', EdgeColor='none');
fill(t,u, 'g', EdgeColor='none');
grid off
But I can't make the properly overlap and make them change the colours like above. Any sugestions, please?
Thank you!

 Accepted Answer

Try making a digital iamge so you can add the colors. What you were doing was stcking opaque patches.
% Demo to make 3 colored primary color circles to show how you can get white, and all 6 "Primary" colors.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
markerSize = 40;
imageSizeX = 512;
imageSizeY = 512;
radius = 181;
distanceFromCenter = 75;
% First make the red image.
centerX = 256;
centerY = 256 - distanceFromCenter;
redImage = CreateCircleImage(imageSizeX, imageSizeY, centerX, centerY, radius);
subplot(2, 2, 1);
% Now, display it.
image(redImage);
colormap([0 0 0; 1 1 1]);
title('Red Circle', 'FontSize', fontSize);
axis('on', 'image');
% Put up crosshairs.
yline(256, 'Color', 'r', 'LineWidth', 2);
xline(256, 'Color', 'r', 'LineWidth', 2);
% Next make the green image.
centerX = 256 + distanceFromCenter * cosd(225);
centerY = 256 - distanceFromCenter * sind(225);
greenImage = CreateCircleImage(imageSizeX, imageSizeY, centerX, centerY, radius);
subplot(2, 2, 2);
% Now, display it.
image(greenImage);
colormap([0 0 0; 1 1 1]);
title('Green Circle', 'FontSize', fontSize);
axis('on', 'image');
% Put up crosshairs.
yline(256, 'Color', 'g', 'LineWidth', 2);
xline(256, 'Color', 'g', 'LineWidth', 2);
% Next make the blue image.
centerX = 256 + distanceFromCenter * cosd(-45);
centerY = 256 - distanceFromCenter * sind(-45);
blueImage = CreateCircleImage(imageSizeX, imageSizeY, centerX, centerY, radius);
subplot(2, 2, 3);
% Now, display it.
image(blueImage);
colormap([0 0 0; 1 1 1]);
title('Blue Circle', 'FontSize', fontSize);
axis('on', 'image');
% Put up crosshairs.
yline(256, 'Color', 'b', 'LineWidth', 2);
xline(256, 'Color', 'b', 'LineWidth', 2);
% Combine the 3 separate color channels into one RGB image.
rgbImage = cat(3, redImage, greenImage, blueImage);
% If you want the surround to be white instead of black, do this:
mask = 255 * uint8(redImage == 0 & greenImage == 0 & blueImage == 0);
rgbImage = rgbImage + cat(3, mask, mask, mask);
subplot(2, 2, 4);
imshow(rgbImage);
title('All Colors', 'FontSize', fontSize);
axis('on', 'image');
% Put up red crosshairs
% yline(256, 'Color', 'r', 'LineWidth', 2);
% xline(256, 'Color', 'r', 'LineWidth', 2);
g = gcf;
g.WindowState = 'maximized';
%================================================================================================
function circlePixels = CreateCircleImage(imageSizeX, imageSizeY, centerX, centerY, radius)
% Create a logical image of a circle with specified
% diameter, center, and image size.
% First create the image.
[columnsInImage rowsInImage] = meshgrid(1:imageSizeX, 1:imageSizeY);
% Next create the circle in the image.
circlePixels = (rowsInImage - centerY).^2 ...
+ (columnsInImage - centerX).^2 <= radius.^2;
% circlePixels is a 2D "logical" array.
% Convert to a uint8 image
circlePixels = uint8(circlePixels) * uint8(255);
end

More Answers (1)

DGM
DGM on 27 Mar 2022
Edited: DGM on 27 Mar 2022
This creates an image:
sz = [512 512]; % image size
cim = sz.*[0.46 0.5]; % [y x] center of image
r = mean(sz)/4; % radius of circle
Pth = [90 210 330]; % position angle
Pr = r*2/3; % position radius
% calculate circle locations
cx = Pr*cosd(Pth) + cim(2);
cy = sz(1)-(Pr*sind(Pth) + cim(1));
% draw circles in an RGB image
outpict = zeros(sz);
for k = 1:numel(Pth)
outpict(:,:,k) = drawcircle(sz,[cy(k) cx(k)],r);
end
% show the result
imshow(outpict)
% draw circle with smooth edges
function circ = drawcircle(sz,c,r)
xx = 1:sz(2);
yy = (1:sz(1)).';
circ = sqrt((xx-c(2)).^2 + (yy-c(1)).^2);
circ = min(max((r-circ)/2,0),1);
end
I'm sure there are plenty of other ways.

Categories

Find more on Images 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!