Main Content

fliplightness

Darken light colors and lighten dark colors

Since R2025a

    Description

    newcolors = fliplightness(colors) darkens the light colors and lightens the dark colors specified in colors, which can be a matrix of RGB triplets, a truecolor image, or an array of hexadecimal color codes or color names.

    example

    Examples

    collapse all

    Plot a dark blue line in a light-themed figure.

    f = figure(Theme="light");
    p = plot([0 3 1 6 4],LineWidth=2,Color="#0023ED");

    Figure contains an axes object. The axes object contains an object of type line.

    Change the theme of the figure to "dark".

    f.Theme="dark";

    Figure contains an axes object. The axes object contains an object of type line.

    Lighten the color of the line.

    newcolor = fliplightness("#0023ED");
    p.Color = newcolor;

    Figure contains an axes object. The axes object contains an object of type line.

    You can change the color palette of a plot by calling the colororder function. Then you can use the fliplightness function to update the colors for dark theme.

    Plot seven lines in a light-themed figure. Use the "dye" color palette by calling the colororder function. Returning the palette as matrix C, which is a 7-by-3 matrix of RGB triplets.

    f = figure(Theme="light");
    ax = axes;
    y = [6 5 4 3 2 1 0; 7 6 5 4 3 2 1];
    plot(y,LineWidth=2)
    C = colororder(ax,"dye");

    Figure contains an axes object. The axes object contains 7 objects of type line.

    Change the theme of the figure to "dark". Some of the lines are slightly less visible against the black background of the figure.

    f.Theme ="dark";

    Figure contains an axes object. The axes object contains 7 objects of type line.

    Lighten the dark colors of C (rows 1, 3, 4, and 6), and then call the colororder function again to update the colors of the lines.

    toodark = [1 3 4 6];
    C(toodark,:) = fliplightness(C(toodark,:));
    colororder(ax,C)

    Figure contains an axes object. The axes object contains 7 objects of type line.

    Input Arguments

    collapse all

    Colors to lighten or darken, specified as a matrix of RGB triplets, a truecolor image, or an array of hexadecimal color codes or color names.

    • RGB triplet — A three-element row vector whose elements specify the intensities of the red, green, and blue components of a color. For m colors, specify an m-by-3 matrix. The intensities must within the range supported by the data type. For double and single values, the range is [0, 1]. For unsigned 8-bit integer values, the range is [0, 255].

    • Truecolor image — An m-by-n-by-3 array of color pixel values. The color of each pixel is determined by the combination of the red, green, and blue intensities stored in each color plane at the pixel's location. For example, the red, green, and blue color components of the pixel (10, 5) in an RGB image X are stored in X(10,5,1), X(10,5,2), and X(10,5,3), respectively.

    • Hexadecimal color code — A string scalar or character vector that starts with a hash symbol (#) followed by three or six hexadecimal digits, which can range from 0 to F. The values are not case sensitive. Therefore, the color codes "#FF8800", "#ff8800", "#F80", and "#f80" are equivalent. To specify multiple hexadecimal color codes, use a string array or a cell array of character vectors.

    Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and hexadecimal color codes.

    Color NameShort NameRGB TripletHexadecimal Color CodeAppearance
    "red""r"[1 0 0]"#FF0000"

    Sample of the color red

    "green""g"[0 1 0]"#00FF00"

    Sample of the color green

    "blue""b"[0 0 1]"#0000FF"

    Sample of the color blue

    "cyan" "c"[0 1 1]"#00FFFF"

    Sample of the color cyan

    "magenta""m"[1 0 1]"#FF00FF"

    Sample of the color magenta

    "yellow""y"[1 1 0]"#FFFF00"

    Sample of the color yellow

    "black""k"[0 0 0]"#000000"

    Sample of the color black

    "white""w"[1 1 1]"#FFFFFF"

    Sample of the color white

    Example: fliplightness([0 0 1])

    Example: fliplightness([0 0 1; 0 0 0.5])

    Example: fliplightness(["#FF0076","#0000FF"])

    Example: fliplightness(["blue","black"])

    Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

    Output Arguments

    collapse all

    Lightness-adjusted colors, returned as a matrix of RGB triplets, a truecolor image, or an array of hexadecimal color codes that has the same size, shape, and data type as the input colors. If you specify a color name as input, fliplightness returns a matrix of RGB triplets.

    Calling fliplightness and passing the result to fliplightness a second time might not return the original color value.

    Algorithms

    fliplightness performs a color space conversion to access the lightness information of a color. Then it adjusts the lightness without affecting the hue.

    Version History

    Introduced in R2025a