mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-20 00:08:55 +00:00
LibGfx: Match vImage premultiply/unpremultiply rounding behavior
Our Color::to_premultiplied() and Color::to_unpremultiplied() used integer truncation. Apple’s Accelerate framework (and many other libraries) use round-to-nearest, which avoids bias and produces results that differ by ±1 in many cases. This commit switches both helpers to round-to-nearest and clamps the results to [0,255]. For alpha==0 we now return fully transparent black (0,0,0,0) to align with common conventions, instead of preserving RGB.
This commit is contained in:
parent
7575beafcb
commit
67432e35f1
Notes:
github-actions[bot]
2025-08-23 12:10:23 +00:00
Author: https://github.com/awesomekling
Commit: 67432e35f1
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5964
Reviewed-by: https://github.com/gmta ✅
2 changed files with 13 additions and 11 deletions
|
@ -317,22 +317,24 @@ public:
|
|||
|
||||
constexpr Color to_premultiplied() const
|
||||
{
|
||||
return Color(
|
||||
red() * alpha() / 255,
|
||||
green() * alpha() / 255,
|
||||
blue() * alpha() / 255,
|
||||
alpha());
|
||||
u32 a = alpha();
|
||||
u8 r = static_cast<u8>((red() * a + 127) / 255);
|
||||
u8 g = static_cast<u8>((green() * a + 127) / 255);
|
||||
u8 b = static_cast<u8>((blue() * a + 127) / 255);
|
||||
return Color(r, g, b, a);
|
||||
}
|
||||
|
||||
constexpr Color to_unpremultiplied() const
|
||||
{
|
||||
if (alpha() == 0 || alpha() == 255)
|
||||
u32 a = alpha();
|
||||
if (a == 0)
|
||||
return Color(0, 0, 0, 0);
|
||||
if (a == 255)
|
||||
return *this;
|
||||
return Color(
|
||||
red() * 255 / alpha(),
|
||||
green() * 255 / alpha(),
|
||||
blue() * 255 / alpha(),
|
||||
alpha());
|
||||
u8 r = static_cast<u8>(min(255u, (red() * 255u + a / 2) / a));
|
||||
u8 g = static_cast<u8>(min(255u, (green() * 255u + a / 2) / a));
|
||||
u8 b = static_cast<u8>(min(255u, (blue() * 255u + a / 2) / a));
|
||||
return Color(r, g, b, a);
|
||||
}
|
||||
|
||||
constexpr Color blend(Color source) const
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 9.9 KiB After Width: | Height: | Size: 9.9 KiB |
Loading…
Add table
Add a link
Reference in a new issue