Greetings from the distant future. Since the question leaves some details unknown and OP likely no longer needs the answer, I'm going to be a bit general in the response. I'm going to interpret this as roughly "how do I change one color to another". There are other answers that cover similar tasks, but I figured I'd point out a few things here first.
First should be obvious. The image has been horribly damaged by JPG compression. Depending on your goals, this may have already made the task impractical.
inpict = imread('popsquares.jpg');
[R G B] = imsplit(inpict);
mostlyred = (R > G) & (R > B);
Trying to do any sort of masking on an image that's this heavily damaged will be difficult, as features like the thin lines between the blocks are comparable in distinctness to the artifacts. While not all interpretations of the problem require masking, it's good to avoid unnecessary and conspicuous damage to your images.
So let's assume we have better control over our working images, and have avoided said damage. I'm going to include a couple quick examples. First, we need to construct a test image.
H = mod(hswing*rand(ncolored,1) - hswing/2,1);
S = (1-mins)*rand(ncolored,1) + mins;
ct = im2uint8(hsv2rgb([H S V]));
redpict = [ct; zeros(prod(tiling)-ncolored,3,'uint8')];
redpict = redpict(randperm(prod(tiling)),:);
redpict = permute(redpict,[4 3 2 1]);
redpict = repmat(redpict,[tilesz 1 1]);
redpict = padarray(redpict,[1 1 0 0]*padsz,0,'both');
redpict = imtile(redpict,tiling);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1003805/image.png)
The simple thing to do would be to simply rotate the hue of the image. This preserves the hue variation within the image and requires no masking, so the artifacts aren't a showstopper. The color model you use can make a significant difference, and note that hue angles between the primary-secondary corners aren't always 60 degrees. Operaing in HSV/HSL works, but apparent brightness is poorly preserved. Operating in CIELAB has good brightness preservation, but it tends to exaggerate hue swings near the blue corner, making things unexpectedly purple. OKLAB and SRLAB are attempts (in part) to reduce the issue.
adjustedpict = imtweak(redpict,'lchok',[1 1 0.65]);
Alternatively, you might not even want to preserve the hue variation. You could simply replace the hue entirely. This would still preserve the brightness and saturation variation.
cpict = colorpict(imsize(redpict,3),[0 135 240],'uint8');
blendpict = imblend(cpict,redpict,1,'hue');
But if what you really want is replacement with a solid color fill, then you'll have to come up with a means of creating a mask that describes the regions you want to fill.
[R G B] = imsplit(redpict);
mostlyred = (R > G) & (R > B);
filledpict = replacepixels([0 135 240]/255,redpict,mostlyred);
For brevity in these examples, I've used several functions (imtile, imtweak, colorpict, replacepixels) which are from MIMT (on the File Exchange). There are generally ways to avoid them, but they are convenient and succinct. For another example of changing colors in an image, try the reference answer in this thread. The answer there links to other references that may help depending on your specific needs. Some of the links also cover ways of doing similar sub-tasks (hue adjustment, composition, colorization) without the associated MIMT tools.