LibGfx: Add Bitmap::solid_color()

This function returns an Optional<Color> and is given an
alpha_threshold. If all pixels above that alpha threshold are the
same color, it returns the color, otherwise it returns an empty
optional.
This commit is contained in:
MacDue 2022-05-09 00:01:21 +01:00 committed by Linus Groh
parent 48d3db3c3d
commit 8e441d402b
Notes: sideshowbarker 2024-07-18 00:54:03 +09:00
2 changed files with 19 additions and 0 deletions

View file

@ -608,4 +608,21 @@ bool Bitmap::visually_equals(Bitmap const& other) const
return true;
}
Optional<Color> Bitmap::solid_color(u8 alpha_threshold) const
{
Optional<Color> color;
for (auto y = 0; y < height(); ++y) {
for (auto x = 0; x < width(); ++x) {
auto const& pixel = get_pixel(x, y);
if (has_alpha_channel() && pixel.alpha() <= alpha_threshold)
continue;
if (!color.has_value())
color = pixel;
else if (pixel != color)
return {};
}
}
return color;
}
}

View file

@ -240,6 +240,8 @@ public:
[[nodiscard]] bool visually_equals(Bitmap const&) const;
[[nodiscard]] Optional<Color> solid_color(u8 alpha_threshold = 0) const;
private:
Bitmap(BitmapFormat, IntSize const&, int, BackingStore const&);
Bitmap(BitmapFormat, IntSize const&, int, size_t pitch, void*);