function nl_srgb2=AHE(nl_srgb,col_out, neg, yuv, tiles, clip, range)
%Algoritmus pro Adaptive histogram equalization
R=nl_srgb(:,:,1); G=nl_srgb(:,:,2); B=nl_srgb(:,:,3);

%CLAHE FULL RANGE pro jasovou slozku
if strcmp(col_out,'RGB_AHE') && yuv==1 && neg==0 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B; 
U=(B-Y)*0.492; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
R = J + 1.13983*V;
G = J - 0.39465*U - 0.58060*V;
B = J + 2.03211*U;
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_AHE') && yuv==1 && neg==1 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
U=(B-Y)*0.492; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
R = J + 1.13983*V;
G = J - 0.39465*U - 0.58060*V;
B = J + 2.03211*U;
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_AHE') && yuv==1 && neg==0 && strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
R = J + 1.13983*V;
G = J - 0.39465*U - 0.58060*V;
B = J + 2.03211*U;
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_AHE') && yuv==1 && neg==1&& strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
U=(B-Y)*0.492; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
R = J + 1.13983*V;
G = J - 0.39465*U - 0.58060*V;
B = J + 2.03211*U;
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_AHE') && yuv==0 && neg==0 && strcmp(range,'FULL')
R=adapthisteq(R,'NumTiles',tiles,'ClipLimit',clip);
G=adapthisteq(G,'NumTiles',tiles,'ClipLimit',clip);
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_AHE') && yuv==0 && neg==1 && strcmp(range,'FULL')
    R=1-R;
    G=1-G;
    B=1-B;
