Skip to content

Commit

Permalink
Refactor plutovg_div255 macro to static inline function
Browse files Browse the repository at this point in the history
  • Loading branch information
sammycage committed Nov 2, 2024
1 parent 361758c commit 80154ce
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions source/plutovg-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#define plutovg_min(a, b) ((a) < (b) ? (a) : (b))
#define plutovg_max(a, b) ((a) > (b) ? (a) : (b))
#define plutovg_clamp(v, lo, hi) ((v) < (lo) ? (lo) : (hi) < (v) ? (hi) : (v))
#define plutovg_div255(x) (((x) + ((x) >> 8) + 0x80) >> 8)

#define plutovg_alpha(c) (((c) >> 24) & 0xff)
#define plutovg_red(c) (((c) >> 16) & 0xff)
Expand Down Expand Up @@ -53,16 +52,21 @@
#define plutovg_array_clear(array) ((array).size = 0)
#define plutovg_array_destroy(array) free((array).data)

static inline uint32_t plutovg_div255(uint32_t x)
{
return (((x) + ((x) >> 8) + 0x80) >> 8);
}

static inline uint32_t plutovg_premultiply_argb(uint32_t color)
{
uint32_t a = plutovg_alpha(color);
uint32_t r = plutovg_red(color);
uint32_t g = plutovg_green(color);
uint32_t b = plutovg_blue(color);
if(a != 255) {
r = (r * a) / 255;
g = (g * a) / 255;
b = (b * a) / 255;
r = plutovg_div255(r * a);
g = plutovg_div255(g * a);
b = plutovg_div255(b * a);
}

return (a << 24) | (r << 16) | (g << 8) | (b);
Expand Down

0 comments on commit 80154ce

Please sign in to comment.