Skip to content

Commit

Permalink
LibGUI: Transfer "color has alpha channel" state
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Apr 29, 2020
1 parent 51df4bd commit f806941
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 17 deletions.
16 changes: 2 additions & 14 deletions Libraries/LibGUI/ColorInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibGUI/ColorInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ColorInput final : public TextEditor {
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; }
Expand Down
4 changes: 2 additions & 2 deletions Libraries/LibGUI/ColorPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions Libraries/LibGUI/ColorPicker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -52,6 +54,7 @@ class ColorPicker final : public Dialog {
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;
Expand Down

0 comments on commit f806941

Please sign in to comment.