PaintBrush: Add a color picker tool.

This commit is contained in:
Andreas Kling 2019-06-22 12:05:35 +02:00
parent 41613e4303
commit 25fd847ef2
Notes: sideshowbarker 2024-07-19 13:31:32 +09:00
8 changed files with 71 additions and 2 deletions

View File

@ -9,6 +9,7 @@ OBJS = \
BucketTool.o \
ColorDialog.o \
SprayTool.o \
PickerTool.o \
main.o
APP = PaintBrush

View File

@ -72,3 +72,21 @@ void PaintableWidget::mousemove_event(GMouseEvent& event)
if (m_tool)
m_tool->on_mousemove(event);
}
void PaintableWidget::set_primary_color(Color color)
{
if (m_primary_color == color)
return;
m_primary_color = color;
if (on_primary_color_change)
on_primary_color_change(color);
}
void PaintableWidget::set_secondary_color(Color color)
{
if (m_secondary_color == color)
return;
m_secondary_color = color;
if (on_secondary_color_change)
on_secondary_color_change(color);
}

View File

@ -15,8 +15,8 @@ public:
Color primary_color() const { return m_primary_color; }
Color secondary_color() const { return m_secondary_color; }
void set_primary_color(Color color) { m_primary_color = color; }
void set_secondary_color(Color color) { m_secondary_color = color; }
void set_primary_color(Color);
void set_secondary_color(Color);
void set_tool(Tool* tool);
Tool* tool();
@ -26,6 +26,9 @@ public:
GraphicsBitmap& bitmap() { return *m_bitmap; }
const GraphicsBitmap& bitmap() const { return *m_bitmap; }
Function<void(Color)> on_primary_color_change;
Function<void(Color)> on_secondary_color_change;
private:
virtual void paint_event(GPaintEvent&) override;
virtual void mousedown_event(GMouseEvent&) override;

View File

@ -73,6 +73,14 @@ PaletteWidget::PaletteWidget(PaintableWidget& paintable_widget, GWidget* parent)
m_primary_color_widget->set_fill_with_background_color(true);
set_primary_color(paintable_widget.primary_color());
paintable_widget.on_primary_color_change = [this](Color color) {
set_primary_color(color);
};
paintable_widget.on_secondary_color_change = [this](Color color) {
set_secondary_color(color);
};
auto* color_container = new GWidget(this);
color_container->set_relative_rect(m_secondary_color_widget->relative_rect().right() + 2, 2, 500, 32);
color_container->set_layout(make<GBoxLayout>(Orientation::Vertical));

View File

@ -0,0 +1,22 @@
#include "PickerTool.h"
#include <SharedGraphics/GraphicsBitmap.h>
PickerTool::PickerTool()
{
}
PickerTool::~PickerTool()
{
}
void PickerTool::on_mousedown(GMouseEvent& event)
{
ASSERT(m_widget);
if (!m_widget->bitmap().rect().contains(event.position()))
return;
auto color = m_widget->bitmap().get_pixel(event.position());
if (event.button() == GMouseButton::Left)
m_widget->set_primary_color(color);
else if (event.button() == GMouseButton::Right)
m_widget->set_secondary_color(color);
}

View File

@ -0,0 +1,15 @@
#pragma once
#include "Tool.h"
class PickerTool final : public Tool {
public:
PickerTool();
virtual ~PickerTool() override;
virtual void on_mousedown(GMouseEvent&) override;
private:
virtual const char* class_name() const override { return "PickerTool"; }
};

View File

@ -3,6 +3,7 @@
#include "SprayTool.h"
#include "PaintableWidget.h"
#include "PenTool.h"
#include "PickerTool.h"
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GButton.h>
#include <SharedGraphics/PNGLoader.h>
@ -59,6 +60,7 @@ ToolboxWidget::ToolboxWidget(GWidget* parent)
add_tool("Pen", "pen", make<PenTool>());
add_tool("Bucket Fill", "bucket", make<BucketTool>());
add_tool("Spray", "spray", make<SprayTool>());
add_tool("Color Picker", "picker", make<PickerTool>());
}
ToolboxWidget::~ToolboxWidget()

Binary file not shown.

After

Width:  |  Height:  |  Size: 254 B