Liste des Groupes | Revenir à cl c |
On 15/06/2024 21:27, Richard Harnden wrote:It's the general function which converts reba quads or rgb triplets (with a little wrapper) to opaque colour values to pass about to graphics functions. And currently it's not used to access the raster for BBX_Canvas elements.On 15/06/2024 19:57, David Brown wrote:As Ben says, that will not work on its own - "r" would get promoted to signed int before the shift, and we are back to undefined behaviour.>>
If you want BBX_RGBA to be a typedef for an unsigned 32-bit integer, write:
>
typedef uint32_t BBX_RGBA;
>
If you want bbx_rgba() to be a function that is typesafe, correct, and efficient (for any decent compiler), write :
>
static inline BBX_RGBA bbx_rgba(uint32_t r, uint32_t g,
uint32_t b, uint32_t a)
{
return (r << 24) | (g << 16) | (b << 8) | a;
}
>
Shouldn't that be ... ?
>
static inline BBX_RGBA bbx_rgba(uint8_t r, uint8_t g,
uint8_t b, uint8_t a)
>
I think there is plenty of scope for improvement in a variety of ways, depending on what the author is looking for. For example, uint8_t might not exist on all platforms (indeed there are current processors that don't support it, not just dinosaur devices). But any system that supports a general-purpose gui, such as Windows or *nix systems, will have these types and will also have a 32-bit int. So the code author can balance portability with convenient assumptions.
There are also balances to be found between run-time checking and efficiency, and how to handle bad data. If the function can assume that no one calls it with values outside 0..255, or that it doesn't matter what happens if such values are used, then you don't need any checks. As it stands, with uint32_t parameters, out-of-range values will lead to fully defined but wrong results. Switching to "uint8_t" types would give a different fully defined but wrong result. Maybe the function should use saturation, or run-time checks and error messages - that will depend on where it is in the API, what the code author wants, and what users expect.
Les messages affichés proviennent d'usenet.