-
Notifications
You must be signed in to change notification settings - Fork 60
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 conversion trait #43
Conversation
This depends on an unstable feature, so it won't work in stable and beta. It's an interesting idea and I would like to entertain it a bit more, but I'm not willing to drop stable and beta support for it. That being said, any improvement that can minimize the amount of |
I am not sure I understand what you are trying say regarding from_, into_ and custom types. I have removed the negative impl trait (unstable feature) and used a macro instead. Do you think this is any better? |
One of the reasons I was looking at this was to avoid rewriting macros, for some of the upcoming changes regarding WhitePoint and RgbVariants. I guess we can't really avoid it. I think there is an advantage in having a conversion trait and I am happy to make these changes if you see a benefit. |
Oh, wait, now I get it! I must have been too distracted... This is actually a very nice approach, and making ColorConvert public would probably be even nicer. I thought it was a replacement for |
I have implemented the color trait and changed the default behaviour of xyz -> rgb to clamp. All color enum / alpha From traits implemented via macro |
eb661d6
to
8567943
Compare
I only skimmed through it, so far, but this looks very nice. One thing, though; wouldn't it be a non-breaking change if we didn't clamp? Also, why only clamp RGB? An other alternative for now, provided that it's not breaking, is to add generic |
Sounds good. I revert the clamping for Rgb. Why only clamp rgb? |
8567943
to
45d7cad
Compare
I think there was an error in the code as well. into_rgb clamped, but from_xyz for Rgb did not clamp. Need to think a bit more about it. |
You can still end up with strange values if you are working with strange values from the start, but that's for a later discussion. I'll look through it in a moment. |
use {Alpha, Rgb, Luma, Xyz, Yxy, Lab, Lch, Hsv, Hsl, Color}; | ||
|
||
///Provides conversion between the colors. | ||
///Color Conver requires from and into xyz and derives others from this. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
///Color Conver
This looks like a mistake. I would also suggest swapping this line with the line below, to keep that as one paragraph, while removing this sentence from the trait list. I don't know if you have checked how things looks after running cargo doc
...
Great, that was all I could find 😄 The only thing, apart from those, that's bothering me a bit is that I can't decide which one of |
Regarding the ColorConvert trait, the code currently seems duplicated. For example into_yxy and from_yxy for Luma is implemented directly and I have to make sure that from_luma and into_luma should also be changed to use these conversions. Same for hsl <-> hsv conversion. I was also thinking about FromColor and IntoColor as separate traits, I think I got tripped up while trying to implement the IntoColor automatically for the FromColor. Separating the traits might be better though. Another reason for From trait not to take &self is because this is a no-op
Doing this might be expensive
But we have no such restriction. The color type is copy. So this should be pretty fast
I think I now prefer &self. No need to take ownership when not required. I don't understand this part "but it's limited because you can't deconstruct more complex types" |
I was thinking about types where
That would be a good idea, I think. Some types may be possible to convert one way, but not the other. It could be destructive, semantically wrong, or whatever else. |
Let me take another crack at separating the from and into color traits. |
Go for it 👍 Just ask if you need an extra pair of eyes. |
Given FromColor and IntoColor
How would you auto impl IntoColor for From
|
It would rather be something like impl<T:Float, C: FromColor<T>> IntoColor<T> for C { but that's not right either, because something is missing. |
I've used a macro to implement IntoColor trait. Not sure how sure how optimal this is. For example self conversion for into does this
instead of
May be the compiler will optimize this away. Is this any better than the ColorConvert trait? |
It's much better because it's divided into two parts and I think the compiler will inline those calls. It would have been possible to implement them in the same way as before, though, but just divided into two I think we can just as well take everything by value, by the way. The implementor can still choose to limit the implementation to |
...It will also leave any copying to the user, instead of always forcing it if it's necessary. |
efd3741
to
0b9f380
Compare
|
||
use {Alpha, Rgb, Luma, Xyz, Yxy, Lab, Lch, Hsv, Hsl, Color}; | ||
|
||
///FromColor provides conversion between the colors. It requires from_xyz and derives conversion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind separating the first sentence into its own paragraph? It will look nicer in the docs.
updated comments
91d7b79
to
50997dc
Compare
Not sure if anything else needs to be done here. I've made the recommended changes. |
No, not unless you want to add anything. Possibly those tests, but they can be added separately, as long as it's done before the release. The [WIP] tag is still there, so I thought you had something else in store, and then I got distracted by other things I had to do... The description is outdated, though. |
Let's close this and I'll add the tests separately. The description is correct. |
Alright :) Just edit the title and description, first. |
I didn't realize that you could edit the title :). Description is ok |
It mentioned a "FromConvert" trait, but I changed that part just now. @homu r+ |
📌 Commit 50997dc has been approved by |
⚡ Test exempted - status |
Add conversion trait This commit adds a FromColor and IntoColor conversion trait. These traits require from_xyz and into_xyz respectively to be implemented and auto derive the conversions for other colors. To implement FromColor and IntoColor for a type, a few things must by kept in mind. - Derived colors like Hsl must override the default from_rgb and use this for from_xyz for efficiency. - The FromColor for the same color must override the default. For example, from_rgb for Rgb will convert via Xyz which needs to be overridden with self to avoid the unnecessary conversion. - If a direct transform exists between the colors, then it should be overridden in both the colors. For example, Luma has direct conversion to Rgb. So from_rgb for Luma and from_luma for Rgb override the defaults.
This commit adds a FromColor and IntoColor conversion trait. These traits require from_xyz and into_xyz respectively to be implemented and auto derive the conversions for other colors.
To implement FromColor and IntoColor for a type, a few things must by kept in mind.