function [A,B,C,mix, res] = secretcolors2(secret)
%This function has been written to show how 'color transparencies cryptography' could work. 
%Arguments : secret is a binary matrix representing a secret (bits or an image). The function
%creates three matrix of the same size : A, B & C. When they are gathered, the original message
%appears. 

size_secret = size(secret);
A = colorize(floor(3*rand(size_secret))); %This first matrix is taken purely randomly
B = colorize(floor(3*rand(size_secret))); %This second matrix is taken purely randomly
C = colorize3(A,B,secret); %This last one is compute to fit but some elements are still random

%Plots of the three matrices
figure(1)
couleurYMC(A);

figure(2)
couleurYMC(B);

figure(3)
couleurYMC(C);

mix = colorcompile(A,B,C); %This function computes the superposition of the three transparencies.

%Plot of the mix
figure(7)
couleur(mix);

%Here, we 'clean' the picture by 'binarizing' the colors : Black, Yellow, Magenta and Cian => Black
%                                                                  but Green, Red and Blue => White 
figure(8)
title('mix cleaned');
res = binarisation(mix);

%End of primary function


%Sub functions

function y = colorize(x)                                                                                
%This function transform a integer matrix in a 'color matrix' where the translation is :                
%  0 => Yellow
%  1 => Cian
%  2 => Magenta

y = '';
s = size(x);
for i = 1:s(1),
    for j = 1:s(2),
        switch x(i,j)
        case 0, 
            y(i,j) = 'Y';
        case 1,
            y(i,j) = 'C';
        case 2,
            y(i,j) = 'M';
        otherwise
            disp('ERROR colorizing');
            y(i,j) = 'X';
        end;
    end;
end;

function y = colorize2(x,sec)
%This function compute the second matrix regarding the first one and the secret matrix
%NOT USED HERE

s = size(x);
for i = 1:s(1)
    for j = 1:s(2)
        if sec(i,j) == 0
            y(i,j) = colorize(floor(3*rand));
        else
            ko = 1;
            while ko,
                tmp = colorize(floor(3*rand));
                if tmp ~= x(i,j)
                    ko = 0;
                end;
            end;
            y(i,j) = tmp;
        end;
    end;
end;


function c = colorize3(a,b,secret)
%This function compute the third matrix regarding the two first ones and the secret matrix

s = size(secret);
for i = 1:s(1)
    for j = 1:s(2)
        if secret(i,j) == 0
            if a(i,j) == b(i,j)
                ko = 1;
                while ko,
                    tmp = colorize(floor(3*rand));
                    if (tmp ~= a(i,j))
                        ko = 0;
                    end;
                end;
            else
                if round(rand) == 0
                    tmp = a(i,j);
                else
                    tmp = b(i,j);
                end;
            end;
        else
            if a(i,j) == b(i,j)
                tmp = a(i,j);
            else
                ko = 1;
                while ko,
                    x = floor(3*rand);
                    tmp = colorize(x);
                    if (tmp ~= a(i,j)) & (tmp ~= b(i,j))
                        ko = 0;
                    end;
                end;
            end;
        end;
        c(i,j) = tmp;
        %disp(sprintf('s = %d, a = %s et b = %s => c = %s',secret(i,j) , a(i,j) , b(i,j) , c(i,j)))
        %pause
    end;
end;

function mix = colorcompile(a,b,c)
%This function mixes the three colors

x = mixing(b,c);

%Plot the temporary result for the mix of two transparencies
figure(4)
couleurRGBYMC(x);

y = mixing(a,c);
z = mixing(a,b);
figure(5)
couleurRGBYMC(y);
figure(6)
couleurRGBYMC(z);


mix = mixing(a,x);

function x = mixing(a,b)
%This function mixes 2 matrices together

s = size(a);
for i = 1:s(1),
    for j = 1:s(2),
        switch a(i,j)
        case 'M',
            switch b(i,j)
            case 'M',
                x(i,j) = 'M';
            case 'Y',
                x(i,j) = 'R';
            case 'C',
                x(i,j) = 'B';
            case 'R',
                x(i,j) = 'R';
            case 'B',
                x(i,j) = 'B';
            case 'G',
                x(i,j) = 'K';
            otherwise
                x(i,j) = '?';
            end;
        case 'C',
            switch b(i,j)
            case 'M',
                x(i,j) = 'B';
            case 'Y',
                x(i,j) = 'G';
            case 'C',
                x(i,j) = 'C';
            case 'R',
                x(i,j) = 'K';
            case 'B',
                x(i,j) = 'B';
            case 'G',
                x(i,j) = 'G';
            otherwise
                x(i,j) = '?';
            end;
        case 'Y',
            switch b(i,j)
            case 'M',
                x(i,j) = 'R';
            case 'Y',
                x(i,j) = 'Y';
            case 'C',
                x(i,j) = 'G';
            case 'R',
                x(i,j) = 'R';
            case 'B',
                x(i,j) = 'K';
            case 'G',
                x(i,j) = 'G';
            otherwise
                x(i,j) = '?';
            end;
        otherwise
            x(i,j) = '!';
        end;
    end;
end;

function r = couleur(x)
%This function plots the matrix containing the 8 colors

map = [[0 0 0]; [1 1 1]; [1 0 0]; [0 1 0]; [0 0 1]; [1 0 1]; [0 1 1]; [1 1 0]];
colormap(map)
s = size(x);
for i = 1:s(1),
    for j = 1:s(2),
        switch x(i,j)
        case 'K',
            r(i,j) = 0;
        case 'W',
            r(i,j) = 1;
        case 'R',
            r(i,j) = 2;
        case 'G',
            r(i,j) = 3;
        case 'B',
            r(i,j) = 4;
        case 'M',
            r(i,j) = 5;
        case 'C',
            r(i,j) = 6;
        case 'Y',
            r(i,j) = 7;
        otherwise r(i,j) = 10;
        end;
    end;
end;
imagesc(r)

function r = couleurYMC(x)
%This function plots the matrix containing Y M & C

map = [[1 0 1]; [0 1 1]; [1 1 0]];
colormap(map)
s = size(x);
for i = 1:s(1),
    for j = 1:s(2),
        switch x(i,j)
        case 'Y',
            r(i,j) = 1;
        case 'C',
            r(i,j) = 2;
        case 'M',
            r(i,j) = 3;
        otherwise r(i,j) = 10;
        end;
    end;
end;
imagesc(r)

function r = couleurRGBYMC(x)
%This function plots the matrix containing R G B Y M & C

map = [[1 0 0]; [0 1 0]; [0 0 1]; [1 0 1]; [0 1 1]; [1 1 0]];
colormap(map)
s = size(x);
for i = 1:s(1),
    for j = 1:s(2),
        switch x(i,j)
        case 'R',
            r(i,j) = 1;
        case 'G',
            r(i,j) = 2;
        case 'B',
            r(i,j) = 3;
        case 'M',
            r(i,j) = 4;
        case 'C',
            r(i,j) = 5;
        case 'Y',
            r(i,j) = 6;
        otherwise r(i,j) = 10;
        end;
    end;
end;
imagesc(r)

function r = binarisation(x)
%This function plots the matrix in binary colors

s = size(x);
for i = 1:s(1),
    for j = 1:s(2),
        switch x(i,j)
        case 'K',
            r(i,j) = 0;
        case 'Y',
            r(i,j) = 0;
        case 'M',
            r(i,j) = 0;
        case 'C',
            r(i,j) = 0;
        otherwise r(i,j) = 1;
        end;
    end;
end;
map = [[0 0 0]; [1 1 1]];
colormap(map)
imagesc(r)