diff options
author | Andreas Kling <kling@serenityos.org> | 2020-03-30 19:39:37 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-30 19:39:37 +0200 |
commit | 7976ef7a790d7b99b688384c41f1aa7bbbace014 (patch) | |
tree | 0a291f5fedd57fcd775fbddda00a5221186cce3a /Libraries | |
parent | fb2be5c404889ced14cbce5dc327d5dc277f3b4c (diff) | |
download | serenity-7976ef7a790d7b99b688384c41f1aa7bbbace014.zip |
LibGfx: Add Painter::blit_filtered() and blit_brightened()
blit_filtered() can be used to easily implement per-pixel filtered blit
functions. All you need to do is provide a callback that can compute
the Color for each pixel based on the original Color.
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGfx/Painter.cpp | 21 | ||||
-rw-r--r-- | Libraries/LibGfx/Painter.h | 2 |
2 files changed, 20 insertions, 3 deletions
diff --git a/Libraries/LibGfx/Painter.cpp b/Libraries/LibGfx/Painter.cpp index fc656eaae3..8d85517d87 100644 --- a/Libraries/LibGfx/Painter.cpp +++ b/Libraries/LibGfx/Painter.cpp @@ -29,6 +29,7 @@ #include "Emoji.h" #include "Font.h" #include <AK/Assertions.h> +#include <AK/Function.h> #include <AK/Memory.h> #include <AK/StdLibExtras.h> #include <AK/StringBuilder.h> @@ -377,7 +378,7 @@ void Painter::blit_with_opacity(const Point& position, const Gfx::Bitmap& source } } -void Painter::blit_dimmed(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect) +void Painter::blit_filtered(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect, Function<Color(Color)> filter) { Rect safe_src_rect = src_rect.intersected(source.rect()); auto dst_rect = Rect(position, safe_src_rect.size()).translated(translation()); @@ -397,17 +398,31 @@ void Painter::blit_dimmed(const Point& position, const Gfx::Bitmap& source, cons for (int x = 0; x <= (last_column - first_column); ++x) { u8 alpha = Color::from_rgba(src[x]).alpha(); if (alpha == 0xff) - dst[x] = Color::from_rgba(src[x]).to_grayscale().lightened().value(); + dst[x] = filter(Color::from_rgba(src[x])).value(); else if (!alpha) continue; else - dst[x] = Color::from_rgba(dst[x]).blend(Color::from_rgba(src[x]).to_grayscale().lightened()).value(); + dst[x] = Color::from_rgba(dst[x]).blend(filter(Color::from_rgba(src[x]))).value(); } dst += dst_skip; src += src_skip; } } +void Painter::blit_brightened(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect) +{ + return blit_filtered(position, source, src_rect, [](Color src) { + return src.lightened(); + }); +} + +void Painter::blit_dimmed(const Point& position, const Gfx::Bitmap& source, const Rect& src_rect) +{ + return blit_filtered(position, source, src_rect, [](Color src) { + return src.to_grayscale().lightened(); + }); +} + void Painter::draw_tiled_bitmap(const Rect& a_dst_rect, const Gfx::Bitmap& source) { auto dst_rect = a_dst_rect.translated(translation()); diff --git a/Libraries/LibGfx/Painter.h b/Libraries/LibGfx/Painter.h index ea12cdffde..0f141583c5 100644 --- a/Libraries/LibGfx/Painter.h +++ b/Libraries/LibGfx/Painter.h @@ -56,6 +56,8 @@ public: void draw_scaled_bitmap(const Rect& dst_rect, const Gfx::Bitmap&, const Rect& src_rect); void blit(const Point&, const Gfx::Bitmap&, const Rect& src_rect, float opacity = 1.0f); void blit_dimmed(const Point&, const Gfx::Bitmap&, const Rect& src_rect); + void blit_brightened(const Point&, const Gfx::Bitmap&, const Rect& src_rect); + void blit_filtered(const Point&, const Gfx::Bitmap&, const Rect& src_rect, Function<Color(Color)>); void draw_tiled_bitmap(const Rect& dst_rect, const Gfx::Bitmap&); void blit_offset(const Point&, const Gfx::Bitmap&, const Rect& src_rect, const Point&); void blit_scaled(const Rect&, const Gfx::Bitmap&, const Rect&, float, float); |