Main Content

hex2rgb

Convert hexadecimal color codes to RGB triplets

Since R2024a

    Description

    RGB = hex2rgb(hexStr) converts the specified hexadecimal color codes to RGB triplets. Specify hexStr as a string array or cell array of character vectors containing the hexadecimal color codes. Each hexadecimal color code must start with a hash symbol (#) followed by three or six hexadecimal digits. The function returns RGB as an m-by-3 matrix or m-by-n-by-3 array containing double values in the range [0, 1].

    example

    RGB = hex2rgb(hexStr,OutputType=datatype) specifies the data type for the RGB triplets. Valid values are "double", "single", "uint8", or "uint16". For example, hex2rgb("#FF0000",OutputType="uint8") returns the unsigned values [255 0 0].

    example

    Examples

    collapse all

    Create a string scalar containing a hexadecimal color code. Then convert it to an RGB triplet.

    hexStr = "#CD00EF";
    hex2rgb(hexStr)
    ans = 1×3
    
        0.8039         0    0.9373
    
    

    Now convert the same hexadecimal color code to unsigned 16-bit RGB values.

    hex2rgb(hexStr,OutputType="uint16")
    ans = 1x3 uint16 row vector
    
       52685       0   61423
    
    

    hex2rgb also accepts shorthand hexadecimal color codes. For example, convert the shorthand value for pure red to an RGB triplet.

    hexStr = "#F00";
    hex2rgb(hexStr)
    ans = 1×3
    
         1     0     0
    
    

    Create a string vector of six hexadecimal color codes, and convert its values to RGB triplets. The result is a 6-by-3 matrix, where each row, RGB(i,:), contains the red, green, and blue intensities for the color hexStr(i).

    hexStr = ["#0072BD" "#D95319" "#EDB120" "#7E2F8E" "#77AC30" "#4DBEEE"];
    RGB = hex2rgb(hexStr)
    RGB = 6×3
    
             0    0.4471    0.7412
        0.8510    0.3255    0.0980
        0.9294    0.6941    0.1255
        0.4941    0.1843    0.5569
        0.4667    0.6745    0.1882
        0.3020    0.7451    0.9333
    
    

    Create a 2-by-2 string array hexadecimal color codes, and convert it to RGB triplets. The result is a 2-by-2-by-3 array, where RGB(:,:,1) contains the red component for all four colors, RGB(:,:,2) contains the green component, and RGB(:,:,3) contains the blue component.

    hexStr = ["#0072BD" "#D95319"; "#EDB120" "#7E2F8E"];
    RGB = hex2rgb(hexStr)
    RGB = 
    RGB(:,:,1) =
    
             0    0.8510
        0.9294    0.4941
    
    
    RGB(:,:,2) =
    
        0.4471    0.3255
        0.6941    0.1843
    
    
    RGB(:,:,3) =
    
        0.7412    0.0980
        0.1255    0.5569
    
    

    Input Arguments

    collapse all

    Hexadecimal color codes, specified as a string scalar, string array, character vector, or cell array of character vectors. Specify each element of hexStr with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F.

    The hexadecimal digits describe a color in terms of its red, green, and blue components. For a three-digit code, the digits correspond to the red, green, and blue components, respectively. For a six-digit code, 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.

    Example: RGB = hex2rgb("#00F") returns the RGB triplet [0 0 1].

    Example: RGB = hex2rgb(["#0000FF"; "#994DCC"]) returns the 2-by-3 matrix [0 0 1; 0.6 0.3 0.8].

    Example: RGB = hex2rgb(["#0000FF" "#994DCC"; "#FFFF00" "#ABC123"]) returns a 2-by-2-by-3 image array.

    Data type of the output, specified as one of these values:

    • "double" or "single" — Double- or single-precision values in the range [0, 1]. A value of 0 corresponds to no color, and a value of 1 corresponds to the full intensity color for that component (red, green, or blue).

    • "uint8" — Unsigned 8-bit values in the range [0, 255]. A value of 0 corresponds to no color, and a value of 255 corresponds to the full intensity color for that component.

    • "uint16" — Unsigned 16-bit values in the range [0, 65535]. A value of 0 corresponds to no color, and a value of 65535 corresponds to the full intensity color for that component.

    Example: RGB = hex2rgb("#FF0000",OutputType="uint8") returns the unsigned 8-bit RGB values [255 0 0].

    Output Arguments

    collapse all

    RGB triplets, returned as a three-element row vector, an m-by-3 matrix, or an m-by-n-by-3 array, depending on the size of hexStr.

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

    • If hexStr is m-by-1, then RGB is m-by-3.

    • If hexStr is m-by-n, then RGB is m-by-n-by-3, where the pages along the third dimension correspond to the red, green, and blue components of the colors, respectively.

    Version History

    Introduced in R2024a