1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:27:45 +00:00

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.
This commit is contained in:
Lucas CHOLLET 2023-03-11 17:56:14 -05:00 committed by Andreas Kling
parent c587ada084
commit b5594bf9a2
3 changed files with 28 additions and 9 deletions

View file

@ -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; Color color;
auto layer = active_layer(); auto* layer = active_layer();
if (sample_all_layers) { if (sample_all_layers) {
color = image().color_at(position); color = image().color_at(position);
} else { } else {
if (!layer || !layer->rect().contains(position)) if (!layer || !layer->rect().contains(position))
return; return {};
color = layer->currently_edited_bitmap().get_pixel(position); color = layer->currently_edited_bitmap().get_pixel(position);
} }
return color;
}
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_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()));
}
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. // We picked a transparent pixel, do nothing.
if (!color.alpha()) if (!color->alpha())
return; return;
if (event.buttons() & GUI::MouseButton::Primary) if (event.buttons() & GUI::MouseButton::Primary)
set_primary_color(color); set_primary_color(*color);
if (event.buttons() & GUI::MouseButton::Secondary) if (event.buttons() & GUI::MouseButton::Secondary)
set_secondary_color(color); set_secondary_color(*color);
} }
void ImageEditor::mousedown_event(GUI::MouseEvent& event) void ImageEditor::mousedown_event(GUI::MouseEvent& event)

View file

@ -118,6 +118,7 @@ public:
Core::EventLoop& gui_event_loop() { return m_gui_event_loop; } 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_editor_color_to_color_at_mouse_position(GUI::MouseEvent const& event, bool sample_all_layers);
void set_modified(DeprecatedString action_text); void set_modified(DeprecatedString action_text);
@ -161,6 +162,8 @@ private:
void paint_selection(Gfx::Painter&); void paint_selection(Gfx::Painter&);
Optional<Color> color_from_position(Gfx::IntPoint position, bool sample_all_layers);
NonnullRefPtr<Image> m_image; NonnullRefPtr<Image> m_image;
RefPtr<Layer> m_active_layer; RefPtr<Layer> m_active_layer;
GUI::UndoStack m_undo_stack; GUI::UndoStack m_undo_stack;

View file

@ -27,7 +27,6 @@ void PickerTool::on_mouseup(Layer*, MouseEvent& event)
auto layer_event = event.layer_event(); auto layer_event = event.layer_event();
if (layer_event.buttons() & GUI::MouseButton::Primary || layer_event.buttons() & GUI::MouseButton::Secondary) if (layer_event.buttons() & GUI::MouseButton::Primary || layer_event.buttons() & GUI::MouseButton::Secondary)
return; return;
m_editor->set_appended_status_info(DeprecatedString::empty());
} }
void PickerTool::on_mousemove(Layer* layer, MouseEvent& event) void PickerTool::on_mousemove(Layer* layer, MouseEvent& event)
@ -35,6 +34,8 @@ void PickerTool::on_mousemove(Layer* layer, MouseEvent& event)
if (!layer) if (!layer)
return; return;
auto layer_event = event.layer_event(); 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)) if (!(layer_event.buttons() & GUI::MouseButton::Primary || layer_event.buttons() & GUI::MouseButton::Secondary))
return; return;
m_editor->set_editor_color_to_color_at_mouse_position(layer_event, m_sample_all_layers); m_editor->set_editor_color_to_color_at_mouse_position(layer_event, m_sample_all_layers);