1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 02:37:35 +00:00

LibGfx: Replace Bitmap::invert() with Bitmap::inverted()

The new function return a new bitmap instead of mutating the current
one in place.
This commit is contained in:
Andreas Kling 2023-02-19 22:43:49 +01:00
parent 1f907a834f
commit 4b3e229157
3 changed files with 9 additions and 10 deletions

View file

@ -89,18 +89,15 @@ void Button::paint_event(PaintEvent& event)
// Reusing that threshold here as it seems to work reasonably well. // Reusing that threshold here as it seems to work reasonably well.
should_invert_icon = contrast_ratio < 4.5f && contrast_ratio < palette().button().contrast_ratio(solid_color->inverted()); should_invert_icon = contrast_ratio < 4.5f && contrast_ratio < palette().button().contrast_ratio(solid_color->inverted());
} }
if (should_invert_icon) auto icon = should_invert_icon ? m_icon->inverted().release_value_but_fixme_should_propagate_errors() : NonnullRefPtr { *m_icon };
m_icon->invert();
if (is_enabled()) { if (is_enabled()) {
if (is_hovered()) if (is_hovered())
painter.blit_brightened(icon_location, *m_icon, m_icon->rect()); painter.blit_brightened(icon_location, *icon, icon->rect());
else else
painter.blit(icon_location, *m_icon, m_icon->rect()); painter.blit(icon_location, *icon, icon->rect());
} else { } else {
painter.blit_disabled(icon_location, *m_icon, m_icon->rect(), palette()); painter.blit_disabled(icon_location, *icon, icon->rect(), palette());
} }
if (should_invert_icon)
m_icon->invert();
} }
auto& font = is_checked() ? this->font().bold_variant() : this->font(); auto& font = is_checked() ? this->font().bold_variant() : this->font();
if (m_icon && !text().is_empty()) { if (m_icon && !text().is_empty()) {

View file

@ -476,12 +476,14 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::to_bitmap_backed_by_anonymous_buffer() co
return bitmap; return bitmap;
} }
void Bitmap::invert() ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::inverted() const
{ {
auto inverted_bitmap = TRY(clone());
for (auto y = 0; y < height(); y++) { for (auto y = 0; y < height(); y++) {
for (auto x = 0; x < width(); x++) for (auto x = 0; x < width(); x++)
set_pixel(x, y, get_pixel(x, y).inverted()); inverted_bitmap->set_pixel(x, y, get_pixel(x, y).inverted());
} }
return inverted_bitmap;
} }
Bitmap::~Bitmap() Bitmap::~Bitmap()

View file

@ -126,7 +126,7 @@ public:
[[nodiscard]] ShareableBitmap to_shareable_bitmap() const; [[nodiscard]] ShareableBitmap to_shareable_bitmap() const;
void invert(); ErrorOr<NonnullRefPtr<Gfx::Bitmap>> inverted() const;
~Bitmap(); ~Bitmap();