vaidehijoshi / css-colors

A Rust converter for transforming CSS colors! 🎨
https://crates.io/crates/css-colors
ISC License
27 stars 9 forks source link

Add serde support #14

Open sergeysova opened 5 years ago

sergeysova commented 5 years ago

What about to add serde support?

extern crate serde;
extern crate serde_json;
extern crate serde_derive;
extern crate css_colors;

use css_colors::{RGB, rgb};
use serde_derive::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Example {
    color: RGB,
}

#[test]
fn example() {
    let example = Example {
        color: rgb(100, 200, 1),
    };

    let json = serde_json::to_string(&example).unwrap();

    assert!(json, "{\"color\":\"rgb(100,200,1)\"}");
}
franky47 commented 5 years ago

This would be a nice addition indeed, I could give it a try.

Best would be support of hex codes (in both short #abc and long #abcdef form) for RGB. The 8-hex version for RGBA is not standardised, but could be added as well.