Skip to content

Commit

Permalink
LibGUI: Add alpha preview to ColorPicker
Browse files Browse the repository at this point in the history
  • Loading branch information
xTibor authored and awesomekling committed Sep 25, 2020
1 parent 59a0e5b commit 5b7decc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 17 deletions.
60 changes: 44 additions & 16 deletions Libraries/LibGUI/ColorPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,19 @@ class ColorSlider final : public GUI::Frame {
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);

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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);
}
}

}
3 changes: 2 additions & 1 deletion Libraries/LibGUI/ColorPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
namespace GUI {

class ColorButton;
class ColorPreview;
class CustomColorWidget;

class ColorPicker final : public Dialog {
Expand All @@ -58,7 +59,7 @@ class ColorPicker final : public Dialog {

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;
Expand Down

0 comments on commit 5b7decc

Please sign in to comment.