function [T2,N] = CA_A2(T1,white, black) 
%CA_A2 boji bijele nnn komponente razlicitim bojama tako sto za svaku ne crnu celiju bira najvecu ne bijelu i ne crnu boju iz nnn susjedstva
%CA_A2 marks all cells, different from black (not a member of potential infinite cluster) and not having black or white nnn-connected neighbour,
%with a maximal color in nnn-neighborhood that is not white or black.

T2=T1;
T3=zeros(size(T1));
N=0;
global cmap;

for i=1:10000
for lines=2:size(T1,1)-1
    for columns=2:size(T1,2)-1
            if (T1(lines, columns) < black) % not black (or green) cell
                if (lines ~= 1 && lines ~= size(T1,1)-1  && columns ~= 1 && columns ~= size(T1,2)-1  ) %not on boundaries
%                     nestoDrugo = false; %nestoDrugo? pamti da li u okolini ne crne celije imamo celiju koja nije ni crna ni bijela
                    if ((T1(lines, columns-1)~= black && T1(lines, columns-1)~= white) || (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) || ...%W,N
                       (T1(lines, columns+1)~= black && T1(lines, columns+1)~= white) || (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) || ...%E,S
                       (T1(lines-1, columns-1)~= black && T1(lines-1, columns-1)~= white) || (T1(lines+1, columns-1)~= black && T1(lines+1, columns-1)~= white) || ...%WN,WS
                       (T1(lines-1, columns+1)~= black && T1(lines-1, columns+1)~= white) || (T1(lines+1, columns+1)~= black && T1(lines+1, columns+1)~= white)) %EN,ES
%                        nestoDrugo = true;
%                     end
                        max_C=[];
%                     if (nestoDrugo==true)
                        if (T1(lines, columns-1)~= black && T1(lines, columns-1)~= white) max_C=[max_C, T1(lines, columns-1)]; end  %W
                        if (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) max_C=[max_C, T1(lines-1, columns)]; end %N
                        if (T1(lines, columns+1)~= black && T1(lines, columns+1)~= white) max_C=[max_C, T1(lines, columns+1)];end %E
                        if (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) max_C=[max_C, T1(lines+1, columns)]; end %S
                        if (T1(lines-1, columns-1)~= black && T1(lines-1, columns-1)~= white) max_C=[max_C, T1(lines-1, columns-1)]; end %WN
                        if (T1(lines+1, columns-1)~= black && T1(lines+1, columns-1)~= white) max_C=[max_C, T1(lines+1, columns-1)]; end %WS
                        if (T1(lines-1, columns+1)~= black && T1(lines-1, columns+1)~= white) max_C=[max_C, T1(lines-1, columns+1)]; end %EN
                        if (T1(lines+1, columns+1)~= black && T1(lines+1, columns+1)~= white) max_C=[max_C, T1(lines+1, columns+1)]; end %ES

                        if (max_C ~= 0) T2(lines, columns)= max(max_C); end
                    end
                elseif (columns == 1) && (lines > 1 && lines < size(T1,1)-1 ) %left boundary  without corners
%                     nestoDrugo = false; %nestoDrugo? pamti da li u okolini ne crne celije imamo celiju koja nije ni crna ni bijela
                    if ((T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) || ... %N
                       (T1(lines, columns+1)~= black && T1(lines, columns+1)~= white) || (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) || ... %E,S
                       (T1(lines-1, columns+1)~= black && T1(lines-1, columns+1)~= white) || (T1(lines+1, columns+1)~= black && T1(lines+1, columns+1)~= white)) %EN,ES
%                        nestoDrugo = true;
%                     end
                        max_C=[];
%                     if (nestoDrugo==true)
                        if (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) max_C=[max_C, T1(lines-1, columns)]; end %N
                        if (T1(lines, columns+1)~= black && T1(lines, columns+1)~= white) max_C=[max_C, T1(lines, columns+1)];end %E
                        if (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) max_C=[max_C, T1(lines+1, columns)]; end %S
                        if (T1(lines-1, columns+1)~= black && T1(lines-1, columns+1)~= white) max_C=[max_C, T1(lines-1, columns+1)]; end %EN
                        if (T1(lines+1, columns+1)~= black && T1(lines+1, columns+1)~= white) max_C=[max_C, T1(lines+1, columns+1)]; end %ES

                        if (max_C ~= 0) T2(lines, columns)= max(max_C); end
                    end
                elseif (columns == size(T1,2)-1 ) && (lines > 1 && lines < size(T1,1)-1 ) %right boundary without corners
%                     nestoDrugo = false; %nestoDrugo? pamti da li u okolini ne crne celije imamo celiju koja nije ni crna ni bijela
                    if ((T1(lines, columns-1)~= black && T1(lines, columns-1)~= white) || (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) || ...%W,N
                       (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) || ...%S
                       (T1(lines-1, columns-1)~= black && T1(lines-1, columns-1)~= white) || (T1(lines+1, columns-1)~= black && T1(lines+1, columns-1)~= white)) %WN,WS
%                        nestoDrugo = true;
%                     end
                        max_C=[];
