4. Color Conversion Tools

MATLAB comes with very few named colors. Although any color that you can figure out the RGB (red-green-blue) color values for can be used, it’s not always convenient to figure these out. For example,

>> x = linspace(0, 5, 101);
>> plot(x, sin(x), 'Color', [1,0.65,0])

plots a sine curve with an orange color. An alternative using the function html2rgb() is to use the following command.

>> plot(x, sin(s), 'Color', html2rgb('Orange'))

This version of the code is somewhat longer, but easier to remember and much easier to read. In addition, wherever colors can be specified in set_plot commands, they can always be given as names or RGB codes. For example, the colormap can be changed to an indigo-themed monochrome map using the following command.

>> contourf(peaks(50))
>> set_colormap({'w', 'Indigo', 'k'})

See set_colormap() for more information about that command.

4.1. Examples

This example demonstrates the usual output of html2rgb().

>> html2rgb('DodgerBlue')
ans =
    0.1176    0.5647    1.0000

Shorter names are also available.

>> html2rgb('y')
ans =
     1     1     0
>> html2rgb('yellow')
ans =
     1     1     0

Because valid RGB color codes are returned when input, the function can safely be nested.

>> html2rgb(html2rgb('Coral'))
ans =
    1.0000    0.4980    0.3137

A single number as input is taken as grayscale.

>> html2rgb(0.3)
ans =
    0.3000    0.3000    0.3000

Table Of Contents

Previous topic

3. Color Map Customization

Next topic

5. Tests

This Page