EquiDistantColorGenerator
Version 1.0.0 (3.89 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 Generates RGB-coded equidistant colors.
%
% ColorMatrix = EquiDistantColorGenerator(numberOfColors)
% ColorMatrix = EquiDistantColorGenerator(numberOfColors, colorSeed)
% ColorMatrix = EquiDistantColorGenerator(numberOfColors, colorSeed, outputType)
%
% This function returns a matrix of RGB color codes that are equidistant in color space,
% given the number of colors requested. It can optionally use a seed number to reproduce
% the same color pattern and specify the output format.
%
% INPUTS:
% numberOfColors - (Required) Integer specifying the number of colors to generate.
% Must be a positive integer.
%
% colorSeed - (Optional) Numeric seed to generate the same color pattern.
% If not provided or empty, a random seed is used.
% Using the same seed with the same number of colors will
% reproduce the same color sequence.
%
% If no seed number is provided, the function will plot the generated
% colors in a new figure window each time it is called. This allows you
% to visualize the color palette and its order. You can keep running the
% function (without a seed) until you are satisfied with the colors.
% The seed used to generate the colors will be displayed in the command window.
% To reuse that color palette, run the function again with that specific seed number.
%
% outputType - (Optional) String specifying the output format of colors.
% 'rgb' (default) returns an N-by-3 matrix of RGB values.
% 'hex' returns an N-by-1 cell array of hexadecimal color codes.
%
% OUTPUT:
% ColorMatrix - Depending on 'outputType':
% - If 'rgb', an N-by-3 matrix where each row is an RGB color code.
% - If 'hex', an N-by-1 cell array of hexadecimal color strings.
% N is the number of colors requested.
%
% USAGE:
% - Generate colors without specifying a seed (random colors each time):
% ColorMatrix = EquiDistantColorGenerator(5);
% % A figure window will open displaying the generated colors.
% % The seed used will be displayed in the command window.
% % You can keep running this until you are happy with the colors.
%
% - Generate colors with a specific seed to reproduce colors:
% ColorMatrix = EquiDistantColorGenerator(5, 897);
% % This will return the same color matrix every time it's run with the same seed and numberOfColors.
%
% - Generate colors and specify output type as hexadecimal codes:
% ColorMatrix = EquiDistantColorGenerator(5, 897, 'hex');
%
% EXAMPLES:
% % Example 1: Generate 3 equidistant RGB colors with a random seed
% ColorMatrix = EquiDistantColorGenerator(3);
% % Keep running until you are satisfied with the colors, noting the seed displayed.
%
% % Example 2: Generate 4 equidistant colors with a specific seed
% ColorMatrix = EquiDistantColorGenerator(4, 12345);
%
% % Example 3: Generate 6 equidistant colors in hexadecimal format
% ColorMatrix = EquiDistantColorGenerator(6, [], 'hex');
%
% % Example 4: Plotting using the generated colors
% ColorMatrix = EquiDistantColorGenerator(3);
% x = linspace(0, 2*pi, 100);
% y1 = sin(x);
% y2 = cos(x);
% y3 = sin(2*x);
% plot(x, y1, 'Color', ColorMatrix(1,:), 'LineWidth', 2); hold on;
% plot(x, y2, 'Color', ColorMatrix(2,:), 'LineWidth', 2);
% plot(x, y3, 'Color', ColorMatrix(3,:), 'LineWidth', 2);
% legend('sin(x)', 'cos(x)', 'sin(2x)');
%
% NOTES:
% - If 'colorSeed' is not provided or empty, the function will generate a random seed.
% The seed used will be displayed in the command window, allowing you to reuse it
% if you wish to reproduce the same colors.
%
% - Additionally, if no seed is provided, the function will plot the generated colors
% in a new figure window each time it is called. You can keep running the function
% until you are satisfied with the color palette and its order.
% Once you find a palette you like, note the seed displayed, and use it as 'colorSeed'
% in future runs to reproduce that color palette.
%
% - The function uses a predefined set of colors and selects equidistant colors
% based on the requested number. This ensures that the colors are visually distinct.
%
% - The 'outputType' parameter allows flexibility in how the colors are returned,
% accommodating different usage scenarios (e.g., plotting, GUI design).
%
% - If 'numberOfColors' is greater than the available unique colors, the function
% will cycle through the color list.
%
% SEE ALSO:
% colormap, rng, rand, plot
%
% REFERENCES:
% - Color palette resources:
% https://www.learnui.design/blog/the-hsb-color-system-practicioners-primer.html
% https://graphicdesign.stackexchange.com/questions/115546/color-wheel-with-constant-subjective-hue-gradient-better-than-cielab
%
% AUTHOR:
% Naveed Reza Aghamohammadi
%
% VERSION:
% Initiated on 2022-12-13
Cite As
Naveed Reza Aghamohammadi (2024). EquiDistantColorGenerator (https://www.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.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0 |