Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "PaletteWidget.h"
28#include "PaintableWidget.h"
29#include <LibGUI/BoxLayout.h>
30#include <LibGUI/ColorPicker.h>
31#include <LibGfx/Palette.h>
32
33class ColorWidget : public GUI::Frame {
34 C_OBJECT(ColorWidget)
35public:
36 explicit ColorWidget(Color color, PaletteWidget& palette_widget)
37 : m_palette_widget(palette_widget)
38 , m_color(color)
39 {
40 }
41
42 virtual ~ColorWidget() override
43 {
44 }
45
46 virtual void mousedown_event(GUI::MouseEvent& event) override
47 {
48 if (event.modifiers() & KeyModifier::Mod_Ctrl && event.button() == GUI::MouseButton::Left) {
49 auto dialog = GUI::ColorPicker::construct(m_color, window());
50 if (dialog->exec() == GUI::Dialog::ExecOK) {
51 m_color = dialog->color();
52 auto pal = palette();
53 pal.set_color(ColorRole::Background, m_color);
54 set_palette(pal);
55 update();
56 }
57 return;
58 }
59
60 if (event.button() == GUI::MouseButton::Left)
61 m_palette_widget.set_primary_color(m_color);
62 else if (event.button() == GUI::MouseButton::Right)
63 m_palette_widget.set_secondary_color(m_color);
64 }
65
66private:
67 PaletteWidget& m_palette_widget;
68 Color m_color;
69};
70
71PaletteWidget::PaletteWidget(PaintableWidget& paintable_widget)
72 : m_paintable_widget(paintable_widget)
73{
74 set_frame_shape(Gfx::FrameShape::Panel);
75 set_frame_shadow(Gfx::FrameShadow::Raised);
76 set_frame_thickness(0);
77 set_fill_with_background_color(true);
78
79 set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
80 set_preferred_size(0, 34);
81
82 m_secondary_color_widget = add<GUI::Frame>();
83 m_secondary_color_widget->set_relative_rect({ 2, 2, 60, 31 });
84 m_secondary_color_widget->set_fill_with_background_color(true);
85 set_secondary_color(paintable_widget.secondary_color());
86
87 m_primary_color_widget = add<GUI::Frame>();
88 Gfx::Rect rect { 0, 0, 38, 15 };
89 rect.center_within(m_secondary_color_widget->relative_rect());
90 m_primary_color_widget->set_relative_rect(rect);
91 m_primary_color_widget->set_fill_with_background_color(true);
92 set_primary_color(paintable_widget.primary_color());
93
94 paintable_widget.on_primary_color_change = [this](Color color) {
95 set_primary_color(color);
96 };
97
98 paintable_widget.on_secondary_color_change = [this](Color color) {
99 set_secondary_color(color);
100 };
101
102 auto& color_container = add<GUI::Widget>();
103 color_container.set_relative_rect(m_secondary_color_widget->relative_rect().right() + 2, 2, 500, 32);
104 color_container.set_layout<GUI::VerticalBoxLayout>();
105 color_container.layout()->set_spacing(1);
106
107 auto& top_color_container = color_container.add<GUI::Widget>();
108 top_color_container.set_layout<GUI::HorizontalBoxLayout>();
109 top_color_container.layout()->set_spacing(1);
110
111 auto& bottom_color_container = color_container.add<GUI::Widget>();
112 bottom_color_container.set_layout<GUI::HorizontalBoxLayout>();
113 bottom_color_container.layout()->set_spacing(1);
114
115 auto add_color_widget = [&](GUI::Widget& container, Color color) {
116 auto& color_widget = container.add<ColorWidget>(color, *this);
117 color_widget.set_fill_with_background_color(true);
118 auto pal = color_widget.palette();
119 pal.set_color(ColorRole::Background, color);
120 color_widget.set_palette(pal);
121 };
122
123 add_color_widget(top_color_container, Color::from_rgb(0x000000));
124 add_color_widget(top_color_container, Color::from_rgb(0x808080));
125 add_color_widget(top_color_container, Color::from_rgb(0x800000));
126 add_color_widget(top_color_container, Color::from_rgb(0x808000));
127 add_color_widget(top_color_container, Color::from_rgb(0x008000));
128 add_color_widget(top_color_container, Color::from_rgb(0x008080));
129 add_color_widget(top_color_container, Color::from_rgb(0x000080));
130 add_color_widget(top_color_container, Color::from_rgb(0x800080));
131 add_color_widget(top_color_container, Color::from_rgb(0x808040));
132 add_color_widget(top_color_container, Color::from_rgb(0x004040));
133 add_color_widget(top_color_container, Color::from_rgb(0x0080ff));
134 add_color_widget(top_color_container, Color::from_rgb(0x004080));
135 add_color_widget(top_color_container, Color::from_rgb(0x8000ff));
136 add_color_widget(top_color_container, Color::from_rgb(0x804000));
137
138 add_color_widget(bottom_color_container, Color::from_rgb(0xffffff));
139 add_color_widget(bottom_color_container, Color::from_rgb(0xc0c0c0));
140 add_color_widget(bottom_color_container, Color::from_rgb(0xff0000));
141 add_color_widget(bottom_color_container, Color::from_rgb(0xffff00));
142 add_color_widget(bottom_color_container, Color::from_rgb(0x00ff00));
143 add_color_widget(bottom_color_container, Color::from_rgb(0x00ffff));
144 add_color_widget(bottom_color_container, Color::from_rgb(0x0000ff));
145 add_color_widget(bottom_color_container, Color::from_rgb(0xff00ff));
146 add_color_widget(bottom_color_container, Color::from_rgb(0xffff80));
147 add_color_widget(bottom_color_container, Color::from_rgb(0x00ff80));
148 add_color_widget(bottom_color_container, Color::from_rgb(0x80ffff));
149 add_color_widget(bottom_color_container, Color::from_rgb(0x8080ff));
150 add_color_widget(bottom_color_container, Color::from_rgb(0xff0080));
151 add_color_widget(bottom_color_container, Color::from_rgb(0xff8040));
152}
153
154PaletteWidget::~PaletteWidget()
155{
156}
157
158void PaletteWidget::set_primary_color(Color color)
159{
160 m_paintable_widget.set_primary_color(color);
161 auto pal = m_primary_color_widget->palette();
162 pal.set_color(ColorRole::Background, color);
163 m_primary_color_widget->set_palette(pal);
164 m_primary_color_widget->update();
165}
166
167void PaletteWidget::set_secondary_color(Color color)
168{
169 m_paintable_widget.set_secondary_color(color);
170 auto pal = m_secondary_color_widget->palette();
171 pal.set_color(ColorRole::Background, color);
172 m_secondary_color_widget->set_palette(pal);
173 m_secondary_color_widget->update();
174}