Convert between RGB and Color Names

버전 7.0.1 (538 KB) 작성자: Stephen23
RGB to color name. Color name to RGB. Supports many palettes: CSS, dvips, HTML, MATLAB, RAL, SVG, X11, xcolor, xkcd,...
다운로드 수: 3.2K
업데이트 날짜: 2024/10/18

라이선스 보기

편집자 메모: This file was selected as MATLAB Central Pick of the Week

One function converts between RGB and color names, in both directions.
The function COLORNAMES accepts either RGB values or color names as its inputs, and returns the color names and RGB values from the palette of your choice. The palette is selected by the first input, the trailing inputs are either color names or RGB values:
  • Input color name matching is generally case-insensitive with optional spaces, except for some palettes where whitespace or CamelCase is significant. Diacritics and other special characters are optional. If no exact match is found then the color names are filtered for all matching characters and the color name with the closest Levenshtein distance is returned.
  • Input RGB are matched to the closest palette colors using one of a number of standard color difference metrics. The supported color differences are: CIEDE2000, OkLab, DIN99, CIE94:1, CIE94:2 (default), CIE76 (i.e. CIELAB), CMC2:1, CMC1:1, and RGB.
For more information on color difference metrics: https://en.wikipedia.org/wiki/Color_difference
Whether matching color names or RGB, COLORNAMES always returns the same two output arguments:
  1. the color names (string array, or cell array of char vectors).
  2. a numeric array of the corresponding RGB values.
A list of the available palettes is returned by calling COLORNAMES with no inputs. A summary of all palettes is printed by calling COLORNAMES with no inputs and no outputs.
Suggestions of interesting palettes (of named colors) are very welcome!
Bonus Functions
  • COLORNAMES_CUBE shows palette colors in a 3D cube. The supported colorspaces: Lab, LCh, DIN99, HSV, OKLab, XYZ, and RGB.
  • COLORNAMES_VIEW shows palette colors in 2D axes, and sorting them by various colorspace dimensions (e.g. Lab, XYZ, YUV, HSV, or RGB) or alphanumerically by color name.
  • COLORNAMES_DELTAE creates a figure comparing the color difference metrics.
  • COLORNAMES_SEARCH (>=R2023b) creates a figure with interactive real-time color name matching, demonstrating the filtering by character and closest name selection using Levenshtein distance.
Examples
>> palettes = colornames()
palettes =
'Alphabet'
'AmstradCPC'
'AppleII'
'Bang'
'BS381C'
'CGA'
'Crayola'
'CSS'
'dvips'
'Foster'
'HTML4'
'ISCC'
'Kelly'
'LeCorbusier'
'MacBeth'
'MATLAB'
'Natural'
'OsXCrayons'
'PicoMiteVGA'
'PptHighlight'
'PptStandard'
'PWG'
'R'
'RAL'
'Resene'
'Resistor'
'SherwinWilliams'
'SVG'
'Tableau'
'Thesaurus'
'Trubetskoy'
'Wada'
'Werner'
'Wikipedia'
'Wolfram'
'X11'
'xcolor'
'xkcd'
>> colornames('Natural') % all color names for one palette
ans =
'Black'
'Blue'
'Green'
'Red'
'White'
'Yellow'
>> [names,rgb] = colornames('HTML4','blue','RED','Teal','olive')
names =
'Blue'
'Red'
'Teal'
'Olive'
rgb =
0 0 1.0000
1.0000 0 0
0 0.5020 0.5020
0.5020 0.5020 0
>> colornames('HTML4',[0,0.5,1;1,0.5,0]) % default deltaE = CIE94:2
ans =
'Blue'
'Red'
>> colornames('HTML4',[0,0.5,1;1,0.5,0],'rgb') % specify deltaE
ans =
'Teal'
'Olive'
>> colornames("MATLAB",'c','m','y','k')
ans =
"Cyan"
"Magenta"
"Yellow"
"Black"
>> [names,rgb] = colornames('MATLAB');
>> compose('%s %g %g %g',char(names),rgb)
ans =
'Black 0 0 0'
'Blue 0 0 1'
'Cyan 0 1 1'
'Green 0 1 0'
'Magenta 1 0 1'
'Red 1 0 0'
'White 1 1 1'
'Yellow 1 1 0'

