function nl_srgb2=ALCE(nl_srgb,col_out, neg, yuv, mask)
%Algoritmus Adaptive Local Contrast Enhancement
R=nl_srgb(:,:,1); G=nl_srgb(:,:,2); B=nl_srgb(:,:,3);
if strcmp(col_out,'RGB_ALCE') && yuv==1 && neg==0
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492;
V=(R-Y)*0.877;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí definován vstupem mask
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí definovaná vstupem mask
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
R = pom4 + 1.13983*V;
G = pom4 - 0.39465*U - 0.58060*V;
B = pom4 + 2.03211*U;
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_ALCE') && yuv==1 && neg==1
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492;
V=(R-Y)*0.877;
%Y=1-Y;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
pom4=1-pom4;
R = pom4 + 1.13983*V;
G = pom4 - 0.39465*U - 0.58060*V;
B = pom4 + 2.03211*U;
nl_srgb2=cat(3,R,G,B);
elseif strcmp(col_out,'RGB_ALCE') && yuv==0 && neg==0
velikost_R=size(R);
velikost_G=size(G);
velikost_B=size(B);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_R=conv2(R,mask/vmask,'same');
ALCE_G=conv2(G,mask/vmask,'same');
ALCE_B=conv2(B,mask/vmask,'same');
ALCE2_R=stdfilt(R,mask);
ALCE2_G=stdfilt(G,mask);
ALCE2_B=stdfilt(B,mask);
pom_R=zeros(velikost_R(1), velikost_R(2));
pom_G=zeros(velikost_G(1), velikost_G(2));
pom_B=zeros(velikost_B(1), velikost_B(2));
pom2_R=zeros(velikost_R(1), velikost_R(2));
pom2_G=zeros(velikost_G(1), velikost_G(2));
pom2_B=zeros(velikost_B(1), velikost_B(2));
pom3_R=zeros(velikost_R(1), velikost_R(2));
pom3_G=zeros(velikost_G(1), velikost_G(2));
pom3_B=zeros(velikost_B(1), velikost_B(2));
pom4_R=zeros(velikost_R(1), velikost_R(2));
pom4_G=zeros(velikost_G(1), velikost_G(2));
pom4_B=zeros(velikost_B(1), velikost_B(2));
Mi_R=min(min(R));
Mi_G=min(min(G));
Mi_B=min(min(B));
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom_R(i,j)=((1+exp((ALCE_R(i,j)-R(i,j))./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom_G(i,j)=((1+exp((ALCE_G(i,j)-G(i,j))./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom_B(i,j)=((1+exp((ALCE_B(i,j)-B(i,j))./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom_R);
pom_R(loc)=0;
loc = isnan(pom_G);
pom_G(loc)=0;
loc = isnan(pom_B);
pom_B(loc)=0;
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom2_R(i,j)=((1+exp((ALCE_R(i,j)-Mi_R)./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom2_G(i,j)=((1+exp((ALCE_G(i,j)-Mi_G)./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom2_B(i,j)=((1+exp((ALCE_B(i,j)-Mi_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom2_R);
pom2_R(loc)=0;
loc = isnan(pom2_G);
pom2_G(loc)=0;
loc = isnan(pom2_B);
pom2_B(loc)=0;
M_R=max(max(R));
M_G=max(max(G));
M_B=max(max(B));
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom3_R(i,j)=((1+exp((ALCE_R(i,j)-M_R)./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom3_G(i,j)=((1+exp((ALCE_G(i,j)-M_G)./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom3_B(i,j)=((1+exp((ALCE_B(i,j)-M_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom3_R);
pom3_R(loc)=0;
loc = isnan(pom3_G);
pom3_G(loc)=0;
loc = isnan(pom3_B);
pom3_B(loc)=0;
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom4_R(i,j)=(pom_R(i,j)-pom2_R(i,j))./(pom3_R(i,j)-pom2_R(i,j));
    end
end
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom4_G(i,j)=(pom_G(i,j)-pom2_G(i,j))./(pom3_G(i,j)-pom2_G(i,j));
    end
end
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom4_B(i,j)=(pom_B(i,j)-pom2_B(i,j))./(pom3_B(i,j)-pom2_B(i,j));
    end
end
loc = isnan(pom4_R);
pom4_R(loc)=0;
loc = isnan(pom4_G);
pom4_G(loc)=0;
loc = isnan(pom4_B);
pom4_B(loc)=0;
nl_srgb2=cat(3,pom4_R,pom4_G,pom4_B);
elseif strcmp(col_out,'RGB_ALCE') && yuv==0 && neg==1
%     R=1-R;
%     G=1-G;
%     B=1-B;
velikost_R=size(R);
velikost_G=size(G);
velikost_B=size(B);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_R=conv2(R,mask/vmask,'same');
ALCE_G=conv2(G,mask/vmask,'same');
ALCE_B=conv2(B,mask/vmask,'same');
ALCE2_R=stdfilt(R,mask);
ALCE2_G=stdfilt(G,mask);
ALCE2_B=stdfilt(B,mask);
pom_R=zeros(velikost_R(1), velikost_R(2));
pom_G=zeros(velikost_G(1), velikost_G(2));
pom_B=zeros(velikost_B(1), velikost_B(2));
pom2_R=zeros(velikost_R(1), velikost_R(2));
pom2_G=zeros(velikost_G(1), velikost_G(2));
pom2_B=zeros(velikost_B(1), velikost_B(2));
pom3_R=zeros(velikost_R(1), velikost_R(2));
pom3_G=zeros(velikost_G(1), velikost_G(2));
pom3_B=zeros(velikost_B(1), velikost_B(2));
pom4_R=zeros(velikost_R(1), velikost_R(2));
pom4_G=zeros(velikost_G(1), velikost_G(2));
pom4_B=zeros(velikost_B(1), velikost_B(2));
Mi_R=min(min(R));
Mi_G=min(min(G));
Mi_B=min(min(B));
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom_R(i,j)=((1+exp((ALCE_R(i,j)-R(i,j))./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom_G(i,j)=((1+exp((ALCE_G(i,j)-G(i,j))./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom_B(i,j)=((1+exp((ALCE_B(i,j)-B(i,j))./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom_R);
pom_R(loc)=0;
loc = isnan(pom_G);
pom_G(loc)=0;
loc = isnan(pom_B);
pom_B(loc)=0;
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom2_R(i,j)=((1+exp((ALCE_R(i,j)-Mi_R)./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom2_G(i,j)=((1+exp((ALCE_G(i,j)-Mi_G)./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom2_B(i,j)=((1+exp((ALCE_B(i,j)-Mi_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom2_R);
pom2_R(loc)=0;
loc = isnan(pom2_G);
pom2_G(loc)=0;
loc = isnan(pom2_B);
pom2_B(loc)=0;
M_R=max(max(R));
M_G=max(max(G));
M_B=max(max(B));
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom3_R(i,j)=((1+exp((ALCE_R(i,j)-M_R)./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom3_G(i,j)=((1+exp((ALCE_G(i,j)-M_G)./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom3_B(i,j)=((1+exp((ALCE_B(i,j)-M_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom3_R);
pom3_R(loc)=0;
loc = isnan(pom3_G);
pom3_G(loc)=0;
loc = isnan(pom3_B);
pom3_B(loc)=0;
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom4_R(i,j)=(pom_R(i,j)-pom2_R(i,j))./(pom3_R(i,j)-pom2_R(i,j));
    end
end
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom4_G(i,j)=(pom_G(i,j)-pom2_G(i,j))./(pom3_G(i,j)-pom2_G(i,j));
    end
end
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom4_B(i,j)=(pom_B(i,j)-pom2_B(i,j))./(pom3_B(i,j)-pom2_B(i,j));
    end
end
loc = isnan(pom4_R);
pom4_R(loc)=0;
loc = isnan(pom4_G);
pom4_G(loc)=0;
loc = isnan(pom4_B);
pom4_B(loc)=0;
    pom4_R=1-pom4_R;
    pom4_G=1-pom4_G;
    pom4_B=1-pom4_B;
nl_srgb2=cat(3,pom4_R,pom4_G,pom4_B);
elseif strcmp(col_out,'R_ALCE') && yuv==1 && neg==0
Y = 0.299*R + 0.587*G + 0.114*B;
V=(R-Y)*0.877;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
R = pom4 + 1.13983*V;
nl_srgb2=R;
elseif strcmp(col_out,'R_ALCE') && yuv==1 && neg==1
Y = 0.299*R + 0.587*G + 0.114*B;
V=(R-Y)*0.877;
% Y=1-Y;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
pom4=1-pom4;
R = pom4 + 1.13983*V;
nl_srgb2=R;
elseif strcmp(col_out,'R_ALCE') && yuv==0 && neg==0
velikost_R=size(R);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_R=conv2(R,mask/vmask,'same');
ALCE2_R=stdfilt(R,mask);
pom_R=zeros(velikost_R(1), velikost_R(2));
pom2_R=zeros(velikost_R(1), velikost_R(2));
pom3_R=zeros(velikost_R(1), velikost_R(2));
pom4_R=zeros(velikost_R(1), velikost_R(2));
Mi_R=min(min(R));
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom_R(i,j)=((1+exp((ALCE_R(i,j)-R(i,j))./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
loc = isnan(pom_R);
pom_R(loc)=0;
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom2_R(i,j)=((1+exp((ALCE_R(i,j)-Mi_R)./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
loc = isnan(pom2_R);
pom2_R(loc)=0;
M_R=max(max(R));
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom3_R(i,j)=((1+exp((ALCE_R(i,j)-M_R)./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
loc = isnan(pom3_R);
pom3_R(loc)=0;
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom4_R(i,j)=(pom_R(i,j)-pom2_R(i,j))./(pom3_R(i,j)-pom2_R(i,j));
    end
end
loc = isnan(pom4_R);
pom4_R(loc)=0;
nl_srgb2=pom4_R;
elseif strcmp(col_out,'R_ALCE') && yuv==0 && neg==1
    %R=1-R;
velikost_R=size(R);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_R=conv2(R,mask/vmask,'same');
ALCE2_R=stdfilt(R,mask);
pom_R=zeros(velikost_R(1), velikost_R(2));
pom2_R=zeros(velikost_R(1), velikost_R(2));
pom3_R=zeros(velikost_R(1), velikost_R(2));
pom4_R=zeros(velikost_R(1), velikost_R(2));
Mi_R=min(min(R));
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom_R(i,j)=((1+exp((ALCE_R(i,j)-R(i,j))./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
loc = isnan(pom_R);
pom_R(loc)=0;
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom2_R(i,j)=((1+exp((ALCE_R(i,j)-Mi_R)./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
loc = isnan(pom2_R);
pom2_R(loc)=0;
M_R=max(max(R));
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom3_R(i,j)=((1+exp((ALCE_R(i,j)-M_R)./(ALCE2_R(i,j)-Mi_R)))^(-1));
    end
end
loc = isnan(pom3_R);
pom3_R(loc)=0;
for i=1:velikost_R(1)
    for j=1:velikost_R(2)
        pom4_R(i,j)=(pom_R(i,j)-pom2_R(i,j))./(pom3_R(i,j)-pom2_R(i,j));
    end
end
loc = isnan(pom4_R);
pom4_R(loc)=0;
pom4_R=1-pom4_R;
nl_srgb2=pom4_R;
elseif strcmp(col_out,'G_ALCE') && yuv==1 && neg==0
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492;
V=(R-Y)*0.877;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
G = pom4 - 0.39465*U - 0.58060*V;
nl_srgb2=G;
elseif strcmp(col_out,'G_ALCE') && yuv==1 && neg==1
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492;
V=(R-Y)*0.877;
% Y=1-Y;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
pom4=1-pom4;
G = pom4 - 0.39465*U - 0.58060*V;
nl_srgb2=G;
elseif strcmp(col_out,'G_ALCE') && yuv==0 && neg==0
velikost_G=size(G);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_G=conv2(G,mask/vmask,'same');
ALCE2_G=stdfilt(G,mask);
pom_G=zeros(velikost_G(1), velikost_G(2));
pom2_G=zeros(velikost_G(1), velikost_G(2));
pom3_G=zeros(velikost_G(1), velikost_G(2));
pom4_G=zeros(velikost_G(1), velikost_G(2));
Mi_G=min(min(G));
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom_G(i,j)=((1+exp((ALCE_G(i,j)-G(i,j))./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
loc = isnan(pom_G);
pom_G(loc)=0;
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom2_G(i,j)=((1+exp((ALCE_G(i,j)-Mi_G)./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
loc = isnan(pom2_G);
pom2_G(loc)=0;
M_G=max(max(G));
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom3_G(i,j)=((1+exp((ALCE_G(i,j)-M_G)./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
loc = isnan(pom3_G);
pom3_G(loc)=0;
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom4_G(i,j)=(pom_G(i,j)-pom2_G(i,j))./(pom3_G(i,j)-pom2_G(i,j));
    end
end
loc = isnan(pom4_G);
pom4_G(loc)=0;
nl_srgb2=pom4_G;
elseif strcmp(col_out,'G_ALCE') && yuv==0 && neg==1
    %G=1-G;
velikost_G=size(G);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_G=conv2(G,mask/vmask,'same');
ALCE2_G=stdfilt(G,mask);
pom_G=zeros(velikost_G(1), velikost_G(2));
pom2_G=zeros(velikost_G(1), velikost_G(2));
pom3_G=zeros(velikost_G(1), velikost_G(2));
pom4_G=zeros(velikost_G(1), velikost_G(2));
Mi_G=min(min(G));
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom_G(i,j)=((1+exp((ALCE_G(i,j)-G(i,j))./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
loc = isnan(pom_G);
pom_G(loc)=0;
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom2_G(i,j)=((1+exp((ALCE_G(i,j)-Mi_G)./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
loc = isnan(pom2_G);
pom2_G(loc)=0;
M_G=max(max(G));
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom3_G(i,j)=((1+exp((ALCE_G(i,j)-M_G)./(ALCE2_G(i,j)-Mi_G)))^(-1));
    end
end
loc = isnan(pom3_G);
pom3_G(loc)=0;
for i=1:velikost_G(1)
    for j=1:velikost_G(2)
        pom4_G(i,j)=(pom_G(i,j)-pom2_G(i,j))./(pom3_G(i,j)-pom2_G(i,j));
    end
end
loc = isnan(pom4_G);
pom4_G(loc)=0;
pom4_G=1-pom4_G;
nl_srgb2=pom4_G;
elseif strcmp(col_out,'B_ALCE') && yuv==1 && neg==0
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
B = pom4 + 2.03211*U;
nl_srgb2=B;
elseif strcmp(col_out,'B_ALCE') && yuv==1 && neg==1
Y = 0.299*R + 0.587*G + 0.114*B;
%Y=1-Y;
U=(B-Y)*0.492;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
B = pom4 + 2.03211*U;
B=1-B;
nl_srgb2=B;
elseif strcmp(col_out,'B_ALCE') && yuv==0 && neg==0
velikost_B=size(B);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_B=conv2(B,mask/vmask,'same');
ALCE2_B=stdfilt(B,mask);
pom_B=zeros(velikost_B(1), velikost_B(2));
pom2_B=zeros(velikost_B(1), velikost_B(2));
pom3_B=zeros(velikost_B(1), velikost_B(2));
pom4_B=zeros(velikost_B(1), velikost_B(2));
Mi_B=min(min(B));
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom_B(i,j)=((1+exp((ALCE_B(i,j)-B(i,j))./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom_B);
pom_B(loc)=0;
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom2_B(i,j)=((1+exp((ALCE_B(i,j)-Mi_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom2_B);
pom2_B(loc)=0;
M_B=max(max(B));
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom3_B(i,j)=((1+exp((ALCE_B(i,j)-M_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom3_B);
pom3_B(loc)=0;
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom4_B(i,j)=(pom_B(i,j)-pom2_B(i,j))./(pom3_B(i,j)-pom2_B(i,j));
    end
end
loc = isnan(pom4_B);
pom4_B(loc)=0;
nl_srgb2=pom4_B;
elseif strcmp(col_out,'B_ALCE') && yuv==0 && neg==1
    %B=1-B;
velikost_B=size(B);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_B=conv2(B,mask/vmask,'same');
ALCE2_B=stdfilt(B,mask);
pom_B=zeros(velikost_B(1), velikost_B(2));
pom2_B=zeros(velikost_B(1), velikost_B(2));
pom3_B=zeros(velikost_B(1), velikost_B(2));
pom4_B=zeros(velikost_B(1), velikost_B(2));
Mi_B=min(min(B));
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom_B(i,j)=((1+exp((ALCE_B(i,j)-B(i,j))./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom_B);
pom_B(loc)=0;
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom2_B(i,j)=((1+exp((ALCE_B(i,j)-Mi_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom2_B);
pom2_B(loc)=0;
M_B=max(max(B));
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom3_B(i,j)=((1+exp((ALCE_B(i,j)-M_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom3_B);
pom3_B(loc)=0;
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom4_B(i,j)=(pom_B(i,j)-pom2_B(i,j))./(pom3_B(i,j)-pom2_B(i,j));
    end
end
loc = isnan(pom4_B);
pom4_B(loc)=0;
pom4_B=1-pom4_B;
nl_srgb2=pom4_B;
elseif strcmp(col_out,'GB_ALCE') && yuv==1 && neg==0
Y = 0.299*R + 0.587*G + 0.114*B;
U=(B-Y)*0.492;
V=(R-Y)*0.877;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
B = pom4 + 2.03211*U;
G = pom4 - 0.39465*U - 0.58060*V;
B=(B+G)/2;
nl_srgb2=B;
elseif strcmp(col_out,'GB_ALCE') && yuv==1 && neg==1
Y = 0.299*R + 0.587*G + 0.114*B;
V=(R-Y)*0.877;
%Y=1-Y;
U=(B-Y)*0.492;
velikost=size(Y);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE=conv2(Y,mask/vmask,'same'); %průměr okolí 49x49
ALCE2=stdfilt(Y,mask); %směrodatná odchylka okolí 49x49
pom=zeros(velikost(1), velikost(2));
pom2=zeros(velikost(1), velikost(2));
pom3=zeros(velikost(1), velikost(2));
pom4=zeros(velikost(1), velikost(2));
Mi=min(min(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom(i,j)=((1+exp((ALCE(i,j)-Y(i,j))./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom);
pom(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom2(i,j)=((1+exp((ALCE(i,j)-Mi)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom2);
pom2(loc)=0;
M=max(max(Y));
for i=1:velikost(1)
    for j=1:velikost(2)
        pom3(i,j)=((1+exp((ALCE(i,j)-M)./(ALCE2(i,j)-Mi)))^(-1));
    end
end
loc = isnan(pom3);
pom3(loc)=0;
for i=1:velikost(1)
    for j=1:velikost(2)
        pom4(i,j)=(pom(i,j)-pom2(i,j))./(pom3(i,j)-pom2(i,j));
    end
end
loc = isnan(pom4);
pom4(loc)=0;
B = pom4 + 2.03211*U;
G = pom4 - 0.39465*U - 0.58060*V;
B=(B+G)/2;
B=1-B;
nl_srgb2=B;
elseif strcmp(col_out,'GB_ALCE') && yuv==0 && neg==0
    B=(G+B)/2;
velikost_B=size(B);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_B=conv2(B,mask/vmask,'same');
ALCE2_B=stdfilt(B,mask);
pom_B=zeros(velikost_B(1), velikost_B(2));
pom2_B=zeros(velikost_B(1), velikost_B(2));
pom3_B=zeros(velikost_B(1), velikost_B(2));
pom4_B=zeros(velikost_B(1), velikost_B(2));
Mi_B=min(min(B));
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom_B(i,j)=((1+exp((ALCE_B(i,j)-B(i,j))./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom_B);
pom_B(loc)=0;
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom2_B(i,j)=((1+exp((ALCE_B(i,j)-Mi_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom2_B);
pom2_B(loc)=0;
M_B=max(max(B));
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom3_B(i,j)=((1+exp((ALCE_B(i,j)-M_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom3_B);
pom3_B(loc)=0;
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom4_B(i,j)=(pom_B(i,j)-pom2_B(i,j))./(pom3_B(i,j)-pom2_B(i,j));
    end
end
loc = isnan(pom4_B);
pom4_B(loc)=0;
nl_srgb2=pom4_B;
elseif strcmp(col_out,'GB_ALCE') && yuv==0 && neg==1
    B=(G+B)/2;
velikost_B=size(B);
vmask=size(mask);
vmask=vmask(1)^2;
ALCE_B=conv2(B,mask/vmask,'same');
ALCE2_B=stdfilt(B,mask);
pom_B=zeros(velikost_B(1), velikost_B(2));
pom2_B=zeros(velikost_B(1), velikost_B(2));
pom3_B=zeros(velikost_B(1), velikost_B(2));
pom4_B=zeros(velikost_B(1), velikost_B(2));
Mi_B=min(min(B));
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom_B(i,j)=((1+exp((ALCE_B(i,j)-B(i,j))./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom_B);
pom_B(loc)=0;
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom2_B(i,j)=((1+exp((ALCE_B(i,j)-Mi_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom2_B);
pom2_B(loc)=0;
M_B=max(max(B));
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom3_B(i,j)=((1+exp((ALCE_B(i,j)-M_B)./(ALCE2_B(i,j)-Mi_B)))^(-1));
    end
end
loc = isnan(pom3_B);
pom3_B(loc)=0;
for i=1:velikost_B(1)
    for j=1:velikost_B(2)
        pom4_B(i,j)=(pom_B(i,j)-pom2_B(i,j))./(pom3_B(i,j)-pom2_B(i,j));
    end
end
loc = isnan(pom4_B);
pom4_B(loc)=0;
pom4_B=1-pom4_B;
nl_srgb2=pom4_B;
end

