BucketTool.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "BucketTool.h"
  7. #include "../ImageEditor.h"
  8. #include "../Layer.h"
  9. #include <AK/HashTable.h>
  10. #include <AK/Queue.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGUI/ValueSlider.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/Rect.h>
  17. namespace PixelPaint {
  18. BucketTool::BucketTool()
  19. {
  20. m_cursor = Gfx::Bitmap::try_load_from_file("/res/icons/pixelpaint/bucket.png").release_value_but_fixme_should_propagate_errors();
  21. }
  22. BucketTool::~BucketTool()
  23. {
  24. }
  25. static float color_distance_squared(Gfx::Color const& lhs, Gfx::Color const& rhs)
  26. {
  27. int a = rhs.red() - lhs.red();
  28. int b = rhs.green() - lhs.green();
  29. int c = rhs.blue() - lhs.blue();
  30. return (a * a + b * b + c * c) / (3.0f * 255.0f * 255.0f);
  31. }
  32. static void flood_fill(Gfx::Bitmap& bitmap, Gfx::IntPoint const& start_position, Color target_color, Color fill_color, int threshold)
  33. {
  34. VERIFY(bitmap.bpp() == 32);
  35. if (target_color == fill_color)
  36. return;
  37. if (!bitmap.rect().contains(start_position))
  38. return;
  39. float threshold_normalized_squared = (threshold / 100.0f) * (threshold / 100.0f);
  40. Queue<Gfx::IntPoint> queue;
  41. queue.enqueue(start_position);
  42. HashTable<Gfx::IntPoint> visited;
  43. while (!queue.is_empty()) {
  44. auto position = queue.dequeue();
  45. if (visited.contains(position))
  46. continue;
  47. visited.set(position);
  48. auto pixel_color = bitmap.get_pixel<Gfx::StorageFormat::BGRA8888>(position.x(), position.y());
  49. if (color_distance_squared(pixel_color, target_color) > threshold_normalized_squared)
  50. continue;
  51. bitmap.set_pixel<Gfx::StorageFormat::BGRA8888>(position.x(), position.y(), fill_color);
  52. if (position.x() != 0)
  53. queue.enqueue(position.translated(-1, 0));
  54. if (position.x() != bitmap.width() - 1)
  55. queue.enqueue(position.translated(1, 0));
  56. if (position.y() != 0)
  57. queue.enqueue(position.translated(0, -1));
  58. if (position.y() != bitmap.height() - 1)
  59. queue.enqueue(position.translated(0, 1));
  60. }
  61. }
  62. void BucketTool::on_mousedown(Layer* layer, MouseEvent& event)
  63. {
  64. if (!layer)
  65. return;
  66. auto& layer_event = event.layer_event();
  67. if (!layer->rect().contains(layer_event.position()))
  68. return;
  69. GUI::Painter painter(layer->bitmap());
  70. auto target_color = layer->bitmap().get_pixel(layer_event.x(), layer_event.y());
  71. flood_fill(layer->bitmap(), layer_event.position(), target_color, m_editor->color_for(layer_event), m_threshold);
  72. layer->did_modify_bitmap();
  73. m_editor->did_complete_action();
  74. }
  75. GUI::Widget* BucketTool::get_properties_widget()
  76. {
  77. if (!m_properties_widget) {
  78. m_properties_widget = GUI::Widget::construct();
  79. m_properties_widget->set_layout<GUI::VerticalBoxLayout>();
  80. auto& threshold_container = m_properties_widget->add<GUI::Widget>();
  81. threshold_container.set_fixed_height(20);
  82. threshold_container.set_layout<GUI::HorizontalBoxLayout>();
  83. auto& threshold_label = threshold_container.add<GUI::Label>("Threshold:");
  84. threshold_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  85. threshold_label.set_fixed_size(80, 20);
  86. auto& threshold_slider = threshold_container.add<GUI::ValueSlider>(Orientation::Horizontal, "%");
  87. threshold_slider.set_range(0, 100);
  88. threshold_slider.set_value(m_threshold);
  89. threshold_slider.on_change = [&](int value) {
  90. m_threshold = value;
  91. };
  92. set_primary_slider(&threshold_slider);
  93. }
  94. return m_properties_widget.ptr();
  95. }
  96. }