diff options
author | Lucas CHOLLET <lucas.chollet@free.fr> | 2023-03-11 17:56:14 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-12 22:08:24 +0100 |
commit | b5594bf9a2587e76a759e668fb825748d955a405 (patch) | |
tree | f428af3cc7d146a60313cb2e19d9d0686307692b | |
parent | c587ada084e5c78130ca2d99c96f627d1e2e2955 (diff) | |
download | serenity-b5594bf9a2587e76a759e668fb825748d955a405.zip |
PixelPaint: Display color information on mousemove with the PickerTool
Per-channel values of the pixel color are now displayed in the status
bar when the PickerTool is selected. This information obviously updates
on mousemove.
-rw-r--r-- | Userland/Applications/PixelPaint/ImageEditor.cpp | 31 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/ImageEditor.h | 3 | ||||
-rw-r--r-- | Userland/Applications/PixelPaint/Tools/PickerTool.cpp | 3 |
3 files changed, 28 insertions, 9 deletions
diff --git a/Userland/Applications/PixelPaint/ImageEditor.cpp b/Userland/Applications/PixelPaint/ImageEditor.cpp index 598e112418..6fb70d6d00 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.cpp +++ b/Userland/Applications/PixelPaint/ImageEditor.cpp @@ -337,29 +337,44 @@ GUI::MouseEvent ImageEditor::event_adjusted_for_layer(GUI::MouseEvent const& eve }; } -void ImageEditor::set_editor_color_to_color_at_mouse_position(GUI::MouseEvent const& event, bool sample_all_layers = false) +Optional<Color> ImageEditor::color_from_position(Gfx::IntPoint position, bool sample_all_layers) { - auto position = event.position(); Color color; - auto layer = active_layer(); + auto* layer = active_layer(); if (sample_all_layers) { color = image().color_at(position); } else { if (!layer || !layer->rect().contains(position)) - return; + return {}; color = layer->currently_edited_bitmap().get_pixel(position); } + return color; +} + +void ImageEditor::set_status_info_to_color_at_mouse_position(Gfx::IntPoint position, bool sample_all_layers) +{ + auto const color = color_from_position(position, sample_all_layers); + if (!color.has_value()) + return; - set_appended_status_info(DeprecatedString::formatted("R:{}, G:{}, B:{}, A:{} [{}]", color.red(), color.green(), color.blue(), color.alpha(), color.to_deprecated_string())); + set_appended_status_info(DeprecatedString::formatted("R:{}, G:{}, B:{}, A:{} [{}]", color->red(), color->green(), color->blue(), color->alpha(), color->to_deprecated_string())); +} + +void ImageEditor::set_editor_color_to_color_at_mouse_position(GUI::MouseEvent const& event, bool sample_all_layers = false) +{ + auto const color = color_from_position(event.position(), sample_all_layers); + + if (!color.has_value()) + return; // We picked a transparent pixel, do nothing. - if (!color.alpha()) + if (!color->alpha()) return; if (event.buttons() & GUI::MouseButton::Primary) - set_primary_color(color); + set_primary_color(*color); if (event.buttons() & GUI::MouseButton::Secondary) - set_secondary_color(color); + set_secondary_color(*color); } void ImageEditor::mousedown_event(GUI::MouseEvent& event) diff --git a/Userland/Applications/PixelPaint/ImageEditor.h b/Userland/Applications/PixelPaint/ImageEditor.h index 8951b5f4e0..48b4e2b1f1 100644 --- a/Userland/Applications/PixelPaint/ImageEditor.h +++ b/Userland/Applications/PixelPaint/ImageEditor.h @@ -118,6 +118,7 @@ public: Core::EventLoop& gui_event_loop() { return m_gui_event_loop; } + void set_status_info_to_color_at_mouse_position(Gfx::IntPoint position, bool sample_all_layers); void set_editor_color_to_color_at_mouse_position(GUI::MouseEvent const& event, bool sample_all_layers); void set_modified(DeprecatedString action_text); @@ -161,6 +162,8 @@ private: void paint_selection(Gfx::Painter&); + Optional<Color> color_from_position(Gfx::IntPoint position, bool sample_all_layers); + NonnullRefPtr<Image> m_image; RefPtr<Layer> m_active_layer; GUI::UndoStack m_undo_stack; diff --git a/Userland/Applications/PixelPaint/Tools/PickerTool.cpp b/Userland/Applications/PixelPaint/Tools/PickerTool.cpp index ce231098a8..906008aaf6 100644 --- a/Userland/Applications/PixelPaint/Tools/PickerTool.cpp +++ b/Userland/Applications/PixelPaint/Tools/PickerTool.cpp @@ -27,7 +27,6 @@ void PickerTool::on_mouseup(Layer*, MouseEvent& event) auto layer_event = event.layer_event(); if (layer_event.buttons() & GUI::MouseButton::Primary || layer_event.buttons() & GUI::MouseButton::Secondary) return; - m_editor->set_appended_status_info(DeprecatedString::empty()); } void PickerTool::on_mousemove(Layer* layer, MouseEvent& event) @@ -35,6 +34,8 @@ void PickerTool::on_mousemove(Layer* layer, MouseEvent& event) if (!layer) return; auto layer_event = event.layer_event(); + m_editor->set_status_info_to_color_at_mouse_position(layer_event.position(), m_sample_all_layers); + if (!(layer_event.buttons() & GUI::MouseButton::Primary || layer_event.buttons() & GUI::MouseButton::Secondary)) return; m_editor->set_editor_color_to_color_at_mouse_position(layer_event, m_sample_all_layers); |