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 twoColor
s 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
sourceimpl Color
impl Color
pub fn from_hsla(hue: f64, saturation: f64, lightness: f64, alpha: f64) -> Color
sourcepub fn from_rgba(r: u8, g: u8, b: u8, alpha: f64) -> Color
pub fn from_rgba(r: u8, g: u8, b: u8, alpha: f64) -> Color
Create a Color
from integer RGB values between 0 and 255 and a floating
point alpha value between 0.0 and 1.0.
sourcepub fn from_rgb(r: u8, g: u8, b: u8) -> Color
pub fn from_rgb(r: u8, g: u8, b: u8) -> Color
Create a Color
from integer RGB values between 0 and 255.
sourcepub fn from_rgba_float(r: f64, g: f64, b: f64, alpha: f64) -> Color
pub fn from_rgba_float(r: f64, g: f64, b: f64, alpha: f64) -> Color
Create a Color
from RGB and alpha values between 0.0 and 1.0. Values outside this range
will be clamped.
sourcepub fn from_rgb_float(r: f64, g: f64, b: f64) -> Color
pub fn from_rgb_float(r: f64, g: f64, b: f64) -> Color
Create a Color
from RGB values between 0.0 and 1.0. Values outside this range will be
clamped.
sourcepub fn from_xyz(x: f64, y: f64, z: f64, alpha: f64) -> Color
pub fn from_xyz(x: f64, y: f64, z: f64, alpha: f64) -> Color
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:
sourcepub fn from_lms(l: f64, m: f64, s: f64, alpha: f64) -> Color
pub fn from_lms(l: f64, m: f64, s: f64, alpha: f64) -> Color
Create a Color
from LMS coordinates. This is the matrix inverse of the matrix that
appears in to_lms
.
sourcepub fn from_lab(l: f64, a: f64, b: f64, alpha: f64) -> Color
pub fn from_lab(l: f64, a: f64, b: f64, alpha: f64) -> Color
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.
sourcepub fn from_lch(l: f64, c: f64, h: f64, alpha: f64) -> Color
pub fn from_lch(l: f64, c: f64, h: f64, alpha: f64) -> Color
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.
sourcepub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Color
pub fn from_cmyk(c: f64, m: f64, y: f64, k: f64) -> Color
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
sourcepub fn to_hsla(&self) -> HSLA
pub fn to_hsla(&self) -> HSLA
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.
sourcepub fn to_hsl_string(&self, format: Format) -> String
pub fn to_hsl_string(&self, format: Format) -> String
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.
sourcepub fn to_rgba(&self) -> RGBA<u8>
pub fn to_rgba(&self) -> RGBA<u8>
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.
sourcepub fn to_rgb_string(&self, format: Format) -> String
pub fn to_rgb_string(&self, format: Format) -> String
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.
sourcepub fn to_cmyk(&self) -> CMYK
pub fn to_cmyk(&self) -> CMYK
Convert a Color
to its cyan, magenta, yellow, and black values. The CMYK
values are floats smaller than or equal to 1.0.
sourcepub fn to_cmyk_string(&self, format: Format) -> String
pub fn to_cmyk_string(&self, format: Format) -> String
Format the color as a CMYK-representation string (cmyk(0, 50, 100, 100)
).
sourcepub fn to_rgb_float_string(&self, format: Format) -> String
pub fn to_rgb_float_string(&self, format: Format) -> String
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.
sourcepub fn to_rgb_hex_string(&self, leading_hash: bool) -> String
pub fn to_rgb_hex_string(&self, leading_hash: bool) -> String
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.
sourcepub fn to_rgba_float(&self) -> RGBA<f64>
pub fn to_rgba_float(&self) -> RGBA<f64>
Convert a Color
to its red, green, blue and alpha values. All numbers are from the range
between 0.0 and 1.0.
sourcepub fn to_lms(&self) -> LMS
pub fn to_lms(&self) -> LMS
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
sourcepub fn to_lab_string(&self, format: Format) -> String
pub fn to_lab_string(&self, format: Format) -> String
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.
sourcepub fn to_lch_string(&self, format: Format) -> String
pub fn to_lch_string(&self, format: Format) -> String
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.
Navy (#000080
)
sourcepub fn graytone(lightness: f64) -> Color
pub fn graytone(lightness: f64) -> Color
Create a gray tone from a lightness value (0.0 is black, 1.0 is white).
sourcepub fn rotate_hue(&self, delta: f64) -> Color
pub fn rotate_hue(&self, delta: f64) -> Color
Rotate along the “hue” axis.
sourcepub fn complementary(&self) -> Color
pub fn complementary(&self) -> Color
Get the complementary color (hue rotated by 180°).
sourcepub fn lighten(&self, f: f64) -> Color
pub fn lighten(&self, f: f64) -> Color
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.
sourcepub fn darken(&self, f: f64) -> Color
pub fn darken(&self, f: f64) -> Color
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.
sourcepub fn saturate(&self, f: f64) -> Color
pub fn saturate(&self, f: f64) -> Color
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.
sourcepub fn desaturate(&self, f: f64) -> Color
pub fn desaturate(&self, f: f64) -> Color
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.
sourcepub fn simulate_colorblindness(&self, cb_ty: ColorblindnessType) -> Color
pub fn simulate_colorblindness(&self, cb_ty: ColorblindnessType) -> Color
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
.
sourcepub fn to_gray(&self) -> Color
pub fn to_gray(&self) -> Color
Convert a color to a gray tone with the same perceived luminance (see luminance
).
sourcepub fn brightness(&self) -> f64
pub fn brightness(&self) -> f64
The percieved brightness of the color (A number between 0.0 and 1.0).
sourcepub fn is_light(&self) -> bool
pub fn is_light(&self) -> bool
Determine whether a color is perceived as a light color (perceived brightness is larger than 0.5).
sourcepub fn luminance(&self) -> f64
pub fn luminance(&self) -> f64
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
sourcepub fn contrast_ratio(&self, other: &Color) -> f64
pub fn contrast_ratio(&self, other: &Color) -> f64
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
sourcepub fn text_color(&self) -> Color
pub fn text_color(&self) -> Color
Return a readable foreground text color (either black
or white
) for a
given background color.
sourcepub fn distance_delta_e_cie76(&self, other: &Color) -> f64
pub fn distance_delta_e_cie76(&self, other: &Color) -> f64
Compute the perceived ‘distance’ between two colors according to the CIE76 delta-E standard. A distance below ~2.3 is not noticable.
sourcepub fn distance_delta_e_ciede2000(&self, other: &Color) -> f64
pub fn distance_delta_e_ciede2000(&self, other: &Color) -> f64
Compute the perceived ‘distance’ between two colors according to the CIEDE2000 delta-E standard.
Trait Implementations
sourceimpl AnsiColor for Color
impl AnsiColor for Color
sourcefn from_ansi_8bit(code: u8) -> Color
fn from_ansi_8bit(code: u8) -> Color
Create a color from an 8-bit ANSI escape code
sourcefn to_ansi_8bit(&self) -> u8
fn to_ansi_8bit(&self) -> u8
Approximate a color by its closest 8-bit ANSI color (as measured by the perceived color distance).
sourcefn to_ansi_sequence(&self, mode: Mode) -> String
fn to_ansi_sequence(&self, mode: Mode) -> String
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
sourceimpl ToAnsiStyle for Color
impl ToAnsiStyle for Color
fn ansi_style(&self) -> Style
Auto Trait Implementations
impl RefUnwindSafe for Color
impl Send for Color
impl Sync for Color
impl Unpin for Color
impl UnwindSafe for Color
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more