%                     if (nestoDrugo==true)
                        if (T1(lines, columns-1)~= black && T1(lines, columns-1)~= white) max_C=[max_C, T1(lines, columns-1)]; end  %W
                        if (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) max_C=[max_C, T1(lines-1, columns)]; end %N
                        if (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) max_C=[max_C, T1(lines+1, columns)]; end %S
                        if (T1(lines-1, columns-1)~= black && T1(lines-1, columns-1)~= white) max_C=[max_C, T1(lines-1, columns-1)]; end %WN
                        if (T1(lines+1, columns-1)~= black && T1(lines+1, columns-1)~= white) max_C=[max_C, T1(lines+1, columns-1)]; end %WS

                        if (max_C ~= 0) T2(lines, columns)= max(max_C); end
                    end
                %corners    
                elseif (columns == 1) && (lines == 1) %NW corner
%                     nestoDrugo = false; %nestoDrugo? pamti da li u okolini ne crne celije imamo celiju koja nije ni crna ni bijela
                    if ((T1(lines, columns+1)~= black && T1(lines, columns+1)~= white) || (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) || ... %E,S
                       (T1(lines+1, columns+1)~= black && T1(lines+1, columns+1)~= white)) %ES
%                        nestoDrugo = true;
%                     end
                        max_C=[];
%                     if (nestoDrugo==true)
                        if (T1(lines, columns+1)~= black && T1(lines, columns+1)~= white) max_C=[max_C, T1(lines, columns+1)];end %E
                        if (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) max_C=[max_C, T1(lines+1, columns)]; end %S
                        if (T1(lines+1, columns+1)~= black && T1(lines+1, columns+1)~= white) max_C=[max_C, T1(lines+1, columns+1)]; end %ES

                        if (max_C ~= 0) T2(lines, columns)= max(max_C); end
                    end
                elseif (columns == 1) && (lines == size(T1,1)-1 ) %SW corner
%                     nestoDrugo = false; %nestoDrugo? pamti da li u okolini ne crne celije imamo celiju koja nije ni crna ni bijela
                    if ((T1(lines, columns+1)~= black && T1(lines, columns+1)~= white) || (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) || ... %E,N
                       (T1(lines-1, columns+1)~= black && T1(lines-1, columns+1)~= white)) %EN
%                        nestoDrugo = true;
%                     end
                        max_C=[];
%                     if (nestoDrugo==true)
                        if (T1(lines, columns+1)~= black && T1(lines, columns+1)~= white) max_C=[max_C, T1(lines, columns+1)];end %E
                        if (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) max_C=[max_C, T1(lines-1, columns)]; end %N
                        if (T1(lines-1, columns+1)~= black && T1(lines-1, columns+1)~= white) max_C=[max_C, T1(lines-1, columns+1)]; end %EN

                        if (max_C ~= 0) T2(lines, columns)= max(max_C); end
                    end
                elseif (columns == size(T1,2)-1 ) && (lines == 1) %NE corner
%                     nestoDrugo = false; %nestoDrugo? pamti da li u okolini ne crne celije imamo celiju koja nije ni crna ni bijela
                    if ((T1(lines, columns-1)~= black && T1(lines, columns-1)~= white) || (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) || ... %W,S
                       (T1(lines+1, columns-1)~= black && T1(lines+1, columns-1)~= white)) %WS
%                        nestoDrugo = true;
%                     end
                        max_C=[];
%                     if (nestoDrugo==true)
                        if (T1(lines, columns-1)~= black && T1(lines, columns-1)~= white) max_C=[max_C, T1(lines, columns-1)];end %W
                        if (T1(lines+1, columns)~= black && T1(lines+1, columns)~= white) max_C=[max_C, T1(lines+1, columns)]; end %S
                        if (T1(lines+1, columns-1)~= black && T1(lines+1, columns-1)~= white) max_C=[max_C, T1(lines+1, columns-1)]; end %WS

                        if (max_C ~= 0) T2(lines, columns)= max(max_C); end
                    end
                elseif (columns == size(T1,2)-1 ) && (lines == size(T1,1)-1 ) %SE corner
%                     nestoDrugo = false; %nestoDrugo? pamti da li u okolini ne crne celije imamo celiju koja nije ni crna ni bijela
                    if ((T1(lines, columns-1)~= black && T1(lines, columns-1)~= white) || (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) || ... %W,N
                       (T1(lines-1, columns-1)~= black && T1(lines-1, columns-1)~= white)) %WN
%                        nestoDrugo = true;
%                     end
                        max_C=[];
%                     if (nestoDrugo==true)
                        if (T1(lines, columns-1)~= black && T1(lines, columns-1)~= white) max_C=[max_C, T1(lines, columns-1)];end %W
                        if (T1(lines-1, columns)~= black && T1(lines-1, columns)~= white) max_C=[max_C, T1(lines-1, columns)]; end %N
                        if (T1(lines-1, columns-1)~= black && T1(lines-1, columns-1)~= white) max_C=[max_C, T1(lines-1, columns-1)]; end %WN

                        if (max_C ~= 0) T2(lines, columns)= max(max_C); end
                    end
                end
            end
            N=N+1;
        end
    end
    if (norm(T2)==norm(T3)) 
        return; 
    else
       figure; colormap(cmap); image(T2((2:size(T2,1)-1),(2:size(T2,2)-1))); title('CA\_A2x'); colorbar;  set(gcf,'renderer','OpenGL'); 
       T3=T1; T1=T2; 
    end
end

