File Exchange

image thumbnail

Maximally Distinct Color Generator

version 2.0.4 (221 KB) by Stephen Cobeldick
Generate truly maximally distinct colors in an RGB colormap.

19 Downloads

Updated 04 Jul 2019

View License

MAXDISTCOLOR generates an RGB colormap of maximally distinct colors.

MAXDISTCOLOR has two required input arguments:
1. the required number of colors.
2. a function handle that converts from sRGB to a uniform colorspace (e.g. CIELAB, DIN99, 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. Note that requesting many colors from a large gamut can require hours of processing.

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

%% Examples %%

>> N = 5;
>> rgb = maxdistcolor(N,@srgb_to_Jab)
rgb =
1.00000 0.36220 0.00000
0.47619 0.00000 0.42857
0.00000 0.70866 0.00000
0.00000 0.55118 1.00000
0.00000 0.16535 0.00000
>> 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(5,@srgb_to_Jab, 'exc',[0,0,0]) % Exclude black background.
ans =
1.00000 1.00000 0.00000
1.00000 0.73228 1.00000
0.00000 0.48031 0.00000
0.71429 0.00000 0.01587
0.00000 0.30709 1.00000

>> maxdistcolor(5,@(m)srgb_to_Jab(m,true,'LCD'), 'inc',[1,0,1]) % Include magenta.
ans =
1.00000 0.00000 1.00000
0.00000 0.54331 0.00000
0.00000 0.35433 1.00000
0.58730 0.00000 0.00000
0.00000 0.00000 0.01587

>> [rgb,Lab] = maxdistcolor(6,@srgb_to_Lab, 'Lmin',0.5, 'Lmax',0.7) % Lightness limits.
rgb =
0.76190 0.00000 1.00000
1.00000 0.00000 0.00000
0.00000 0.77953 0.00000
0.00000 0.55906 1.00000
0.82540 0.64567 0.07937
0.82540 0.28346 0.53968
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 (3)

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

* Update screenshot

2.0.3

* Add DIN99 conversion function.
* Improve time display.
* Iteration limit based on reaching a stable solution.
* _VIEW: new RGB values on colorbands plot.
* _VIEW: show excluded colors in RGB cube.

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