linebender / piet

An abstraction for 2D graphics.
Apache License 2.0
1.23k stars 95 forks source link

adding const fn with_a8() to Color #525

Closed flxzt closed 11 months ago

flxzt commented 2 years ago

similar to with_alpha(), which unfortunately can't currently be const fn due to floating point arithmetic's, I am suggesting to add const fn with_a8().

My personal use case for it: I have a palette of predefined colors (think Gnome Colors), which I use as the basis for other default colors definitions (which is why I need 'const' ).

Related: the discussion that Color probably won't be anything else than u32 so the const is unlikely to become a problem in the future.

With the same thought, as_rgba_u32() can then also converted to be const.

I could also add analogous methods with_r8(), with_g8() and with_b8() to the PR for completeness sake, if the idea is approved.

xStrom commented 2 years ago

That discussion is about Copy and f64 implements Copy. If floats cause issues for const then this here is a different decision.

The code itself looks good.

flxzt commented 2 years ago

Sorry, poorly worded. What I meant: Right now Color simply holds an int. When piet would switch to a more complex data type (to support HDR for example) it could be challenging to keep the methods I am introducing here const, because a const fn cannot use regular fn's inside it, and the methods for floating point arithmetics are currently all regular fn's .

So I see two (potential) decisions: