#convert #cmyk #rgb #hsl #hex #hsv #rgba

color-convert

Support RGB,RGBA,HEX,HSL,HSLA,HSV,CMYK to convert each other, write by rust

1 unstable release

Uses old Rust 2015

0.1.0 Nov 21, 2018

#1597 in Text processing

Download history 4/week @ 2024-07-22 8/week @ 2024-07-29 2/week @ 2024-08-05 7/week @ 2024-08-12 4/week @ 2024-08-26 20/week @ 2024-09-02 21/week @ 2024-09-09 12/week @ 2024-09-16 24/week @ 2024-09-23 19/week @ 2024-09-30 23/week @ 2024-10-14 10/week @ 2024-10-21 8/week @ 2024-10-28 19/week @ 2024-11-04

60 downloads per month

Custom license

39KB
1K SLoC

color-convert

Convert color to each other by rust.

license

Installation

Add this to the Cargo.toml file of your project:

[dependencies]
color-convert = "0.1.0"

Don't forget to add the external crate:

extern crate color_convert;

Getting Started

extern crate color_convert;

use color_convert::color::{Color, Error};

fn main() -> Result<(), Error> {
    let mut color = Color::init("#c8c8c8ff", false, false, false);
    // or just 
    let mut color = Color::new("#c8c8c8ff");

    assert_eq!("rgb(200,200,200)", color.to_rgb()?);
}

Settings

Color has three simple configurations:

  1. whether it is uppercase: upper
  2. whether it contains transparency: alpha
  3. whether it is Android format (only hex will be used): andriod
// ...
fn main() -> Result<(), Error> {
    let mut color = Color::new("#c8c8c8ff");

    // you can change some config
    assert_eq!("RGB(200,200,200)", color.to_upper(true).to_rgb()?);
    assert_eq!("RGBA(200,200,200,1)", color.to_alpha(true).to_rgb()?);
    assert_eq!("RGBA(200,200,255,0.78)", color.to_android(true).to_rgb()?);
}

Features

Support RGB,RGBA,HEX,HSL,HSLA,HSV,CMYK to transform each other.

Their corresponding method is to_rgb, to_hex, to_hsl, to_hsv, to_cmyk. For formats that need to be converted to contain transparency values, you need to first set the configuration alpha to true(use method to_alpha).

Notes

  1. Some values are lost when some precision values are converted to CMYK and HSV, this is the result of the algorithm, so be careful in the process of using it.
  2. Do not support css3 color:transparent and currentColor.

Next

Help

If you have any questions or suggestions, please contact me via issues or email.

For those who use sublime text, try a similar plugin I wrote for ColorConvert.

Dependencies

~2.2–3MB
~55K SLoC