인용 양식

Stephen23 (2024). Convert between RGB and Color Names (https://www.mathworks.com/matlabcentral/fileexchange/48155-convert-between-rgb-and-color-names), MATLAB Central File Exchange. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2010b
R2009b 이상 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
7.0.1

* Add PowerPoint palettes.
* Add "exact"/"closest" match indicator to COLORNAMES_SEARCH.
* Add error hypercode.
* Documentation improvements.

7.0.0

* Faster and more flexible color name matching algorithm.
* Add bonus function COLORNAMES_SEARCH showing real-time color name matching.
* Add PicoMiteVGA palette.

6.1.3

* Error message improvements
* Documentation improvements
* Leading index handling improvements
* Graphics: use CLA to clear axes

6.1.2

* fix description bullet points

6.1.1

* Correct description.

6.1.0

- Add palettes LeCorbusier, OsxCrayons, PWG, Wada, Werner.
- Add OKLab deltaE.
- Support both standard parameter sets for CMC deltaE.
- Support both standard parameter sets for CIE94 deltaE.
- Bonus functions accept string inputs.

6.0.0

* Accept STRING class text inputs.
* Returns STRING class colornames when palette is a STRING.
* Inbuilt NATSORT routine.
* More flexible color name matching.

5.0.1

Add notes on how to add a new color palette.

5.0.0

* Faster color name matching algorithm. Also more flexible, e.g.:
- characters with or without diacritic.
- optional parentheses.
- optional hyphen.
* Add error ID codes.
* VIEW: sort alphabetically excluding leading index.

4.0.1

* Improve layout of palette descriptions and error messages.
* Permanent scrollbar for much faster COLORNAMES_VIEW.

4.0.0

* Simplify .mat file structure.
* Add SherwinWilliams palette.

3.6.0

* Add DIN99 colorspace for viewing and deltaE calculation.
* Add palette Trubetskoy.

3.5.3

* Add "Thesaurus" and "Tableau" palettes.
* Add feature to print detailed list of supported palettes.
* Correct "xcolor" blue values.
* Now works with >=R2014b graphics engine.

3.5.2.1

* Improve online description.

3.5.2

* Remove non-ASCII characters from file.

3.5.1

* Fixed missing variable in COLORNAMES_CUBE
* Improved indexed-colorname regular expression.

3.5.0.0

* VIEW: show RGB value with click on color name.
* Combine into one resistor palette.
* Allow for negative leading integer.

3.4.0.0

* Change Lab illuminant to D65.
* Fix bug in camorbit for CUBE.
* Improve help examples.

3.3.0.0

* Add AmstradCPC palette.
* Match initial letters for palettes Alphabet, MATLAB, and Natural.

3.2.0.0

* Documentation changes.

3.1.0.0

* Add CIEDE2000 deltaE color distance calculation.
* Add COLORNAMES_CUBE function.
* Change input argument name from "scheme" to "palette".
* Improved HTML documentation.

3.0.0.0

* Total rewrite of the RGB matching algorithm.
* Add HTML documentation.

2.1.0.0

* Much more robust string matching using Levenshtein distance to pick closest colorname (Wagner–Fischer algorithm).
* Rename supplementary function (deltaE viewer) and add default values.

2.0.0.0

* Add lots of colorschemes.
* Add new fields to .MAT file: <spaces> and <indices>.

1.2.0.0

* Add Alphabet and Kelly colorschemes.

1.1.0.0

- Downloadable as toolbox

1.0.0.0