kornelski / rust-rgb

struct RGB for sharing pixels between crates
https://lib.rs/rgb
MIT License
98 stars 19 forks source link

feature: option to convert color to html code #52

Closed LuckyTurtleDev closed 2 months ago

LuckyTurtleDev commented 2 years ago

It would be nice if it will be possible to convert the color to html.

Some thing like this:

let color = RGB8 { r: 255, g: 0, b: 0 };
println("color: {}", color.to_html());
color: #ff0000
rursprung commented 1 year ago

this is pretty easy to implement (code w/o dependency on rgb as it's not available on the playground):

fn main() {
    let rgb = [255 as u8, 2 as u8, 1 as u8];
    println!("RGB ({}, {}, {}) = #{:0>2x}{:0>2x}{:0>2x}", rgb[0], rgb[1], rgb[2], rgb[0], rgb[1], rgb[2]);
}

output:

RGB (255, 2, 1) = #ff0201

see on the playground.

ripytide commented 3 months ago

Related to #1. This seems out of scope of this project.

It is easy to implement for the end user:

pub trait ToHtml {
    fn to_html(&self) -> String;
}
impl ToHtml for Rgb<u8> {
    ...
}

I'd recommend closing this.