pub trait Color {
type Alpha: Color;
Show 18 methods
fn to_css(self) -> String;
fn to_hex(self) -> String;
fn to_rgb(self) -> RGB;
fn to_rgba(self) -> RGBA;
fn to_hsl(self) -> HSL;
fn to_hsla(self) -> HSLA;
fn saturate(self, amount: Ratio) -> Self;
fn desaturate(self, amount: Ratio) -> Self;
fn lighten(self, amount: Ratio) -> Self;
fn darken(self, amount: Ratio) -> Self;
fn fadein(self, amount: Ratio) -> Self::Alpha;
fn fadeout(self, amount: Ratio) -> Self::Alpha;
fn fade(self, amount: Ratio) -> Self::Alpha;
fn spin(self, amount: Angle) -> Self;
fn mix<T: Color>(self, other: T, weight: Ratio) -> Self::Alpha;
fn tint(self, weight: Ratio) -> Self;
fn shade(self, weight: Ratio) -> Self;
fn greyscale(self) -> Self;
}
Expand description
A trait that can be used for converting between different color models and performing various transformations on them.
Required Associated Types
Required Methods
sourcefn to_css(self) -> String
fn to_css(self) -> String
Converts self
to its CSS string format.
Examples
use farver::{Color, rgb, rgba};
let salmon = rgb(250, 128, 114);
let opaque_salmon = rgba(250, 128, 114, 0.50);
assert_eq!(salmon.to_css(), "rgb(250, 128, 114)");
assert_eq!(opaque_salmon.to_css(), "rgba(250, 128, 114, 0.50)");
sourcefn to_hex(self) -> String
fn to_hex(self) -> String
Converts self
to a hex string in the format #rrggbb without
alpha, and #rrggbbaa with alpha.
This will always be lowercase.
Example
use farver::{Color, rgb, rgba};
let salmon = rgb(250, 128, 114);
let opaque_salmon = rgba(250, 128, 114, 0.50);
assert_eq!(salmon.to_hex(), "#fa8072");
assert_eq!(opaque_salmon.to_hex(), "#fa807280");
sourcefn to_rgb(self) -> RGB
fn to_rgb(self) -> RGB
Converts self
into its RGB representation.
When converting from a color model that supports an alpha channel
(e.g. RGBA), the alpha value will not be preserved.
Examples
use farver::{Color, rgb, rgba};
let opaque_tomato = rgba(255, 99, 71, 0.5);
assert_eq!(opaque_tomato.to_rgb(), rgb(255, 99, 71));
sourcefn to_rgba(self) -> RGBA
fn to_rgba(self) -> RGBA
Converts self
into its RGBA representation.
When converting from a color model that does not supports an alpha channel
(e.g. RGB), it will be treated as fully opaque.
Examples
use farver::{Color, rgb, rgba};
let tomato = rgb(255, 99, 71);
assert_eq!(tomato.to_rgba(), rgba(255, 99, 71, 1.0));
sourcefn to_hsl(self) -> HSL
fn to_hsl(self) -> HSL
Converts self
into its HSL representation.
When converting from a color model that supports an alpha channel
(e.g. RGBA), the alpha value will not be preserved.
Examples
use farver::{Color, rgb, rgba, hsl};
let tomato = rgb(255, 99, 71);
let opaque_tomato = rgba(255, 99, 71, 0.5);
assert_eq!(tomato.to_hsl(), hsl(9, 100, 64));
assert_eq!(opaque_tomato.to_hsl(), hsl(9, 100, 64));
sourcefn to_hsla(self) -> HSLA
fn to_hsla(self) -> HSLA
Converts self
into its HSLA representation.
When converting from a color model that does not supports an alpha channel
(e.g. RGB), it will be treated as fully opaque.
Examples
use farver::{Color, rgb, rgba, hsl, hsla};
let tomato = rgb(255, 99, 71);
let opaque_tomato = rgba(255, 99, 71, 0.5);
assert_eq!(tomato.to_hsla(), hsla(9, 100, 64, 1.0));
assert_eq!(opaque_tomato.to_hsla(), hsla(9, 100, 64, 0.5));
sourcefn saturate(self, amount: Ratio) -> Self
fn saturate(self, amount: Ratio) -> Self
Increases the saturation of self
by an absolute amount.
Operates on the color within its HSL representation and preserves any existing alpha channel.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, hsla, percent};
let salmon = hsla(6, 93, 71, 1.0);
let cornflower_blue = rgb(100, 149, 237);
assert_eq!(salmon.saturate(percent(7)), hsla(6, 100, 71, 1.0));
assert_eq!(cornflower_blue.saturate(percent(10)), rgb(92, 146, 246));
sourcefn desaturate(self, amount: Ratio) -> Self
fn desaturate(self, amount: Ratio) -> Self
Decreases the saturation of self
by an absolute amount.
Operates on the color within its HSL representation and preserves any existing alpha channel.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, percent};
let tomato = rgba(255, 99, 71, 1.0);
let cornflower_blue = rgb(100, 149, 237);
assert_eq!(tomato.desaturate(percent(10)), rgba(246, 105, 80, 1.0));
assert_eq!(cornflower_blue.desaturate(percent(33)), rgb(129, 157, 209));
sourcefn lighten(self, amount: Ratio) -> Self
fn lighten(self, amount: Ratio) -> Self
Increases the lightness of self
by an absolute amount.
Operates on the color within its HSL representation and preserves any existing alpha channel.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, percent};
let tomato = rgba(255, 99, 71, 1.0);
let cornflower_blue = rgb(100, 149, 237);
assert_eq!(tomato.lighten(percent(20)), rgba(255, 185, 173, 1.0));
assert_eq!(cornflower_blue.lighten(percent(33)), rgb(251, 253, 255));
sourcefn darken(self, amount: Ratio) -> Self
fn darken(self, amount: Ratio) -> Self
Decreases the lightness of self
by an absolute amount.
Operates on the color within its HSL representation and preserves any existing alpha channel.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, percent};
let tomato = rgba(255, 99, 71, 1.0);
let cornflower_blue = rgb(100, 149, 237);
assert_eq!(tomato.darken(percent(20)), rgba(224, 34, 0, 1.0));
assert_eq!(cornflower_blue.darken(percent(33)), rgb(18, 65, 152));
sourcefn fadein(self, amount: Ratio) -> Self::Alpha
fn fadein(self, amount: Ratio) -> Self::Alpha
Decreases the transparency (or increase the opacity) of self
, making it more opaque.
For opqaue colors, converts into the alpha equivalent of self
, and then increases the opacity.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, percent};
let tomato = rgba(255, 99, 71, 0.25);
let cornflower_blue = rgb(100, 149, 237);
assert_eq!(tomato.fadein(percent(25)), rgba(255, 99, 71, 0.5));
assert_eq!(cornflower_blue.fadein(percent(75)), rgba(100, 149, 237, 1.0));
sourcefn fadeout(self, amount: Ratio) -> Self::Alpha
fn fadeout(self, amount: Ratio) -> Self::Alpha
Increases the transparency (or decrease the opacity) of self
, making it less opaque.
For opqaue colors, converts into the alpha equivalent of self
, and then decreases the opacity.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, percent};
let tomato = rgba(255, 99, 71, 0.5);
let cornflower_blue = rgb(100, 149, 237);
assert_eq!(tomato.fadeout(percent(25)), rgba(255, 99, 71, 0.25));
assert_eq!(cornflower_blue.fadeout(percent(75)), rgba(100, 149, 237, 0.25));
sourcefn fade(self, amount: Ratio) -> Self::Alpha
fn fade(self, amount: Ratio) -> Self::Alpha
Sets the absolute opacity of self
, and returns the alpha equivalent.
Can be applied to colors whether they already have an opacity value or not.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, percent};
let tomato = rgba(255, 99, 71, 0.5);
let cornflower_blue = rgb(100, 149, 237);
assert_eq!(tomato.fade(percent(25)), rgba(255, 99, 71, 0.25));
assert_eq!(cornflower_blue.fade(percent(50)), rgba(100, 149, 237, 0.5));
sourcefn spin(self, amount: Angle) -> Self
fn spin(self, amount: Angle) -> Self
Rotate the hue angle of self
in either direction.
Returns the appropriate RGB
representation of the color once it has been spun.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, hsl, deg};
let red = hsl(10, 90, 50);
let pink = rgb(243, 13, 90);
assert_eq!(red.spin(deg(30)), hsl(40, 90, 50));
assert_eq!(pink.spin(deg(-30)), rgb(243, 13, 205));
sourcefn mix<T: Color>(self, other: T, weight: Ratio) -> Self::Alpha
fn mix<T: Color>(self, other: T, weight: Ratio) -> Self::Alpha
Mixes two colors (self
and any other Color
) together in variable proportion.
Takes opacity into account in the calculations.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, hsl, hsla, percent};
let red = hsl(10, 90, 50);
let golden = rgb(243, 166, 13);
let navy = rgba(0, 0, 80, 1.0);
assert_eq!(red.mix(navy, percent(50)).to_string(), "hsla(347, 65%, 29%, 1.00)");
assert_eq!(golden.mix(navy, percent(25)), rgba(61, 42, 63, 1.0));
sourcefn tint(self, weight: Ratio) -> Self
fn tint(self, weight: Ratio) -> Self
Mixes self
with white in variable proportion.
Equivalent to calling mix()
with white
(rgb(255, 255, 255)
).
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, hsl, percent};
let red = hsl(10, 90, 50);
let golden = rgb(243, 166, 13);
assert_eq!(red.tint(percent(10)), hsl(10, 92, 95));
assert_eq!(golden.tint(percent(25)), rgb(252, 233, 194));
sourcefn shade(self, weight: Ratio) -> Self
fn shade(self, weight: Ratio) -> Self
Mixes self
with white in variable proportion.
Equivalent to calling mix()
with black
(rgb(0, 0, 0)
).
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba, hsl, percent};
let red = hsl(10, 90, 50);
let golden = rgb(243, 166, 13);
assert_eq!(red.shade(percent(10)), hsl(10, 92, 5));
assert_eq!(golden.shade(percent(25)), rgb(61, 42, 3));
sourcefn greyscale(self) -> Self
fn greyscale(self) -> Self
Remove all saturation from self
in the HSL color space.
Equivalent to calling desaturate(0)
on a color.
For more, see Less’ Color Operations.
Examples
use farver::{Color, rgb, rgba};
let tomato = rgba(255, 99, 71, 1.0);
let cornflower_blue = rgb(100, 149, 237);
assert_eq!(tomato.greyscale(), rgba(163, 163, 163, 1.0));
assert_eq!(cornflower_blue.greyscale(), rgb(169, 169, 169));