diff options
author | Tibor Nagy <xnagytibor@gmail.com> | 2020-09-25 17:01:58 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-09-26 00:00:08 +0200 |
commit | 5b7decc3afd3df6f343c8a57342ec6c45fa4c545 (patch) | |
tree | 2134df313174345e224367a365a79a60b0cfb7fb /Libraries/LibGUI | |
parent | 59a0e5ba1ed6e6b9fe7ae59dcb51f201d33da992 (diff) | |
download | serenity-5b7decc3afd3df6f343c8a57342ec6c45fa4c545.zip |
LibGUI: Add alpha preview to ColorPicker
Diffstat (limited to 'Libraries/LibGUI')
-rw-r--r-- | Libraries/LibGUI/ColorPicker.cpp | 60 | ||||
-rw-r--r-- | Libraries/LibGUI/ColorPicker.h | 3 |
2 files changed, 46 insertions, 17 deletions
diff --git a/Libraries/LibGUI/ColorPicker.cpp b/Libraries/LibGUI/ColorPicker.cpp index a74a0dc031..ff471e1131 100644 --- a/Libraries/LibGUI/ColorPicker.cpp +++ b/Libraries/LibGUI/ColorPicker.cpp @@ -118,6 +118,19 @@ private: virtual void resize_event(ResizeEvent&) override; }; +class ColorPreview final : public GUI::Widget { + C_OBJECT(ColorPreview); + +public: + void set_color(Color); + +private: + ColorPreview(Color); + + Color m_color; + virtual void paint_event(GUI::PaintEvent&) override; +}; + class CustomColorWidget final : public GUI::Widget { C_OBJECT(CustomColorWidget); @@ -257,20 +270,10 @@ void ColorPicker::build_ui_custom(Widget& root_container) preview_container.set_preferred_size(0, 128); // Current color - auto& current_color_widget = preview_container.add<Widget>(); - current_color_widget.set_fill_with_background_color(true); - - auto pal1 = current_color_widget.palette(); - pal1.set_color(ColorRole::Background, m_color); - current_color_widget.set_palette(pal1); + preview_container.add<ColorPreview>(m_color); // Preview selected color - m_preview_widget = preview_container.add<Widget>(); - m_preview_widget->set_fill_with_background_color(true); - - auto pal2 = m_preview_widget->palette(); - pal2.set_color(ColorRole::Background, m_color); - m_preview_widget->set_palette(pal2); + m_preview_widget = preview_container.add<ColorPreview>(m_color); vertical_container.layout()->add_spacer(); @@ -367,10 +370,7 @@ void ColorPicker::build_ui_custom(Widget& root_container) void ColorPicker::update_color_widgets() { - auto pal = m_preview_widget->palette(); - pal.set_color(ColorRole::Background, m_color); - m_preview_widget->set_palette(pal); - m_preview_widget->update(); + m_preview_widget->set_color(m_color); m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha()); @@ -702,4 +702,32 @@ void ColorSlider::resize_event(ResizeEvent&) recalculate_position(); } +ColorPreview::ColorPreview(Color color) + : m_color(color) +{ +} + +void ColorPreview::set_color(Color color) +{ + if (m_color == color) + return; + + m_color = color; + update(); +} + +void ColorPreview::paint_event(PaintEvent& event) +{ + Painter painter(*this); + painter.add_clip_rect(event.rect()); + + if (m_color.alpha() < 255) { + Gfx::StylePainter::paint_transparency_grid(painter, rect(), palette()); + painter.fill_rect(rect(), m_color); + painter.fill_rect({ 0, 0, rect().width() / 4, rect().height() }, m_color.with_alpha(255)); + } else { + painter.fill_rect(rect(), m_color); + } +} + } diff --git a/Libraries/LibGUI/ColorPicker.h b/Libraries/LibGUI/ColorPicker.h index 228ddc46f3..0d0e2334c4 100644 --- a/Libraries/LibGUI/ColorPicker.h +++ b/Libraries/LibGUI/ColorPicker.h @@ -32,6 +32,7 @@ namespace GUI { class ColorButton; +class ColorPreview; class CustomColorWidget; class ColorPicker final : public Dialog { @@ -58,7 +59,7 @@ private: Vector<ColorButton*> m_color_widgets; RefPtr<CustomColorWidget> m_custom_color; - RefPtr<Widget> m_preview_widget; + RefPtr<ColorPreview> m_preview_widget; RefPtr<TextBox> m_html_text; RefPtr<SpinBox> m_red_spinbox; RefPtr<SpinBox> m_green_spinbox; |