LibGUI: Transfer "color has alpha channel" state

This commit is contained in:
Andreas Kling 2020-04-29 16:22:09 +02:00
parent 51df4bdbfc
commit f8069418e1
Notes: sideshowbarker 2024-07-19 07:11:22 +09:00
4 changed files with 8 additions and 17 deletions

View file

@ -67,27 +67,15 @@ void ColorInput::set_color(Color color)
{
if (m_color == color)
return;
set_text(color.to_string());
set_text(m_color_has_alpha_channel ? color.to_string() : color.to_string_without_alpha());
};
void ColorInput::set_color_has_alpha_channel(bool has_alpha)
{
if (m_color_has_alpha_channel == has_alpha)
return;
m_color_has_alpha_channel = has_alpha;
m_color.set_alpha(0xff);
if (!has_alpha)
set_text(m_color.to_string_without_alpha());
else
set_text(m_color.to_string());
}
void ColorInput::mousedown_event(MouseEvent& event)
{
if (event.button() == MouseButton::Left) {
if (is_enabled() && color_rect().contains(event.position())) {
auto dialog = GUI::ColorPicker::construct(m_color, window(), m_color_picker_title);
dialog->set_color_has_alpha_channel(m_color_has_alpha_channel);
if (dialog->exec() == GUI::Dialog::ExecOK)
set_color(dialog->color());
event.accept();

View file

@ -38,7 +38,7 @@ public:
virtual ~ColorInput() override;
bool has_alpha_channel() const { return m_color_has_alpha_channel; }
void set_color_has_alpha_channel(bool);
void set_color_has_alpha_channel(bool has_alpha) { m_color_has_alpha_channel = has_alpha; }
void set_color(Color);
Color color() { return m_color; }

View file

@ -223,7 +223,7 @@ void ColorPicker::build_ui_custom(Widget& root_container)
m_html_text = html_container.add<GUI::TextBox>();
m_html_text->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
m_html_text->set_text(m_color.to_string());
m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
m_html_text->on_change = [this]() {
auto color_name = this->m_html_text->text();
auto optional_color = Color::from_string(color_name);
@ -299,7 +299,7 @@ void ColorPicker::update_color_widgets()
m_preview_widget->set_palette(pal);
m_preview_widget->update();
m_html_text->set_text(m_color.to_string());
m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
m_red_spinbox->set_value(m_color.red());
m_green_spinbox->set_value(m_color.green());

View file

@ -40,6 +40,8 @@ class ColorPicker final : public Dialog {
public:
virtual ~ColorPicker() override;
bool color_has_alpha_channel() const { return m_color_has_alpha_channel; }
void set_color_has_alpha_channel(bool has_alpha) { m_color_has_alpha_channel = has_alpha; }
Color color() const { return m_color; }
private:
@ -52,6 +54,7 @@ private:
void create_color_button(Widget& container, unsigned rgb);
Color m_color;
bool m_color_has_alpha_channel { true };
Vector<ColorButton*> m_color_widgets;
RefPtr<CustomColorWidget> m_custom_color;