# rgb2hex

Convert RGB triplets to hexadecimal color codes

Since R2024a

## Description

example

hexStr = rgb2hex(RGB) converts the specified RGB triplets to strings containing six-digit hexadecimal color codes. To convert m RGB triplets, specify RGB as an m-by-3 matrix. To convert an m-by-n image, specify RGB as an m-by-n-by-3 array. Each value returned in hexStr starts with a hash symbol (#) followed by six hexadecimal digits.

example

hexStr = rgb2hex(RGB,Shorthand=tf) specifies whether to return the hexadecimal color codes in shorthand notation. By default, the output contains the six-digit codes. Shorthand values contain only three hexadecimal digits (one digit for each color component). As a consequence, these values can be less accurate than the six-digit notation.

## Examples

collapse all

Create an RGB triplet and convert it to a hexadecimal color code.

RGB = [0.60 0.30 0.80];
rgb2hex(RGB)
ans =
"#994DCC"

Convert the equivalent uint8 RGB triplet to a hexadecimal color code.

RGB = uint8([153 77 204]);
rgb2hex(RGB)
ans =
"#994DCC"

Now calculate the shorthand hexadecimal color code for this color.

rgb2hex(RGB,Shorthand=true)
ans =
"#95C"

Create a 6-by-3 matrix of RGB triplets and convert its values to hexadecimal color codes. Each row of the matrix corresponds to a different color.

RGB = [1 0 0
1 1 0
1 1 1
0 1 1
0 0 1
0 0 0];
rgb2hex(RGB)
ans = 6x1 string
"#FF0000"
"#FFFF00"
"#FFFFFF"
"#00FFFF"
"#0000FF"
"#000000"

Read peppers.png into the variable img. The size of this image is 384-by-512-by-3, where the third dimension contains the red, green, and blue intensities for each pixel.

size(img)
ans = 1×3

384   512     3

Convert the image to hexadecimal color codes. hexStr is returned as a string array with the same number of rows and columns as the image.

hexStr = rgb2hex(img);
whos hexStr
Name          Size                Bytes  Class     Attributes

hexStr      384x512            10616944  string

## Input Arguments

collapse all

RGB values, specified as an m-by-3 matrix for m colors or an m-by-n-by-3 array for an m-by-n image. The RGB values can have any of four data types:

• double or single values in the range [0, 1]

• uint8 values in the range [0, 255]

• uint16 values in the range [0, 65535]

Example: hexStr = rgb2hex([1 0 0]) returns the hexadecimal value for pure red, "#FF0000".

Example: hexStr = rgb2hex(uint8([255 255 0])) returns the hexadecimal value for yellow, "#FFFF00".

Example: hexStr = rgb2hex(imread("peppers.png")) returns the hexadecimal values for the image peppers.png.

Data Types: single | double | uint8 | uint16

Return the output in shorthand notation, specified as numeric or logical 1 (true) or 0 (false).

rgb2hex returns the hexadecimal color codes in shorthand notation when tf is 1 or true. Otherwise, the output contains the six-digit codes.

Shorthand values contain only three hexadecimal digits (one digit for each color component). As a consequence, these values might be less accurate than the six-digit notation.

Example: hexStr = rgb2hex([1 0 0],Shorthand=false) returns "#FF0000".

Example: hexStr = rgb2hex([1 0 0],Shorthand=true) returns "#F00".

Example: hexStr = rgb2hex([0.1 0.3 0.9],Shorthand=0) returns "#1A4DE6".

Example: hexStr = rgb2hex([0.1 0.3 0.9],Shorthand=1) returns "#25E".

## Output Arguments

collapse all

Hexadecimal color codes, returned as a string scalar, string vector, or string array, depending on the shape of the RGB data.

• If RGB is a three-element row vector, then hexStr is a string scalar.

• If RGB is an m-by-3 matrix of RGB triplets, then hexStr is an m-by-1 string vector.

• If RGB is an m-by-n-by-3 image, then hexStr is an m-by-n string array.

Each element of hexStr starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F.

Six-digit hexadecimal color codes contain two pairs of digits for each color component. The first pair of digits corresponds to the red component, the second pair corresponds to the green component, and the third pair corresponds to the blue component. Three-digit (shorthand) values contain only one digit per color component.

## Version History

Introduced in R2024a