瀏覽代碼

LibDraw: Add Painter::clear_rect() for filling a rect without blending

Sometimes you want to fill a rect with a specific color without alpha
blending it with whatever's already there.
Andreas Kling 5 年之前
父節點
當前提交
c645d9fe4a
共有 2 個文件被更改,包括 24 次插入8 次删除
  1. 23 8
      Libraries/LibDraw/Painter.cpp
  2. 1 0
      Libraries/LibDraw/Painter.h

+ 23 - 8
Libraries/LibDraw/Painter.cpp

@@ -54,6 +54,24 @@ void Painter::fill_rect_with_draw_op(const Rect& a_rect, Color color)
     }
 }
 
+void Painter::clear_rect(const Rect& a_rect, Color color)
+{
+    auto rect = a_rect.translated(translation()).intersected(clip_rect());
+    if (rect.is_empty())
+        return;
+
+    ASSERT(m_target->rect().contains(rect));
+
+    RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
+    const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
+
+    for (int i = rect.height() - 1; i >= 0; --i) {
+        fast_u32_fill(dst, color.value(), rect.width());
+        dst += dst_skip;
+    }
+}
+
+
 void Painter::fill_rect(const Rect& a_rect, Color color)
 {
     if (color.alpha() == 0)
@@ -64,6 +82,11 @@ void Painter::fill_rect(const Rect& a_rect, Color color)
         return;
     }
 
+    if (color.alpha() == 0xff) {
+        clear_rect(a_rect, color);
+        return;
+    }
+
     auto rect = a_rect.translated(translation()).intersected(clip_rect());
     if (rect.is_empty())
         return;
@@ -73,14 +96,6 @@ void Painter::fill_rect(const Rect& a_rect, Color color)
     RGBA32* dst = m_target->scanline(rect.top()) + rect.left();
     const size_t dst_skip = m_target->pitch() / sizeof(RGBA32);
 
-    if (color.alpha() == 0xff) {
-        for (int i = rect.height() - 1; i >= 0; --i) {
-            fast_u32_fill(dst, color.value(), rect.width());
-            dst += dst_skip;
-        }
-        return;
-    }
-
     for (int i = rect.height() - 1; i >= 0; --i) {
         for (int j = 0; j < rect.width(); ++j)
             dst[j] = Color::from_rgba(dst[j]).blend(color).value();

+ 1 - 0
Libraries/LibDraw/Painter.h

@@ -19,6 +19,7 @@ class Painter {
 public:
     explicit Painter(GraphicsBitmap&);
     ~Painter();
+    void clear_rect(const Rect&, Color);
     void fill_rect(const Rect&, Color);
     void fill_rect_with_gradient(const Rect&, Color gradient_start, Color gradient_end);
     void draw_rect(const Rect&, Color, bool rough = false);