123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461 |
- /*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * 1. Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- #include <LibGUI/BoxLayout.h>
- #include <LibGUI/Button.h>
- #include <LibGUI/ColorPicker.h>
- #include <LibGUI/Frame.h>
- #include <LibGUI/Label.h>
- #include <LibGUI/Painter.h>
- #include <LibGUI/SpinBox.h>
- #include <LibGUI/TabWidget.h>
- #include <LibGUI/TextBox.h>
- #include <LibGfx/Palette.h>
- namespace GUI {
- class ColorButton : public AbstractButton {
- C_OBJECT(ColorButton)
- public:
- virtual ~ColorButton() override;
- void set_selected(bool selected);
- Color color() const { return m_color; }
- Function<void(const Color)> on_click;
- protected:
- virtual void click() override;
- virtual void paint_event(PaintEvent&) override;
- private:
- explicit ColorButton(Color color = {});
- Color m_color;
- bool m_selected { false };
- };
- class CustomColorWidget final : public GUI::Widget {
- C_OBJECT(CustomColorWidget);
- public:
- Function<void(Color)> on_pick;
- void clear_last_position();
- private:
- CustomColorWidget();
- RefPtr<Gfx::Bitmap> m_custom_colors;
- bool m_status { false };
- Gfx::Point m_last_position;
- void fire_event(GUI::MouseEvent& event);
- virtual void mousedown_event(GUI::MouseEvent&) override;
- virtual void mouseup_event(GUI::MouseEvent&) override;
- virtual void mousemove_event(GUI::MouseEvent&) override;
- virtual void paint_event(GUI::PaintEvent&) override;
- virtual void resize_event(ResizeEvent&) override;
- };
- ColorPicker::ColorPicker(Color color, Window* parent_window, String title)
- : Dialog(parent_window)
- , m_color(color)
- {
- set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/color-chooser.png"));
- set_title(title);
- set_resizable(false);
- resize(530, 325);
- build_ui();
- }
- ColorPicker::~ColorPicker()
- {
- }
- void ColorPicker::build_ui()
- {
- auto& root_container = set_main_widget<Widget>();
- root_container.set_layout<VerticalBoxLayout>();
- root_container.layout()->set_margins({ 4, 4, 4, 4 });
- root_container.set_fill_with_background_color(true);
- auto& tab_widget = root_container.add<GUI::TabWidget>();
- auto& tab_palette = tab_widget.add_tab<Widget>("Palette");
- tab_palette.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
- tab_palette.set_layout<VerticalBoxLayout>();
- tab_palette.layout()->set_margins({ 4, 4, 4, 4 });
- tab_palette.layout()->set_spacing(4);
- build_ui_palette(tab_palette);
- auto& tab_custom_color = tab_widget.add_tab<Widget>("Custom Color");
- tab_custom_color.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
- tab_custom_color.set_layout<VerticalBoxLayout>();
- tab_custom_color.layout()->set_margins({ 4, 4, 4, 4 });
- tab_custom_color.layout()->set_spacing(4);
- build_ui_custom(tab_custom_color);
- auto& button_container = root_container.add<Widget>();
- button_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
- button_container.set_preferred_size(0, 22);
- button_container.set_layout<HorizontalBoxLayout>();
- button_container.layout()->set_spacing(4);
- button_container.layout()->add_spacer();
- auto& cancel_button = button_container.add<Button>();
- cancel_button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
- cancel_button.set_preferred_size(80, 0);
- cancel_button.set_text("Cancel");
- cancel_button.on_click = [this] {
- done(ExecCancel);
- };
- auto& ok_button = button_container.add<Button>();
- ok_button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
- ok_button.set_preferred_size(80, 0);
- ok_button.set_text("Select");
- ok_button.on_click = [this] {
- done(ExecOK);
- };
- }
- void ColorPicker::build_ui_palette(Widget& root_container)
- {
- unsigned colors[4][9] = {
- { 0xef2929, 0xf0b143, 0xfce94f, 0x9fe13a, 0x7c9ece, 0xa680a8, 0xe1ba70, 0x888a85, 0xeeeeec },
- { 0xba1e09, 0xf57900, 0xe9d51a, 0x8bd121, 0x4164a3, 0x6f517b, 0xb77f19, 0x555753, 0xd4d7cf },
- { 0x961605, 0xbf600c, 0xe9d51a, 0x619910, 0x2b4986, 0x573666, 0x875b09, 0x2f3436, 0xbbbdb6 },
- { 0x000000, 0x2f3436, 0x555753, 0x808080, 0xbabdb6, 0xd3d7cf, 0xeeeeec, 0xf3f3f3, 0xffffff }
- };
- for (int r = 0; r < 4; r++) {
- auto& colors_row = root_container.add<Widget>();
- colors_row.set_layout<HorizontalBoxLayout>();
- colors_row.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
- for (int i = 0; i < 8; i++) {
- create_color_button(colors_row, colors[r][i]);
- }
- }
- }
- void ColorPicker::build_ui_custom(Widget& root_container)
- {
- enum RGBComponent {
- Red,
- Green,
- Blue
- };
- auto& horizontal_container = root_container.add<Widget>();
- horizontal_container.set_fill_with_background_color(true);
- horizontal_container.set_layout<HorizontalBoxLayout>();
- // Left Side
- m_custom_color = horizontal_container.add<GUI::CustomColorWidget>();
- m_custom_color->set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
- m_custom_color->on_pick = [this](Color color) {
- if (m_color == color)
- return;
- m_color = color;
- update_color_widgets();
- };
- // Right Side
- auto& vertical_container = horizontal_container.add<Widget>();
- vertical_container.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
- vertical_container.set_layout<VerticalBoxLayout>();
- vertical_container.layout()->set_margins({ 4, 0, 0, 0 });
- vertical_container.set_preferred_size(150, 0);
- // Preview
- m_preview_widget = vertical_container.add<Frame>();
- m_preview_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
- m_preview_widget->set_preferred_size(0, 150);
- m_preview_widget->set_fill_with_background_color(true);
- auto pal = m_preview_widget->palette();
- pal.set_color(ColorRole::Background, m_color);
- m_preview_widget->set_palette(pal);
- vertical_container.layout()->add_spacer();
- // HTML
- auto& html_container = vertical_container.add<GUI::Widget>();
- html_container.set_layout<GUI::HorizontalBoxLayout>();
- html_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
- html_container.set_preferred_size(0, 22);
- auto& html_label = html_container.add<GUI::Label>();
- html_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
- html_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
- html_label.set_preferred_size({ 70, 0 });
- html_label.set_text("HTML:");
- 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->on_change = [this]() {
- auto color_name = this->m_html_text->text();
- auto optional_color = Color::from_string(color_name);
- if (optional_color.has_value()) {
- auto color = optional_color.value();
- if (m_color == color)
- return;
- m_color = optional_color.value();
- this->m_custom_color->clear_last_position();
- update_color_widgets();
- }
- };
- // RGB Lines
- auto make_spinbox = [&](RGBComponent component, int initial_value) {
- auto& rgb_container = vertical_container.add<GUI::Widget>();
- rgb_container.set_layout<GUI::HorizontalBoxLayout>();
- rgb_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
- rgb_container.set_preferred_size(0, 22);
- auto& rgb_label = rgb_container.add<GUI::Label>();
- rgb_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
- rgb_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
- rgb_label.set_preferred_size({ 70, 0 });
- auto& spinbox = rgb_container.add<SpinBox>();
- spinbox.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
- spinbox.set_preferred_size(0, 20);
- spinbox.set_min(0);
- spinbox.set_max(255);
- spinbox.set_value(initial_value);
- spinbox.on_change = [this, component](auto value) {
- auto color = m_color;
- if (component == Red)
- color.set_red(value);
- if (component == Green)
- color.set_green(value);
- if (component == Blue)
- color.set_blue(value);
- if (m_color == color)
- return;
- m_color = color;
- this->m_custom_color->clear_last_position();
- update_color_widgets();
- };
- if (component == Red) {
- rgb_label.set_text("Red:");
- m_red_spinbox = spinbox;
- } else if (component == Green) {
- rgb_label.set_text("Green:");
- m_green_spinbox = spinbox;
- } else if (component == Blue) {
- rgb_label.set_text("Blue:");
- m_blue_spinbox = spinbox;
- }
- };
- make_spinbox(Red, m_color.red());
- make_spinbox(Green, m_color.green());
- make_spinbox(Blue, m_color.blue());
- }
- 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_html_text->set_text(m_color.to_string());
- m_red_spinbox->set_value(m_color.red());
- m_green_spinbox->set_value(m_color.green());
- m_blue_spinbox->set_value(m_color.blue());
- }
- void ColorPicker::create_color_button(Widget& container, unsigned rgb)
- {
- Color color = Color::from_rgb(rgb);
- auto& widget = container.add<ColorButton>(color);
- widget.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
- widget.on_click = [this](Color color) {
- for (auto& value : m_color_widgets) {
- value->set_selected(false);
- value->update();
- }
- this->m_color = color;
- };
- if (color == m_color) {
- widget.set_selected(true);
- }
- m_color_widgets.append(&widget);
- }
- ColorButton::ColorButton(Color color)
- {
- m_color = color;
- m_selected = false;
- }
- ColorButton::~ColorButton()
- {
- }
- void ColorButton::set_selected(bool selected)
- {
- m_selected = selected;
- }
- void ColorButton::paint_event(PaintEvent& event)
- {
- Painter painter(*this);
- painter.add_clip_rect(event.rect());
- Gfx::StylePainter::paint_button(painter, rect(), palette(), Gfx::ButtonStyle::Normal, is_being_pressed(), is_hovered(), is_checked(), is_enabled());
- painter.fill_rect({ 1, 1, rect().width() - 2, rect().height() - 2 }, m_color);
- if (m_selected) {
- painter.fill_rect({ 3, 3, rect().width() - 6, rect().height() - 6 }, Color::Black);
- painter.fill_rect({ 5, 5, rect().width() - 10, rect().height() - 10 }, Color::White);
- painter.fill_rect({ 7, 6, rect().width() - 14, rect().height() - 14 }, m_color);
- }
- }
- void ColorButton::click()
- {
- if (on_click)
- on_click(m_color);
- m_selected = true;
- }
- CustomColorWidget::CustomColorWidget()
- {
- m_custom_colors = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 360, 512 });
- auto painter = Gfx::Painter(*m_custom_colors);
- for (int h = 0; h < 360; h++) {
- Gfx::HSV hsv;
- hsv.hue = h / 2;
- hsv.saturation = 255;
- for (int v = 0; v < 256; v++) {
- hsv.value = v;
- Color color = Color::from_hsv(hsv);
- painter.set_pixel({ h, v }, color);
- }
- hsv.value = 255;
- for (int s = 0; s < 256; s++) {
- hsv.saturation = 255 - s;
- Color color = Color::from_hsv(hsv);
- painter.set_pixel({ h, 256 + s }, color);
- }
- }
- }
- void CustomColorWidget::clear_last_position()
- {
- m_last_position = { -1, -1 };
- update();
- }
- void CustomColorWidget::fire_event(GUI::MouseEvent& event)
- {
- if (!m_status)
- return;
- if (!on_pick)
- return;
- auto position = event.position();
- if (!this->rect().contains(position)) {
- return;
- }
- m_last_position = position;
- auto color = this->window()->back_bitmap()->get_pixel(position);
- on_pick(color);
- update();
- }
- void CustomColorWidget::mousedown_event(GUI::MouseEvent& event)
- {
- if (event.button() == GUI::MouseButton::Left) {
- m_status = true;
- } else {
- m_status = false;
- }
- }
- void CustomColorWidget::mouseup_event(GUI::MouseEvent& event)
- {
- fire_event(event);
- m_status = false;
- }
- void CustomColorWidget::mousemove_event(GUI::MouseEvent& event)
- {
- fire_event(event);
- }
- void CustomColorWidget::paint_event(GUI::PaintEvent& event)
- {
- GUI::Painter painter(*this);
- Gfx::Rect rect = event.rect();
- painter.add_clip_rect(rect);
- painter.draw_scaled_bitmap(rect, *m_custom_colors, m_custom_colors->rect());
- painter.draw_line({ m_last_position.x(), 0 }, { m_last_position.x(), rect.height() }, Color::Black);
- painter.draw_line({ 0, m_last_position.y() }, { rect.width(), m_last_position.y() }, Color::Black);
- }
- void CustomColorWidget::resize_event(ResizeEvent&)
- {
- clear_last_position();
- }
- }
|