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

A simple color type #40

Closed
raphlinus opened this issue Feb 6, 2019 · 1 comment · Fixed by #48
Closed

A simple color type #40

raphlinus opened this issue Feb 6, 2019 · 1 comment · Fixed by #48

Comments

@raphlinus
Copy link
Contributor

Discussion branched from review of #39.

Use of a raw u32 is problematic to represent color. We should have a simple color type.

One model is ColorF from the direct2d crate, which wraps D2D_COLOR_F but has its own conversions. I think having a From conversion from u32 interpreted as rgba (note the ColorF conversion is from rgb) would be good. I'd also be open to a conversion from (u32, f32), interpreted as (rgb, a); this latter conversion would be the same as ColorF. And APIs that directly accept color (solid_brush) is the main thing, should accept impl Into so any of these variants can be used.

I'll note that the concept of color is extremely deep water. There are questions of colorspace (sRGB vs other standard RGB color spaces vs the idea of "device color"), gamut, depth (including HDR), use of color for print (CMYK and other spaces that include spot color), other appearance measurements such as gloss, etc. I don't want to get into any of that right now. The new color type would be, as much as specified, an 8 bit sRGB value with separate alpha. I can imagine adding more complex color types later as optional extensions.

Bikeshedding regarding naming and other similar questions are welcome.

@m-hilgendorf
Copy link

m-hilgendorf commented Feb 21, 2019

Pure bikeshed incoming.

It's a little weird to bake alpha into the encoding, considering that HTML/CSS doesn't do that and opts for 6 nibble color codes (e.g #abcdef). Personally I think color is orthogonal to opacity, when you aren't dealing with compositing.

If integer literals are convertible to a Color type it would make sense to fall to the more common use case of using the lower 24 bits of a u32 without considering alpha. But that said, since there are multiple methods for conversion to the internal color representation, then it makes sense to have multiple methods. E.g. rather than implement From<u32> for Color, how about an IntoColor and FromColor trait for different types.

    trait IntoColor {
        fn rgb(Self) -> Color;
        fn rgba(Self) -> Color;
        fn hsv(Self) -> Color; 
        fn hsva(Self) -> Color; 
        fn cmyk(Self) -> Color; 
    }

Then the trait could be implemented for both u32 and tuples like (f32, f32, f32)

    let cyan = 0x00ffff.rgb();
    let magenta = (0.833, 1.0, 1.0).hsv();
    let transparent_white = 0xffffff00.rgba(); 
    let transparent_black = (0.0, 0.0, 0.0, 0.0).rgba(); 

And then for the From<Color> implementation you can choose a default. For internal representation you could use anything, which would help with HDR compat.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants