pub struct Color { /* private fields */ }
Expand description

The representation of a color.

Note:

  • Colors outside the sRGB gamut (which cannot be displayed on a typical computer screen) can not be represented by Color.
  • The PartialEq instance compares two Colors by comparing their (integer) RGB values. This is different from comparing the HSL values. For example, HSL has many different representations of black (arbitrary hue and saturation values).

Implementations

Create a Color from integer RGB values between 0 and 255 and a floating point alpha value between 0.0 and 1.0.

Create a Color from integer RGB values between 0 and 255.

Create a Color from RGB and alpha values between 0.0 and 1.0. Values outside this range will be clamped.

Create a Color from RGB values between 0.0 and 1.0. Values outside this range will be clamped.

Create a Color from XYZ coordinates in the CIE 1931 color space. Note that a Color always represents a color in the sRGB gamut (colors that can be represented on a typical computer screen) while the XYZ color space is bigger. This function will tend to create fully saturated colors at the edge of the sRGB gamut if the coordinates lie outside the sRGB range.

See:

Create a Color from LMS coordinates. This is the matrix inverse of the matrix that appears in to_lms.

Create a Color from L, a and b coordinates coordinates in the Lab color space. Note: See documentation for from_xyz. The same restrictions apply here.

See: https://en.wikipedia.org/wiki/Lab_color_space

Create a Color from lightness, chroma and hue coordinates in the CIE LCh color space. This is a cylindrical transform of the Lab color space. Note: See documentation for from_xyz. The same restrictions apply here.

See: https://en.wikipedia.org/wiki/Lab_color_space

Create a Color from the four colours of the CMYK model: Cyan, Magenta, Yellow and Black. The CMYK colours are subtractive. This means the colours get darker as you blend them together

Convert a Color to its hue, saturation, lightness and alpha values. The hue is given in degrees, as a number between 0.0 and 360.0. Saturation, lightness and alpha are numbers between 0.0 and 1.0.

Format the color as a HSL-representation string (hsla(123, 50.3%, 80.1%, 0.4)). If the alpha channel is 1.0, the simplified hsl() format will be used instead.

Convert a Color to its red, green, blue and alpha values. The RGB values are integers in the range from 0 to 255. The alpha channel is a number between 0.0 and 1.0.

Format the color as a RGB-representation string (rgba(255, 127, 0, 0.5)). If the alpha channel is 1.0, the simplified rgb() format will be used instead.

Convert a Color to its cyan, magenta, yellow, and black values. The CMYK values are floats smaller than or equal to 1.0.

Format the color as a CMYK-representation string (cmyk(0, 50, 100, 100)).

Format the color as a floating point RGB-representation string (rgb(1.0, 0.5, 0)). If the alpha channel is 1.0, the simplified rgb() format will be used instead.

Format the color as a RGB-representation string (#fc0070). The output will contain 6 hex digits if the alpha channel is 1.0, or 8 hex digits otherwise.

Convert a Color to its red, green, blue and alpha values. All numbers are from the range between 0.0 and 1.0.

Return the color as an integer in RGB representation (0xRRGGBB)

Get XYZ coordinates according to the CIE 1931 color space.

See:

Get coordinates according to the LSM color space

See https://en.wikipedia.org/wiki/LMS_color_space for info on the color space as well as an algorithm for converting from CIE XYZ

Get L, a and b coordinates according to the Lab color space.

See: https://en.wikipedia.org/wiki/Lab_color_space

Format the color as a Lab-representation string (Lab(41, 83, -93, 0.5)). If the alpha channel is 1.0, it won’t be included in the output.

Get L, C and h coordinates according to the CIE LCh color space.

See: https://en.wikipedia.org/wiki/Lab_color_space

Format the color as a LCh-representation string (LCh(0.3, 0.2, 0.1, 0.5)). If the alpha channel is 1.0, it won’t be included in the output.

Pure black.

Pure white.

Red (#ff0000)

Green (#008000)

Blue (#0000ff)

Yellow (#ffff00)

Fuchsia (#ff00ff)

Aqua (#00ffff)

Lime (#00ff00)

Maroon (#800000)

Olive (#808000)

Navy (#000080)

Purple (#800080)

Teal (#008080)

Silver (#c0c0c0)

Gray (#808080)

Create a gray tone from a lightness value (0.0 is black, 1.0 is white).

Rotate along the “hue” axis.

Get the complementary color (hue rotated by 180°).

Lighten a color by adding a certain amount (number between -1.0 and 1.0) to the lightness channel. If the number is negative, the color is darkened.

Darken a color by subtracting a certain amount (number between -1.0 and 1.0) from the lightness channel. If the number is negative, the color is lightened.

Increase the saturation of a color by adding a certain amount (number between -1.0 and 1.0) to the saturation channel. If the number is negative, the color is desaturated.

Decrease the saturation of a color by subtracting a certain amount (number between -1.0 and 1.0) from the saturation channel. If the number is negative, the color is saturated.

Adjust the long-, medium-, and short-wavelength cone perception of a color to simulate what a colorblind person sees. Since there are multiple kinds of colorblindness, the desired kind must be specified in cb_ty.

Convert a color to a gray tone with the same perceived luminance (see luminance).

The percieved brightness of the color (A number between 0.0 and 1.0).

See: https://www.w3.org/TR/AERT#color-contrast

Determine whether a color is perceived as a light color (perceived brightness is larger than 0.5).

The relative brightness of a color (normalized to 0.0 for darkest black and 1.0 for lightest white), according to the WCAG definition.

See: https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef

Contrast ratio between two colors as defined by the WCAG. The ratio can range from 1.0 to 21.0. Two colors with a contrast ratio of 4.5 or higher can be used as text color and background color and should be well readable.

https://www.w3.org/TR/2008/REC-WCAG20-20081211/#contrast-ratiodef

Return a readable foreground text color (either black or white) for a given background color.

Compute the perceived ‘distance’ between two colors according to the CIE76 delta-E standard. A distance below ~2.3 is not noticable.

See: https://en.wikipedia.org/wiki/Color_difference

Compute the perceived ‘distance’ between two colors according to the CIEDE2000 delta-E standard.

See: https://en.wikipedia.org/wiki/Color_difference

Mix two colors by linearly interpolating between them in the specified color space. For the angle-like components (hue), the shortest path along the unit circle is chosen.

Alpha composite two colors, placing the second over the first.

Trait Implementations

Create a color from an 8-bit ANSI escape code

See: https://en.wikipedia.org/wiki/ANSI_escape_code

Approximate a color by its closest 8-bit ANSI color (as measured by the perceived color distance).

See: https://en.wikipedia.org/wiki/ANSI_escape_code

Return an ANSI escape sequence in 8-bit or 24-bit representation:

  • 8-bit: ESC[38;5;CODEm, where CODE represents the color.
  • 24-bit: ESC[38;2;R;G;Bm, where R, G, B represent 8-bit RGB values

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

The associated error which can be returned from parsing.

Parses a string s to return a value of this type. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.