View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Generate maximally perceptually-distinct colors

4.8 | 61 ratings Rate this file 290 Downloads (last 30 days) File Size: 3.42 KB File ID: #29702 Version: 1.2
image thumbnail

Generate maximally perceptually-distinct colors


Tim Holy (view profile)


14 Dec 2010 (Updated )

Choose a set of n colors that can be readily distinguished from each other

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

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.


Varycolor and Colorspace Transformations inspired this file.

This file inspired Feedback Control Of Many Differential Drive Robots With Uniform Control Inputs, Tactics Toolbox, Simulate Control Of Magnetized Tetrahymena Pyriformis Cells, Youden's Plot, Grouped Bar Plots With Error Bars, Plot Spread Points (Beeswarm Plot), Explore Experimental Data, Colorize Document Bar, Rgbmap Color Maps, Intuitive Rgb Color Values From Xkcd, Cubehelix Colormaps: Beautiful, Distinct, Versatile!, and Color Brewer: Attractive And Distinctive Colormaps.

Required Products Image Processing Toolbox
MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (71)
15 Feb 2017 Ellen

Ellen (view profile)

Thanks, it works perfectly!

20 Dec 2016 Andrew

Andrew (view profile)

08 Nov 2016 J Eduardo Mucino

05 Nov 2016 Clair

Clair (view profile)

28 Oct 2016 Bhagyashri Telsang

12 Oct 2016 Carmine Gno'

28 Jul 2016 R S

R S (view profile)

Thanks, very useful!

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

Good work!

22 Jul 2016 Gary Mirams

Really handy, and worked first time.

02 Jul 2016 binarybottle

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:


16 Jun 2016 Arnold Klein

Great! Thanks!

01 Jun 2016 Serge

Serge (view profile)

19 Apr 2016 Whyjay Zheng

Really usefull for making a clear graph!

19 Apr 2016 Whyjay Zheng

09 Apr 2016 Raghavendra Gulagundi

30 Mar 2016 Alexei Jolkin

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

27 Jan 2016 Sudhanshu Nahata

nice and easy!!

12 Nov 2015 Barbad Molaei

12 Nov 2015 Barbad Molaei

06 Nov 2015 Andrea Perissinotto

05 Aug 2015 Noam Greenboim

Very useful

14 May 2015 cmo

cmo (view profile)

16 Apr 2015 Luca3n1

01 Apr 2015 Alexander H

19 Mar 2015 Lauren Hatchell

03 Mar 2015 Stefan Karlsson

Stefan Karlsson (view profile)

A really, really useful function. Thank you.

26 Jan 2015 Pierre Mégevand

26 Jan 2015 Igal

Igal (view profile)

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.

11 Nov 2014 Hesam

Hesam (view profile)

Awesome function. Very useful.

03 Nov 2014 Eric

Eric (view profile)

30 Oct 2014 Dan

Dan (view profile)

10 Oct 2014 Miguel Lopes

02 Oct 2014 M S

M S (view profile)

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

20 Sep 2014 Timo W

Timo W (view profile)

17 Jun 2014 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...

12 May 2014 Pierre Mégevand

09 Apr 2014 Jim

Jim (view profile)

04 Apr 2014 Tom Gottlieb

27 Mar 2014 Dana Engebretson

02 Mar 2014 abc

abc (view profile)


10 Dec 2013 Felipe G. Nievinski

06 Dec 2013 Brad Humphreys


02 Oct 2013 Werner

Werner (view profile)

02 Oct 2013 Tim Holy

Tim Holy (view profile)

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

Comment only
02 Oct 2013 Thierry Dalon

Requires the image processing toolbox.

Comment only
29 Jul 2013 Tim Holy

Tim Holy (view profile)

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

Comment only
29 Jul 2013 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.

Comment only
26 Jul 2013 Clara

Clara (view profile)

25 Jul 2013 Abraham Asfaw

Thanks! Learned quite a bit from your code.

23 Apr 2013 Chad Greene

Chad Greene (view profile)

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.

Comment only
28 Mar 2013 Melissa Day

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

28 Feb 2013 Raz Shimoni

I love this function! Thank you.

16 Dec 2012 Manel Soria

Well done !

10 Jun 2012 Jonas

Jonas (view profile)

04 Jun 2012 Georg Stillfried

08 May 2012 Krista Jansen

27 Dec 2011 John

John (view profile)

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.

09 Nov 2011 georg

georg (view profile)

Thanks. Suggestion: Add these lines at the beginning

if nargin < 1

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

colormap distinguishable_colors

27 Oct 2011 Pete

Pete (view profile)

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.

28 Apr 2011 Tim Holy

Tim Holy (view profile)

@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.

Comment only
27 Apr 2011 Bosheng


31 Mar 2011 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).

Comment only
07 Feb 2011 Mihail Petrov

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

07 Feb 2011 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?

Comment only
18 Jan 2011 Il

Il (view profile)

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.

17 Jan 2011 Arthur

Arthur (view profile)


24 Dec 2010 Brian Katz

Brian Katz (view profile)

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.

24 Dec 2010 Evgeny Pr

Evgeny Pr (view profile)

14 Dec 2010 Tim Holy

Tim Holy (view profile)

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.

Comment only
14 Dec 2010 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:

14 Dec 2010 Lars

Lars (view profile)

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:


Comment only
14 Dec 2010 Brett Shoelson

Brett Shoelson (view profile)

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

14 Dec 2010 1.1

I have added the option for the user to supply a function handle to any desired colorspace conversion function. One application is to use the file exchange's "colorspace" set of tools. In this case, you no longer need the image processing toolbox.

07 Feb 2011 1.2

As suggested by Il, I added the ability to avoid multiple background colors.

Contact us