Problem with using a lot of colors for consecutive lines

2 views (last 30 days)
I am plotting these consecutive lines (the # of lines can be anywhere btw 1 and 300+) I need each line to be represented with a different color and no two consecutive lines should have similar colors (the distinction of lines is important for the user) currently I am using the following code, but this sometimes produces colors with similar hue/tone:
n = number of consecutive lines as necessary
C = rand(n,3);
for i=1:1:n
p = plot3( [x(i) x(i+1)], [y(i) y(i+1)], [z(i) z(i+1)], 'Color',C(i,:))
end
the problem is that I don't want to use alternative colors since it's important to be able to recognize different parts of my line structure, which happen to be a protein.
any suggestion is appreciated

Accepted Answer

Image Analyst
Image Analyst on 25 Jul 2012
Edited: Image Analyst on 26 Jul 2012
Here's one way. Just make sure that the distance between consecutive lines is more than some minimum distance in RGB space. I also make sure it's not close to the background color of the chart.
% Makes a plot of lines making sure that each line is a much
% different color than the line before it.
% Initialization stuff.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longG;
format compact;
fontSize = 20;
%--------------------------------------------------------------
% Main routine begins here.
numberOfLines = 300; % Number of consecutive lines as necessary
% Specify the miniumum acceptable distance in 3D RGB space.
minAcceptableColorDiff = 0.5;
% Specify the background color (we'll use white).
backgroundColor = [1 1 1];
numGoodColors = 1;
% Keep track of distances just for fun.
distance = zeros(numberOfLines, 1);
backgroundColorDistance = zeros(numberOfLines, 1);
% Start off with some arbitrary color.
listOfGoodColors(numGoodColors, :) = rand(1,3);
% Use a random walk to find subsequent good colors.
% They must be at least minAcceptableColorDiff
% away from the prior color.
tic;
for k = 2 : numberOfLines
% Initialize.
lastColor = listOfGoodColors(numGoodColors, :);
% Now try to find colors that are minAcceptableColorDiff
% away from the last color.
while (distance(k) < minAcceptableColorDiff) ...
|| (backgroundColorDistance(k) < minAcceptableColorDiff)
potentialColor = rand(1,3);
% Find the color difference. Keep track as function of k just for fun.
distance(k) = sum((potentialColor - lastColor).^2);
% Also check the distance of this color from the
% plot background color.
backgroundColorDistance(k) = sum((potentialColor - backgroundColor).^2);
end
% You get to here when you got a good color.
numGoodColors = numGoodColors + 1;
listOfGoodColors(numGoodColors, :) = potentialColor;
end
toc;
%--------------------------------------------------------------
% DONE. Done with creating the list of colors to use.
% Now plot some lines using those colors.
figure;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Now plot the lines at random locations.
x = rand(numberOfLines, 1);
y = rand(numberOfLines, 1);
p = zeros(numberOfLines, 1);
pauseForUser = true;
for k = 2 : numberOfLines
p = plot( [x(k-1) x(k)], [y(k-1) y(k)], ...
'Color', listOfGoodColors(k,:),...
'LineWidth', 3);
if k <= 2
xlim([0 1]);
ylim([0 1]);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
title('Random Color Lines', 'FontSize', fontSize);
hold on;
end
if pauseForUser
message = sprintf('Do you want to continue and plot line %d of %d',...
k, numberOfLines-1);
button = questdlg(message, 'Continue?', 'Yes', 'No', 'Non-stop', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
return;
end
if strcmpi(button, 'Non-stop')
pauseForUser = false;
end
end
end

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!