Generate list of random colors, matlab
Alec Jacobson
August 23, 2011
Here's a little matlab function to generate a list of random RGB colors. Save it in random_color.m
function R = random_color(n,preset)
% RANDOM_COLOR generate a list of random colors
%
% R = random_color(size)
% or
% R = random_color(size,preset)
%
% Inputs:
% size size of matrix of random colors to generate
% preset string containing colormap name or 'Pastel'
% Outputs:
% R size by 3 list of RGB colors
%
if(~exist('n','var'))
N = 1;
end
if(~exist('preset','var'))
preset = '';
end
if(strcmp(preset,''))
R = rand([n,3]);
else if(strcmp(preset,'Pastel'))
attempt = 0;
% probably should be between O(n) and O(log n)
max_attempts = 10*prod(n);
retry = true([prod(n),1]);
R = zeros([prod(n),3]);
while(attempt < max_attempts)
num_retry = sum(retry);
R(retry,:) = rand(num_retry,3);
R(retry,:) = (291/255) * R(retry,:)./repmat(sum(R(retry,:),2),1,3);
%retry(retry) = max(R(retry,:),[],2) > 0;
% too bright
next_retry = retry;
next_retry(retry) = ...
(max(R(retry,:),[],2) - min(R(retry,:),[],2)) > (221/255) ;
% too grey
next_retry(retry) = std(R(retry,:),0,2) < (68/255);
if(~any(next_retry))
break;
end
retry = next_retry;
attempt = attempt+1;
end
R=reshape(R,[n 3]) + 34/255;
else
error('Preset not supported');
end
end
Use the 'Pastel' preset to force it to produce colors that are little easier to differentiate between especially on a white background with black foreground (e.g. text, mesh lines, etc.)