rgb2hex - Convert RGB triplets to hexadecimal color codes - MATLAB (original) (raw)

Convert RGB triplets to hexadecimal color codes

Since R2024a

Syntax

Description

[hexStr](#mw%5F56065af3-c7f5-4bad-a696-d32a193636c1) = rgb2hex([RGB](#mw%5F68a944d3-aa1c-4c5a-a0db-6b03e5ec4e7f)) 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 inhexStr starts with a hash symbol (#) followed by six hexadecimal digits.

example

[hexStr](#mw%5F56065af3-c7f5-4bad-a696-d32a193636c1) = rgb2hex([RGB](#mw%5F68a944d3-aa1c-4c5a-a0db-6b03e5ec4e7f),Shorthand=[tf](#mw%5F5a480cf5-a416-48ed-9986-4c32603b5497)) 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.

example

Examples

collapse all

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

RGB = [0.60 0.30 0.80]; rgb2hex(RGB)

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

RGB = uint8([153 77 204]); rgb2hex(RGB)

Now calculate the shorthand hexadecimal color code for this color.

rgb2hex(RGB,Shorthand=true)

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 = 6×1 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.

img = imread("peppers.png"); size(img)

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:

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 logical1 (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.

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