File Exchange

image thumbnail

Generate maximally perceptually-distinct colors

version (3.42 KB) by Tim Holy
Choose a set of n colors that can be readily distinguished from each other


Updated 07 Feb 2011

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

When plotting a set of lines, you may want to distinguish them by color. By default, Matlab chooses a small set of colors and cycles among them, and so if you have more than a few lines there will be confusion about which line is which. To fix this problem, one would want to be able to pick a much larger set of distinct colors, where the number of colors equals or exceeds the number of lines you want to plot. Because our ability to distinguish among colors has limits, one should choose these colors to be "maximally perceptually distinguishable."

This function generates a set of colors which are distinguishable by reference to the "Lab" color space, which more closely matches human color perception than RGB. Given an initial large list of possible colors, it iteratively chooses the entry in the list that is farthest (in Lab space) from all previously-chosen entries.

Cite As

Tim Holy (2021). Generate maximally perceptually-distinct colors (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (124)

Donna Dierker



Belinda Finlay

mm gs

Great work.

Davide Manca

Very good function!!! It works perfectly. Kudos.

Lucas Lima

Amanda Morris

Loïc Toraille

Paul Richardson

Thank you, very helpful. I embedded into an app and therefore had to change the inputs to (app,____)
This requires a couple of other changes too: if (nargin < 3) rather than 2, and if (nargin > 3) rather than 2

Yanchao Liu

Gayan Lankeshwara

Works very well.

Thanks a lot.

Adam Rose

Never mind to my question below, it was user error (I saved it to an adjacent folder).

Adam Rose

I'm getting an error with R2019a:

Undefined function 'distinguishable_colors' for input arguments of type 'double'.

Error in PN_model (line 87)
colors = distinguishable_colors(9);

The function description does not mention anything about a specific type of input number, does anyone have any suggestions? I've tried single(9) and int8(9), and they both receive a similar error that the function does not take those as input.

Douglas Fitzpatrick

getting an error with R2020a
Undefined function 'makecform' for input arguments of type 'char'.

Error in distinguishable_colors (line 93)
C = makecform('srgb2lab');

Ernst Uzhanskii

Facundo Cabrera

super useful



Even without the image processing Toolbox this function is very useful! Simply download the colorspace-transformation function (Link below)! Which is, by the way, also mentioned in the given example...

Sina Tureli

better than its counterparts you can find on internet


This is great. Thanks.

andreas franzke

Guilherme Franco

Ken Fu

Awesome thank you

Patrick Morhai

Great work! Thanks a lot for sharing

Luuk Roumen

Nikolas Lamb

Chandrama Sarker

So easy to use. thanks very much for sharing this.

Chanyeol Yoo

Exactly what I was looking for! Thank you!

William Warriner

Anna Weeks



Karen Sachs

My plots and I THANK YOU!!


Excellent tool.

tengzhou ma

It's wonderful and more useful than jet. Thanks~

Sebastian Laechele

Barbara Pernot



Zhuo Chen



Daniel Gaultney

Notsure USA

Amin Mohammed

Very interesting



Marica Valentini

Robert Mar

Eric Zarahn

Burak Kakillioglu

Awesome tool. Thanks.

Mendi Barel


Peng He


Thanks, it works perfectly!


J Eduardo Mucino


Bhagyashri Telsang

Carmine Gno'


Thanks, very useful!

Don't have image processing toolbox. Used the alternative MEX colorspace transformation (got it via File Exchange).

Good work!

Gary Mirams

Really handy, and worked first time.


Thank you very much!

I have ported this algorithm to Python and wrote another program to take the distinguishable_colors output and a label adjacency matrix to group colors as part of the brain image software project:


Arnold Klein

Great! Thanks!


Whyjay Zheng

Really usefull for making a clear graph!

Whyjay Zheng

Alexei Jolkin

Useless, really, as it requires the image processing toolbox AND/OR few other scripts from file exchange.

Sudhanshu Nahata

nice and easy!!

Barbad Molaei

Barbad Molaei

Andrea Perissinotto

Noam Greenboim

Very useful



Alexander H

Lauren Hatchell

Stefan Karlsson

A really, really useful function. Thank you.

Pierre Mégevand


Uses "makecform", which belongs to image processing => usless for those who do not have it. USing the alternative from commens requires some extra effort. Pity.


Awesome function. Very useful.



Miguel Lopes


Great function. Any plans to add in colorblind-friendly functionality?

Timo W

Harshavardhan Sundar

Awesome piece of code....this helped me a lot in building intuition in a different problem I am working on..thanks a lot...

Pierre Mégevand


Tom Gottlieb

Dana Engebretson



Felipe G. Nievinski

Brad Humphreys



Tim Holy

See comments from 2010 for a workaround that doesn't require the Image Processing Toolbox.

Thierry Dalon

Requires the image processing toolbox.

Tim Holy

Looks like it's the same idea. Thanks, I didn't know about that work.

Jean-Yves Tinevez

Hi Tim.
How does your submission relate to Glasbey LUT? and
Glasbey, Chris, et al. "Colour displays for categorical images." Color Research & Application 32.4 (2007): 304-309.


Abraham Asfaw

Thanks! Learned quite a bit from your code.

Chad Greene

Very cool idea! One cautionary comment: I'm one of the 5 to 10% of scientists who is mildly colorblind, and I cannot distinguish between colors 3 and 6 in the distinguishable_colors image shown above.

Melissa Day

Thanks for this! And thanks to the below commenter who suggested replacing the ~ on line 115.

Raz Shimoni

I love this function! Thank you.

Manel Soria

Well done !


Georg Stillfried

Krista Jansen


Thank you for writinig this, it does generate an interesting set of colors that do appear to be contrasting. Would it be possible to generate one, or more, "key" colors that are highly contasted with the rest. I have about 30 lines on a figure, along with the average. While I did make the average wider, it is difficult to determine the color for the "key" line(s) using this routine.


Thanks. Suggestion: Add these lines at the beginning

if nargin < 1

(taken from jet.m) in order to allow something like

colormap distinguishable_colors


Very neat. The usage in combination with Colorspace Transformations is especially elegant.

Change [~,index] to [tmp,index] on line 115 to work on pre-2010 versions of Matlab.

Tim Holy

@Robert Daly: sorry I didn't see your question earlier. It's an interesting application.

I'm no expert at these matters, but I imagine you could convert to LMS color space and then set one or more of the components to zero. You'd probably then want to convert back to Lab to judge perceptual distinguishability.

Alternatively (and perhaps more guaranteed to be accurate), you could use this lookup table:
and then convert the modified RGB values to Lab.

If you implement this, I'd be curious to see what the final result looks like.



Robert Daly

Is there a colour space function that I could use with this function to find colours that are perceptually-distinct to a person with a colour vision deficiency (colour blind)? In the example I have in mind colours that are only different by the amount of red in them such as blue and purple look the same (Protanopia).

Mihail Petrov

I've downloaded the 'colorspace' function from FEX and now it works. Great job!

Mihail Petrov

This is an extremely useful function, but it's unusable for me because I do not have the image processing toolbox :-( Any workaround?


Excellent idea. I have one suggestion. Often times, I want to avoid not just a single background color but a set of them. For example both black and white when I have black text on white background already.



Brian Katz

This works very well. If you are using an older version of MatLab, you will need to modify line 98
[~,index] = max(mindist2);
as the ~ operator will produce an error. Simply change it to junk for example.

Evgeny Pr

Tim Holy

Thanks for the feedback and suggestions, everyone. Perhaps even more important than the issue of depending on the Image Processing Toolbox, you helped me realize that there's no particular reason to restrict this to Lab colorspace; one can easily envision scenarios where the user would like to be able to have some control over what is considered "perceptually similar." So I have uploaded a new version that allows the user to supply his/her own function that converts RGB colors into whatever space "similarity" is to be judged in. Of course, one effect is that this will allow you to use the "colorspace" function.

Matteo Niccoli

Even though I do not have image processing toolbox either, I agree this is an excellent idea and well developed code.

We should be able to use the colorspace FEX by replacing this section of code
C = makecform('srgb2lab');
lab = applycform(rgb,C);
bglab = applycform(bg,C);

with this one:


From the description is seems to be one of the functions that I have been looking for many times, and the excellent rating from Brett only wettens my appetite. Alas, I do not have the image processing toolbox. Would it be possible to instead use this FEX contribution:


Brett Shoelson

Very nicely done...well written, solid, useful code.

MATLAB Release Compatibility
Created with R2010a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!