Deriving maximum and minimum RGB values from sine wave grating

Dear All,

I have a cyan-magenta (isoluminant) sinusoidal grating generated in Psychtoolbox with a matrix of RGB values across the wave. What I need is to find the RGB values at the maximum and minimum phases (magenta and cyan phases - peak and the dip of the wave) of the grating. How can I derive these values from the matrix?

P.S.
I did not use a cLUT. Here is how I created the heating with magenta and cyan phases.

[X, y] = meshgrid(0:511, 1:height);
imageMatrix = cos(freq*2*pi*X+randomPhase+phaseDiff);
widthOfGrid =height;
widthOfGrid2=512;

grating(:,:,1) = gray + (gray * imageMatrix*contrast); % Red: 0-to-255
grating(:,:,2) = gray - (gray * imageMatrix*contrast); % Green: 0-to-255
grating(:,:,3) = repmat(gray,size(imageMatrix)); % Blue: 127.5


   
for k = 1:(widthOfGrid)
    for z = 1:(widthOfGrid2)
        grating(k,z,2) = ((grating(k,z,2) * (g_diff)) + ( white * g_min)); 
    end
end


for k = 1:(widthOfGrid)
    for z = 1:(widthOfGrid2)
        grating(k,z,1) = ((grating(k,z,1) * (r_diff)) + ( white * r_min)); 
    end
end

Many thanks!

Find the min and the max value in one row of image matrix, that tells you in which columns of the grating variable to look