mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:47:42 +00:00
LibGUI: Add alpha preview to ColorPicker
This commit is contained in:
parent
59a0e5ba1e
commit
5b7decc3af
2 changed files with 46 additions and 17 deletions
|
@ -118,6 +118,19 @@ private:
|
||||||
virtual void resize_event(ResizeEvent&) override;
|
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 {
|
class CustomColorWidget final : public GUI::Widget {
|
||||||
C_OBJECT(CustomColorWidget);
|
C_OBJECT(CustomColorWidget);
|
||||||
|
|
||||||
|
@ -257,20 +270,10 @@ void ColorPicker::build_ui_custom(Widget& root_container)
|
||||||
preview_container.set_preferred_size(0, 128);
|
preview_container.set_preferred_size(0, 128);
|
||||||
|
|
||||||
// Current color
|
// Current color
|
||||||
auto& current_color_widget = preview_container.add<Widget>();
|
preview_container.add<ColorPreview>(m_color);
|
||||||
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 selected color
|
// Preview selected color
|
||||||
m_preview_widget = preview_container.add<Widget>();
|
m_preview_widget = preview_container.add<ColorPreview>(m_color);
|
||||||
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);
|
|
||||||
|
|
||||||
vertical_container.layout()->add_spacer();
|
vertical_container.layout()->add_spacer();
|
||||||
|
|
||||||
|
@ -367,10 +370,7 @@ void ColorPicker::build_ui_custom(Widget& root_container)
|
||||||
|
|
||||||
void ColorPicker::update_color_widgets()
|
void ColorPicker::update_color_widgets()
|
||||||
{
|
{
|
||||||
auto pal = m_preview_widget->palette();
|
m_preview_widget->set_color(m_color);
|
||||||
pal.set_color(ColorRole::Background, m_color);
|
|
||||||
m_preview_widget->set_palette(pal);
|
|
||||||
m_preview_widget->update();
|
|
||||||
|
|
||||||
m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
|
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();
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
class ColorButton;
|
class ColorButton;
|
||||||
|
class ColorPreview;
|
||||||
class CustomColorWidget;
|
class CustomColorWidget;
|
||||||
|
|
||||||
class ColorPicker final : public Dialog {
|
class ColorPicker final : public Dialog {
|
||||||
|
@ -58,7 +59,7 @@ private:
|
||||||
|
|
||||||
Vector<ColorButton*> m_color_widgets;
|
Vector<ColorButton*> m_color_widgets;
|
||||||
RefPtr<CustomColorWidget> m_custom_color;
|
RefPtr<CustomColorWidget> m_custom_color;
|
||||||
RefPtr<Widget> m_preview_widget;
|
RefPtr<ColorPreview> m_preview_widget;
|
||||||
RefPtr<TextBox> m_html_text;
|
RefPtr<TextBox> m_html_text;
|
||||||
RefPtr<SpinBox> m_red_spinbox;
|
RefPtr<SpinBox> m_red_spinbox;
|
||||||
RefPtr<SpinBox> m_green_spinbox;
|
RefPtr<SpinBox> m_green_spinbox;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue