File Exchange

image thumbnail

Maximally Distinct Color Generator

version 2.0.2 (233 KB) by Stephen Cobeldick
Generate truly maximally distinct colors in an RGB colormap, using CAM02 colorspace (much better than CIELAB).

16 Downloads

Updated 14 Mar 2019

View License

MAXDISTCOLOR generates an RGB colormap of maximally distinct colors.

MAXDISTCOLOR has two required input arguments:
1. the number of required colors.
2. a function handle that converts from sRGB to a uniform colorspace (e.g. CIELAB or CAM02).

Optional input arguments allow the user to:
* Limit the lightness range.
* Limit the chroma range.
* Provide a colormap of colors to be excluded (e.g. background colors).
* Provide a colormap of colors to be included (e.g. company colorscheme).
* Specify the RGB bit depth (e.g. 8 bits per channel, TrueColor).
* Sort the colormap (e.g. by hue, lightness, farthest colors, etc.).

See the Examples page for information on the required and optional input arguments, together with examples of the generated colormaps.

The recommended colorspace conversion function can be downloaded here: <https://github.com/DrosteEffect/CIECAM02>

%% Examples %%

>> N = 5;
>> fun = @(m)srgb_to_Jab(m,true,'LCD'); % CAM02-LCD, the recommended colorspace.
>> rgb = maxdistcolor(N,fun)
rgb =
1.0000 0.3228 0.0000
0.8730 0.0000 1.0000
0.0000 0.0000 0.7937
0.0000 0.5512 0.0000
0.2222 0.0000 0.0000
>> axes('ColorOrder',rgb, 'NextPlot','replacechildren')
>> X = linspace(0,pi*3,1000);
>> Y = bsxfun(@(x,n)n*sin(x+2*n*pi/N), X(:), 1:N);
>> plot(X,Y, 'linewidth',4)

>> maxdistcolor(N,fun, 'exc',[0,0,0]) % Exclude black (e.g. background).
ans =
0.8095 1.0000 0.0000
1.0000 0.0000 0.9524
0.1111 0.8661 1.0000
0.6349 0.3543 0.0000
0.2698 0.1024 1.0000

>> maxdistcolor(N,fun, 'inc',[1,0,1]) % Include magenta.
ans =
1.0000 0.0000 1.0000
0.0000 0.5433 0.0000
0.0000 0.3543 1.0000
0.5873 0.0000 0.0000
0.0000 0.0000 0.0159

>> [rgb,Lab] = maxdistcolor(6,@srgb_to_Lab, 'Lmin',0.5, 'Lmax',0.7) % Lightness limits.
rgb =
0.7619 0.0000 1.0000
1.0000 0.0000 0.0000
0.0000 0.7795 0.0000
0.0000 0.5591 1.0000
0.8254 0.6457 0.0794
0.8254 0.2835 0.5397
Lab =
50.3665 89.7885 -77.4167
53.2329 80.1093 67.2201
69.9972 -71.4464 68.9565
58.7262 9.8285 -64.4614
69.8987 5.1700 70.3789
52.1378 59.8754 -6.6658

%% Motivation %%

The development of MAXDISTCOLOR was prompted by:
1. Existing "distinct color" generators use inadequate colorspaces and/or algorithms, leading to suboptimal color distinctiveness.
2. The realization that 64 bit PCs with 8 GB of RAM can operate on the entire 16 million colors of 24 bit TrueColor, which allows for neat vectorized MATLAB code.

These two motivations are closely linked to two non-trivial tasks that have to be solved when generating maximally distinct colors:
1. An algorithm to find the best color combination is essentially an NP hard problem (i.e. permutations or global optimum). In MAXDISTCOLOR I use a repeated application of a simple greedy algorithm to find the maximally distinct colors. The repeated greedy algorithm is not fast and is not a general solution for all NP-hard problems, but luckily it gives good results for the regularly sampled RGB cube.
2. Defining a true Uniform Colorspace: the famous CIELAB (used by most existing tools) is outdated and totally inadequate. For MAXDISTCOLOR I recommend CAM02, based on the color appearance model CIECAM02. The CAM02 colorspace provides an accurate measure of the color distance.

Together these provide demonstrably maximally distinct colors. Note also that this algorithm contains no random numbers: it is entirely deterministic and repeatable.

Cite As

Stephen Cobeldick (2019). Maximally Distinct Color Generator (https://www.mathworks.com/matlabcentral/fileexchange/70215-maximally-distinct-color-generator), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (2)

@Yu Li: I suspect that you renamed some function in an attempt to make that example work. I can see two main candidates for your mistake:

1. The function SRGB_TO_LAB is supplied with this submission. It has only one input argument, and it will not work like you are trying to use it (the documentation has examples of how to use it).

2. Earlier versions of my CIECAM02 repository included a function named SRGB_2_JAB which only accepted two input arguments, and it will not work like you are trying to use it (it does not have the second input <isd>)

Note that it really makes absolutely no difference to MAXDISTCOLOR. MAXDISTCOLOR will work with almost any RGB->UCS conversion function, as long as you read and follow the documentation for the conversion function that you use!

In any case, the simplest solution is to download the current CIECAM02 version and use it. I tried it just now: I downloaded both the CIECAM02 and MAXDISTCOLOR zip files to a new folder, unzipped them, and it worked without any error:

>> addpath CIECAM02-master
>> N = 5;
>> fun = @(m)srgb_to_Jab(m,true,'LCD');
>> rgb = maxdistcolor(N,fun)
rgb =
1.0000 0.3228 0.0000
0.8730 0.0000 1.0000
0.0000 0.0000 0.7937
0.0000 0.5512 0.0000
0.2222 0.0000 0.0000

Yu Li

Hi:

I tried the test code:
>> N = 5;
>> fun = @(m)srgb_to_Jab(m,true,'LCD'); % CAM02-LCD, the recommended colorspace.
>> rgb = maxdistcolor(N,fun)

but it reported that:
Error using srgb_to_Jab
Too many input arguments.

Error in @(m)srgb_to_Jab(m,true,'LCD')

Error in maxdistcolor (line 157)
map = fun([0,0,0;1,1,1]);

is there any mistake with my operation?

Bests,
Yu

Updates

2.0.2

Fix time display.

2.0.1

Add correct screenshot.

2.0.0

* Use new CIECAM02 version for documentation.
* Faster VIEW function (plot gamut only on demand).

1.2.0

Add interactive viewer function

1.1.0

Change default to [6,7,6] bits.

1.0.2

Add FEX image

1.0.1

Add links to HTML.

MATLAB Release Compatibility
Created with R2015b
Compatible with R2009a to any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.


Learn About Live Editor