A Rust converter to transform CSS colors. 🎨
Add the farver
crate to your Cargo.toml
's list of dependencies:
rust
[dependencies]
farver = "3.1.0"
This crate allows you to create and manipulate colors using Less
functions, and to be able to use a common color type if you need
to convert to interact with multiple crates
The RGB color model is often useful when you'd like to represent a color using a certain amount of red, green, and blue.
css
background-color: rgb(255, 99, 71); // tomato
However, it is also possible to represent the same color using the HSL color model, which specifies the hue, saturation, and luminosity of a color:
css
background-color: hsl(9, 100%, 64%); // also tomato!
You can also use CSS preprocessors like Less to manipulate these colors in interesting ways.
css
$tomato: hsl(9, 100%, 64%); // equivalent to rgb(255, 99, 71)
$dark-tomato: darken($tomato, 20%); // hsl(9, 100%, 44%)
$desaturated-tomato: desaturate($tomato, 40%); // hsl(9, 60%, 64%)
This crate allows you to perform operations that map to Less' color operations API. These operations can be applied on both RGB & HSL color models.
Represent colors as a valid CSS string: ```rust use farver::{Color, rgb, hsla};
let salmon = rgb(250, 128, 114); let chartreuse = hsla(90, 100, 50, 1.0);
asserteq!(salmon.tocss(), "rgb(250, 128, 114)"); asserteq!(chartreuse.tocss(), "hsla(90, 100%, 50%, 1.00)"); ```
Convert between different color model representations: ```rust use farver::{Color, rgb, rgba, hsl, hsla};
let chartreuse = rgb(127, 255, 0);
asserteq!(chartreuse.tohsl(), hsl(90, 100, 50)); asserteq!(chartreuse.tohsla(), hsla(90, 100, 50, 1.0)); asserteq!(chartreuse.torgba(), rgba(127, 255, 0, 1.0)); ```
Manipulate single colors to create new color model representations: ```rust use farver::{Color, hsl, percent};
let chartreuse = hsl(90, 100, 50);
asserteq!(chartreuse.darken(percent(20)), hsl(90, 100, 30)); asserteq!(chartreuse.desaturate(percent(20)), hsl(90, 80, 50)); assert_eq!(chartreuse.greyscale(), hsl(90, 0, 50)); ```
Manipulate multiple colors to create new color model representations: ```rust use farver::{Color, rgb, rgba, hsl, hsla, percent};
let chartreuse = hsl(90, 100, 50); let red = rgba(100, 0, 0, 1.0);
asserteq!( chartreuse.mix(red, percent(50)).tocss(), "hsla(67, 98%, 25%, 1.00)" ); asserteq!(chartreuse.tint(percent(50)).tocss(), "hsl(90, 100%, 75%)"); asserteq!(chartreuse.shade(percent(50)).tocss(), "hsl(90, 98%, 25%)"); ```
Check out the documentation to learn more about what color operations are available to use!
The following links may be helpful while using this crate.
git clone https://github.com/nyxkrage/farver
cd farver
cargo build
Please use the below tools to ensure code consistency when contributing to this crate. * Rustfmt for formatting code style
cargo build
– Builds the cratecargo test
– Runs the test suiteThis project is licensed under the ISC License.