yoanlcq / vek

Generic 2D-3D math swiss army knife for game engines, with SIMD support and focus on convenience.
https://docs.rs/vek
Apache License 2.0
282 stars 32 forks source link

Blend Rgba colors with different blend modes #22

Open sunjay opened 5 years ago

sunjay commented 5 years ago

Would it be possible to add functions to blend Rgba structs together using common color blending functions?

For the list of blend functions to support, we can take inspiration from other libraries, e.g. https://github.com/Loilo/color-blend

There is a Rust crate that already implements many of these, but using it would require converting vek types to the types of that crate: https://docs.rs/palette

yoanlcq commented 5 years ago

I'm OK with this as long as it remains "simple" (which is admittedly a subjective notion), what I mean is that it should be made to get along with vek's overall design. Functions such inverted_rgb() and average_rgb in the existing API are a good example I guess.
My point is that one the one hand, these may be simple and convenient functions, but on the other hand, there's a probably a reason there are separate packages for this.

In any case, I would accept a PR that implements the blend modes you need (the "W3C Compositing and Blending spec" looks like a good start).
Each blend mode should be implemented as a method on Rgb and Rgba where T: ColorComponent.
After that, I guess we could add some kind of enum BlendMode and a function which calls the appropriate method based on the selected enum value.

Some caveats: