image thumbnail

Maximally Distinct Color Generator

version 2.2.0 (190 KB) by Stephen
Generate maximally distinct colors in an RGB colormap.

811 Downloads

Updated 25 Nov 2020

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, CAM02-UCS, CAM16-UCS, OSA-UCS, etc.).

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 tab (HTML documentation) for explanations of the required and optional input arguments.

%% Warning %%

Requesting many colors from a large gamut can require hours/days/.. of processing. Some option combinations are not tractable.

%% Examples %%

>> N = 5;
>> fun = @(m)sRGB_to_OSAUCS(m,true,true); % recommended OSA-UCS
>> rgb = maxdistcolor(N,fun)
rgb =
1.0000 0.0000 1.0000
0.0000 0.0000 1.0000
0.3016 0.0000 0.3492
1.0000 0.0000 0.0000
0.0000 0.4331 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(5,fun, 'exc',[0,0,0]) % Exclude black (e.g. background).
ans =
1.0000 0.0000 1.0000
0.0000 0.0000 1.0000
0.0000 1.0000 0.0000
1.0000 0.0315 0.0000
0.7619 0.8189 1.0000

>> maxdistcolor(5,fun, 'inc',[1,0,1]) % Include magenta.
ans =
1.0000 0.0000 1.0000 % magenta
0.0000 0.0000 1.0000
0.3016 0.0000 0.3492
1.0000 0.0000 0.0000
0.0000 0.4331 0.0000

>> [rgb,Lab] = maxdistcolor(6,@sRGB_to_CIELab, 'Lmin',0.5, 'Lmax',0.7)
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.3682 89.7713 -77.4020
53.2408 80.0925 67.2032
69.9953 -71.4448 68.9550
58.7226 09.8163 -64.4545
69.9008 05.1696 70.3753
52.1421 59.8639 -06.6541

%% 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, allowing for neat and simple vectorized MATLAB code.

These two motivations are closely linked to two non-trivial tasks that have to be solved in order to generate maximally-distinct colors:
1. An algorithm to find the best color combination requires finding the global optimum, a task which grows exponentially with the number of requested colors and with the color gamut size. In MAXDISTCOLOR I use repeated application of a simple greedy algorithm to find the maximally-distinct colors: the repeated greedy algorithm is not particularly fast and is not a general solution for finding a global optimum, but luckily it gives good results for the regularly sampled RGB cube. Note that this algorithm contains no random numbers: it is entirely deterministic and repeatable.
2. Defining a true uniform colorspace: the venerable CIELAB (used by most existing tools I could find) is not really very uniform. For MAXDISTCOLOR I recommend OSA-UCS or CAM02-UCS or CAM16-UCS, all of which provide a more accurate measure of the color distance.

Cite As

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

MATLAB Release Compatibility
Created with R2015b
Compatible with R2009a and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!