Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hex constructors for Color #375

Closed
BillyDM opened this issue May 31, 2020 · 5 comments · Fixed by #1227
Closed

Add hex constructors for Color #375

BillyDM opened this issue May 31, 2020 · 5 comments · Fixed by #1227
Labels
developer experience feature New feature or request good first issue Good for newcomers question Further information is requested

Comments

@BillyDM
Copy link

BillyDM commented May 31, 2020

It would be nice to add a feature to create colors from a single hexadecimal number like this:

let color1 = Color::from_hex_rgb(0x3280c8);
let color2 = Color::from_hex_rgba(0x3280c86f);

The code could be something like this:

pub fn from_hex_rgb(hex: u32) -> Color {
    let r = (hex & 0xff0000) >> 16;
    let g = (hex & 0xff00) >> 8;
    let b = (hex & 0xff);

    Color {
        r: f32::from(r) / 255.0,
        g: f32::from(g) / 255.0,
        b: f32::from(b) / 255.0,
        a: 1.0f32,
    }
}

pub fn from_hex_rgba(hex: u32) -> Color {
    let r = (hex & 0xff000000) >> 24;
    let g = (hex & 0xff0000) >> 16;
    let b = (hex & 0xff00) >> 8;
    let a = (hex & 0xff);

    Color {
        r: f32::from(r) / 255.0,
        g: f32::from(g) / 255.0,
        b: f32::from(b) / 255.0,
        a: f32::from(a) / 255.0,
    }
}
@hecrj
Copy link
Member

hecrj commented May 31, 2020

You can currently write:

let color1 = Color::from_rgb8(0x32, 0x80, 0xC8);
let color2 = Color::from_rgba8(0x32, 0x80, 0xC8, 0.44);

A constructor like from_hex_rgb doesn't exist because it feels wrong to ask for a u32 and discard part of it.

However, I think we could add from_hex_rgba. But maybe we should name it from_rgba8_u32 for consistency (i.e. the hexadecimal representation of the value isn't relevant).

@hecrj hecrj added feature New feature or request question Further information is requested labels May 31, 2020
@BillyDM
Copy link
Author

BillyDM commented May 31, 2020

Well, I'm usually copying and pasting the hex values straight from an external program like Inkscape. It is cumbersome to have to constantly reformat it every time I want to check how it looks in the app.

@BillyDM
Copy link
Author

BillyDM commented May 31, 2020

Actually, yeah, from_rgba8_u32 could work. Inkscape uses that format anyway, even if the alpha is ff. I feel like that name's not that clear that it's an option for using hex values, but perhaps that could be explained in a comment / documentation.

@hecrj
Copy link
Member

hecrj commented May 31, 2020

Well, I'm usually copying and pasting the hex values straight from an external program like Inkscape. It is cumbersome to have to constantly reformat it every time I want to check how it looks in the app.

You could create your own helper function:

fn inkscape_color(value: u32) -> iced::Color { /* ... */ }

I feel like that name's not that clear that it's an option for using hex values, but perhaps that could be explained in a comment / documentation.

Yes. It feels wrong to mention hex when the argument can be any u32.

@BillyDM
Copy link
Author

BillyDM commented May 31, 2020

Oh yeah, I could write my own helper function. It would be nice just to have this in iced at some point though. Other people using iced may want the same thing but not know how to write that specific helper function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
developer experience feature New feature or request good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants