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:
MacDue 2022-11-28 22:27:38 +00:00 committed by Linus Groh
parent 613963cbce
commit f274f04e35

View file

@ -257,11 +257,12 @@ public:
constexpr float distance_squared_to(Color const& other) const
{
int a = other.red() - red();
int b = other.green() - green();
int c = other.blue() - blue();
int d = other.alpha() - alpha();
return (a * a + b * b + c * c + d * d) / (4.0f * 255.0f * 255.0f);
int delta_red = other.red() - red();
int delta_green = other.green() - green();
int delta_blue = other.blue() - blue();
int delta_alpha = other.alpha() - alpha();
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