R=adapthisteq(R,'NumTiles',tiles,'ClipLimit',clip);
G=adapthisteq(G,'NumTiles',tiles,'ClipLimit',clip);
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_AHE') && yuv==0 && neg==0 && strcmp(range,'ORIGINAL')
R=adapthisteq(R,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
G=adapthisteq(G,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_AHE') && yuv==0 && neg==1&& strcmp(range,'ORIGINAL')
    R=1-R;
    G=1-G;
    B=1-B;
R=adapthisteq(R,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
G=adapthisteq(G,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'R_AHE') && yuv==1 && neg==0 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
R = J + 1.13983*V;
nl_srgb2=R;
elseif strcmp(col_out,'R_AHE') && yuv==1 && neg==1 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
R = J + 1.13983*V;
nl_srgb2=R;
elseif strcmp(col_out,'R_AHE') && yuv==1 && neg==0 && strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
R = J + 1.13983*V;
nl_srgb2=R;
elseif strcmp(col_out,'R_AHE') && yuv==1 && neg==1&& strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
R = J + 1.13983*V;
nl_srgb2=R;
elseif strcmp(col_out,'R_AHE') && yuv==0 && neg==0 && strcmp(range,'FULL')
R=adapthisteq(R,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=R;
elseif strcmp(col_out,'R_AHE') && yuv==0 && neg==1 && strcmp(range,'FULL')
    R=1-R;
R=adapthisteq(R,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=R;
elseif strcmp(col_out,'R_AHE') && yuv==0 && neg==0 && strcmp(range,'ORIGINAL')
R=adapthisteq(R,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=R;
elseif strcmp(col_out,'R_AHE') && yuv==0 && neg==1&& strcmp(range,'ORIGINAL')
    R=1-R;
R=adapthisteq(R,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=R;
elseif strcmp(col_out,'G_AHE') && yuv==1 && neg==0 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B; 
U=(B-Y)*0.492; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
G = J - 0.39465*U - 0.58060*V;
nl_srgb2=G;
elseif strcmp(col_out,'G_AHE') && yuv==1 && neg==1 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
U=(B-Y)*0.492; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
G = J - 0.39465*U - 0.58060*V;
nl_srgb2=G;
elseif strcmp(col_out,'G_AHE') && yuv==1 && neg==0 && strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
G = J - 0.39465*U - 0.58060*V;
nl_srgb2=G;
elseif strcmp(col_out,'G_AHE') && yuv==1 && neg==1&& strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
U=(B-Y)*0.492; 
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
G = J - 0.39465*U - 0.58060*V;
nl_srgb2=G;
elseif strcmp(col_out,'G_AHE') && yuv==0 && neg==0 && strcmp(range,'FULL')
G=adapthisteq(G,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=G;
elseif strcmp(col_out,'G_AHE') && yuv==0 && neg==1 && strcmp(range,'FULL')
    G=1-G;
G=adapthisteq(G,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=G;
elseif strcmp(col_out,'G_AHE') && yuv==0 && neg==0 && strcmp(range,'ORIGINAL')
G=adapthisteq(G,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=G;
elseif strcmp(col_out,'G_AHE') && yuv==0 && neg==1&& strcmp(range,'ORIGINAL')
    G=1-G;
G=adapthisteq(G,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=G;
elseif strcmp(col_out,'B_AHE') && yuv==1 && neg==0 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B; 
U=(B-Y)*0.492; 
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
B = J + 2.03211*U;
nl_srgb2=B;
elseif strcmp(col_out,'B_AHE') && yuv==1 && neg==1 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
U=(B-Y)*0.492; 
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
B = J + 2.03211*U;
nl_srgb2=B;
elseif strcmp(col_out,'B_AHE') && yuv==1 && neg==0 && strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492; 
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
B = J + 2.03211*U;
nl_srgb2=B;
elseif strcmp(col_out,'B_AHE') && yuv==1 && neg==1&& strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
U=(B-Y)*0.492; 
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
B = J + 2.03211*U;
nl_srgb2=B;
elseif strcmp(col_out,'B_AHE') && yuv==0 && neg==0 && strcmp(range,'FULL')
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=B;
elseif strcmp(col_out,'B_AHE') && yuv==0 && neg==1 && strcmp(range,'FULL')
    B=1-B;
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=B;
elseif strcmp(col_out,'B_AHE') && yuv==0 && neg==0 && strcmp(range,'ORIGINAL')
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=B;
elseif strcmp(col_out,'B_AHE') && yuv==0 && neg==1&& strcmp(range,'ORIGINAL')
    B=1-B;
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=B;
elseif strcmp(col_out,'GB_AHE') && yuv==1 && neg==0 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B; 
U=(B-Y)*0.492;
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
B = J + 2.03211*U;
G = J - 0.39465*U - 0.58060*V;
B=(G+B)/2;
nl_srgb2=B;
elseif strcmp(col_out,'GB_AHE') && yuv==1 && neg==1 && strcmp(range,'FULL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
U=(B-Y)*0.492;
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip);
B = J + 2.03211*U;
G = J - 0.39465*U - 0.58060*V;
B=(G+B)/2;
nl_srgb2=B;
elseif strcmp(col_out,'GB_AHE') && yuv==1 && neg==0 && strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492;
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
B = J + 2.03211*U;
G = J - 0.39465*U - 0.58060*V;
B=(G+B)/2;
nl_srgb2=B;
elseif strcmp(col_out,'GB_AHE') && yuv==1 && neg==1&& strcmp(range,'ORIGINAL')
Y = 0.299*R + 0.587*G + 0.114*B;
Y=1-Y;
U=(B-Y)*0.492;
V=(R-Y)*0.877;
J=adapthisteq(Y,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
B = J + 2.03211*U;
G = J - 0.39465*U - 0.58060*V;
B=(G+B)/2;
nl_srgb2=B;
elseif strcmp(col_out,'GB_AHE') && yuv==0 && neg==0 && strcmp(range,'FULL')
    B=(G+B)/2;
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=B;
elseif strcmp(col_out,'GB_AHE') && yuv==0 && neg==1 && strcmp(range,'FULL')
    B=(G+B)/2;
    B=1-B;
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip);
nl_srgb2=B;
elseif strcmp(col_out,'GB_AHE') && yuv==0 && neg==0 && strcmp(range,'ORIGINAL')
    B=(G+B)/2;
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=B;
elseif strcmp(col_out,'GB_AHE') && yuv==0 && neg==1&& strcmp(range,'ORIGINAL')
    B=(G+B)/2;
    B=1-B;
B=adapthisteq(B,'NumTiles',tiles,'ClipLimit',clip,'Range','original');
nl_srgb2=B;
end
