function I = findColor(RGB, mode, thres)
% function BIN = findColor(RGB, rgbVal, Thres)
% Function for detecting a specific rgb value, within acceptable tollerance

% RGB: the rgb image
% mode: the color to detect:
% 1: red
% 2: green
% 3: blue
% thres: the distance tollerance

[M,N,t] = size(RGB);

switch (mode)
case 1
I1 = zeros(M,N); I2 = zeros(M,N);
I1( find(RGB(:,:,1) > thres * RGB(:,:,2)) ) = 1;
I2( find(RGB(:,:,1) > thres * RGB(:,:,3)) ) = 1;
strTitle = 'Color RED detected (white areas)';
I = I1 .* I2;
case 2
I1 = zeros(M,N); I2 = zeros(M,N);
I1( find(RGB(:,:,2) > thres * RGB(:,:,1)) ) = 1;
I2( find(RGB(:,:,2) > thres * RGB(:,:,3)) ) = 1;
strTitle = 'Color GREEN detected (white areas)';
I = I1 .* I2; 
case 3
I1 = zeros(M,N); I2 = zeros(M,N);
I1( find(RGB(:,:,3) > thres * RGB(:,:,1)) ) = 1;
I2( find(RGB(:,:,3) > thres * RGB(:,:,2)) ) = 1;
strTitle = 'Color BLUE detected (white areas)';
I = I1 .* I2; 
otherwise
fprintf('WRONG ARGUMENTS'\n);
return;
end