function imagex_color = colorsecstruct(imagex,offset,residue_locations,whichres,whattoplot,maxplot,minplot,zeroplot,plotsomething,plotlegend,square_width, legend_x, legend_y)

%hold off; image(imagex); hold on; axis equal; 
[xsize,ysize,zsize]=size(imagex);
axis([0 ysize 0 xsize]); zoomedin = 0;

numres = length(residue_locations);

if (nargin<6) maxplot = max(abs(whattoplot));end;
if (nargin<7) minplot = maxplot;end;

imagex_color = double(imagex);
count = 1;
%for k=whichres
for k=whichres(1):1:whichres(length(whichres))
%     k
%     offset
    if(k-offset <= size(residue_locations,2) && k-offset >= 1)
        x=residue_locations(1,k - offset);
        y=residue_locations(2,k - offset);
        %   h=rectangle('Position', ...
        %       [x - square_width/2, y - square_width/2, square_width,square_width]);
        colorplot = getcolor(whattoplot(count),maxplot,minplot, zeroplot);
        xbins = max(floor(y-square_width/2),1) :  min(floor(y+square_width/2),xsize); 
        ybins = max(floor(x-square_width/2),1) :  min(floor(x+square_width/2),ysize); 
        for n=1:3
            imagex_color(xbins,ybins,n) = double(imagex(xbins,ybins,n))*colorplot(n);
        end
%     else
%         'Can''t plot with k and offset of:\n'
%         k
%         offset
%         size(residue_locations)
    end
    count=count+1;
end

%for squares where there is no data 
for k=1:size(residue_locations,2)
    if (isempty(find(k+offset == whichres)))
        x=residue_locations(1,k);
        y=residue_locations(2,k);
        xbins = floor(y-square_width/2) :  min(floor(y+square_width/2),xsize); 
        ybins = floor(x-square_width/2) :  min(floor(x+square_width/2),ysize); 
        for n=1:3
            imagex_color(xbins,ybins,n) = double(imagex(xbins,ybins,n))*0.7;
        end
    end
end

%Make a "legend"
if (nargin<9) plotlegend = 1; end;

if (plotlegend)
    numlines = 10;
    sizebar = 4;

    x_offset = 15*square_width;
    if(nargin > 11) x_offset = round(legend_x) + round(sizebar*square_width); end
    
    for k=1:-(1/numlines):-1
        ybins = square_width:2*square_width;
        if(nargin > 12) ybins = round(legend_y):round(legend_y) + square_width; end
        xbins = x_offset + ...
            [round(sizebar*-1*k*square_width) : round(sizebar*-1*k*square_width)+sizebar*square_width/numlines];
        colorplot = getcolor(k, 1,1,0);
        for n=1:3
            imagex_color(xbins,ybins,n) = double(imagex(xbins,ybins,n))*colorplot(n);
        end
    end
end

if (nargin<8) plotsomething = 1; end;

if (plotsomething>0)
    hold off; image(imagex_color/256); hold on; axis equal
    if (plotlegend)
        k=-1;
        y_offset = 2*square_width;
        if(nargin > 12) y_offset = round(legend_y) + square_width; end

        [maxString, unused] = sprintf('%.1f', maxplot);
        [zeroString, unused] = sprintf('%.1f', zeroplot);
        [minString, unused] = sprintf('%.1f', minplot);

        if(minplot >= 0)
            text(y_offset,x_offset+round(sizebar*-1*k*square_width),['+',minString]);
        else
            text(y_offset,x_offset+round(sizebar*-1*k*square_width),[minString]);    
        end
        k=0;
        if(zeroplot >= 0)
            text(y_offset,x_offset+round(sizebar*-1*k*square_width),['+',zeroString]);
        else
            text(y_offset,x_offset+round(sizebar*-1*k*square_width),[zeroString]);    
        end
        k=+1;
        if(maxplot >= 0)
            text(y_offset,x_offset+round(sizebar*-1*k*square_width),['+',maxString]);
        else
            text(y_offset,x_offset+round(sizebar*-1*k*square_width),[maxString]);
        end
    end    
    hold off; axis off
end

function  colorplot = getcolor(colorvalue, maxplot,minplot, zeroplot);
if (colorvalue>zeroplot)
%    colorplot = [1, max(1-colorvalue/maxplot,0), max(1-colorvalue/maxplot,0)] ;
    colorplot = [1, max(1-abs((colorvalue-zeroplot)/(maxplot-zeroplot)),0), max(1-abs((colorvalue-zeroplot)/(maxplot-zeroplot)),0)] ;
else  
%    colorplot = [max(1+colorvalue/abs(minplot),0),  max(1+colorvalue/abs(minplot),0),1 ] ;
    colorplot = [max(1+(colorvalue-zeroplot)/abs(zeroplot-minplot),0),  max(1+(colorvalue-zeroplot)/abs(zeroplot-minplot),0),1 ] ;
end