EquiDistantColorGenerator
Version 2.0.0 (3.91 KB) by
Naveed Reza Aghamohammadi
Optimize your colored plots by using colors that have maximum contrast on a white background. Best for plotting in scientific publications.
%EQUIDISTANTCOLORGENERATOR Generate visually distinct, equidistant colors.
%
% [ColorMatrix, angleOnPallet] = EquiDistantColorGenerator(numberOfColors)
% [ColorMatrix, angleOnPallet] = EquiDistantColorGenerator(numberOfColors, colorSeed)
% [ColorMatrix, angleOnPallet] = EquiDistantColorGenerator(numberOfColors, colorSeed, outputType)
% [ColorMatrix, angleOnPallet] = EquiDistantColorGenerator(numberOfColors, colorSeed, outputType, plotIt)
%
% DESCRIPTION
% Returns a set of N visually distinct colors sampled (approximately)
% equidistantly from a fixed hue palette. Colors can be returned as RGB
% triples or HEX strings. Optionally supply a seed to reproduce the same
% palette across runs. Optionally display a quick preview plot.
%
% INPUTS
% numberOfColors : (required, positive integer)
% The number of colors N to generate.
%
% colorSeed : (optional, scalar numeric) [default: random seed]
% Seed used to initialize RNG for reproducible color selection. If
% empty or not provided, a random seed is chosen and printed to the
% command window so it can be reused later.
%
% outputType : (optional, char/string) [default: 'rgb']
% Output format for colors:
% 'rgb' : return N×3 double matrix with RGB values in [0,1].
% 'hex' : return N×1 cell array of '#RRGGBB' strings.
%
% plotIt : (optional, logical) [default: false]
% When true (or when no seed is provided), a simple line plot is
% shown to preview the generated colors and their order.
%
% OUTPUTS
% ColorMatrix :
% If outputType = 'rgb' -> N×3 matrix of doubles in [0,1].
% If outputType = 'hex' -> N×1 cell array of '#RRGGBB' strings.
%
% angleOnPallet : N×1 vector of hue “angles” (in degrees) corresponding
% to each returned color’s position on the internal palette.
%
% BEHAVIOR & NOTES
% • Determinism: Using the same (numberOfColors, colorSeed) pair returns
% the same ColorMatrix and angleOnPallet.
% • No seed provided: The function picks a random seed, prints it to the
% command window (e.g., “seed=1234”), and (by default) shows a preview
% plot so you can iterate until you like the palette.
% • Palette: Colors are chosen from a fixed 36-color palette spanning the
% hue circle. Indices are stepped to approximate equal spacing.
% • Legacy special case: For compatibility, if colorSeed == 0.84913 and
% numberOfColors == 4, the first three colors are fixed to a legacy set
% and the 4th is generated from the palette (see code block).
%
% EXAMPLES
% % 1) Random palette, preview shown, seed printed to command window:
% C = EquiDistantColorGenerator(5);
%
% % 2) Reproducible palette (no preview by default):
% C = EquiDistantColorGenerator(5, 897);
%
% % 3) HEX output:
% H = EquiDistantColorGenerator(6, [], 'hex');
%
% % 4) Force preview even with a seed:
% C = EquiDistantColorGenerator(4, 123, 'rgb', true);
%
% SEE ALSO
% colormap, rng, plot, hex2rgb
%
% AUTHOR
% Naveed Reza Aghamohammadi
%
% VERSION
% Initiated: 2022-12-13
% Header updated: 2025-11-13
%
% -------------------------------------------------------------------------
Cite As
Naveed Reza Aghamohammadi (2025). EquiDistantColorGenerator (https://uk.mathworks.com/matlabcentral/fileexchange/176048-equidistantcolorgenerator), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2022b
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Acknowledgements
Inspired by: rgb2hex and hex2rgb
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
