Skip to content

Commit

Permalink
Fix flickering caught in high level runtimes.
Browse files Browse the repository at this point in the history
When converting floating point opacity values to 8 bit alpha, make sure to not under/overflow 0-255.

Fixes rive-app/rive-react-native#159

Diffs=
e65175b88 Fix flickering caught in high level runtimes. (#5243)
  • Loading branch information
luigi-rosso committed May 11, 2023
1 parent 608e498 commit 79e3229
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a8c1e3e758819373ba9246b3731d395f89fa3575
e65175b88afb40b101b54617a29b1bedc6f63d99
9 changes: 7 additions & 2 deletions src/shapes/paint/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,24 @@ void UnpackColorToRGBA32FPremul(ColorInt color, float out[4])

float colorOpacity(ColorInt value) { return (float)colorAlpha(value) / 0xFF; }

static uint8_t opacityToAlpha(float opacity)
{
return (uint8_t)std::lround(255.f * std::max(0.0f, std::min(1.0f, opacity)));
}

ColorInt colorWithAlpha(ColorInt value, unsigned int a)
{
return colorARGB(a, colorRed(value), colorGreen(value), colorBlue(value));
}

ColorInt colorWithOpacity(ColorInt value, float opacity)
{
return colorWithAlpha(value, std::lround(255.f * opacity));
return colorWithAlpha(value, opacityToAlpha(opacity));
}

ColorInt colorModulateOpacity(ColorInt value, float opacity)
{
return colorWithAlpha(value, std::lround(255.f * colorOpacity(value) * opacity));
return colorWithAlpha(value, opacityToAlpha(colorOpacity(value) * opacity));
}

static unsigned int lerp(unsigned int a, unsigned int b, float mix)
Expand Down

0 comments on commit 79e3229

Please sign in to comment.