mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
LibGfx: Handle alpha in color distance
This gives a slightly more reasonable result when comparing different colors with low alpha values. For example comparing white with alpha 100 to transparent: Before distance: 0.78 After distance: 0.07 (Where distance is between 0 and 1) The result is unchanged for comparing colors without alpha values.
This commit is contained in:
parent
613963cbce
commit
f274f04e35
1 changed files with 6 additions and 5 deletions
|
@ -257,11 +257,12 @@ public:
|
||||||
|
|
||||||
constexpr float distance_squared_to(Color const& other) const
|
constexpr float distance_squared_to(Color const& other) const
|
||||||
{
|
{
|
||||||
int a = other.red() - red();
|
int delta_red = other.red() - red();
|
||||||
int b = other.green() - green();
|
int delta_green = other.green() - green();
|
||||||
int c = other.blue() - blue();
|
int delta_blue = other.blue() - blue();
|
||||||
int d = other.alpha() - alpha();
|
int delta_alpha = other.alpha() - alpha();
|
||||||
return (a * a + b * b + c * c + d * d) / (4.0f * 255.0f * 255.0f);
|
auto rgb_distance = (delta_red * delta_red + delta_green * delta_green + delta_blue * delta_blue) / (3.0f * 255 * 255);
|
||||||
|
return delta_alpha * delta_alpha / (2.0f * 255 * 255) + rgb_distance * alpha() * other.alpha() / (255 * 255);
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr u8 luminosity() const
|
constexpr u8 luminosity() const
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue