ColorPicker.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #include <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Button.h>
  28. #include <LibGUI/ColorPicker.h>
  29. #include <LibGUI/Frame.h>
  30. #include <LibGUI/Label.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/SpinBox.h>
  33. #include <LibGUI/TabWidget.h>
  34. #include <LibGUI/TextBox.h>
  35. #include <LibGfx/Palette.h>
  36. namespace GUI {
  37. class ColorButton : public AbstractButton {
  38. C_OBJECT(ColorButton)
  39. public:
  40. virtual ~ColorButton() override;
  41. void set_selected(bool selected);
  42. Color color() const { return m_color; }
  43. Function<void(const Color)> on_click;
  44. protected:
  45. virtual void click() override;
  46. virtual void doubleclick_event(GUI::MouseEvent&) override;
  47. virtual void paint_event(PaintEvent&) override;
  48. private:
  49. explicit ColorButton(ColorPicker& picker, Color color = {});
  50. ColorPicker& m_picker;
  51. Color m_color;
  52. bool m_selected { false };
  53. };
  54. class CustomColorWidget final : public GUI::Widget {
  55. C_OBJECT(CustomColorWidget);
  56. public:
  57. Function<void(Color)> on_pick;
  58. void clear_last_position();
  59. private:
  60. CustomColorWidget();
  61. RefPtr<Gfx::Bitmap> m_custom_colors;
  62. bool m_being_pressed { false };
  63. Gfx::Point m_last_position;
  64. void pick_color_at_position(GUI::MouseEvent& event);
  65. virtual void mousedown_event(GUI::MouseEvent&) override;
  66. virtual void mouseup_event(GUI::MouseEvent&) override;
  67. virtual void mousemove_event(GUI::MouseEvent&) override;
  68. virtual void paint_event(GUI::PaintEvent&) override;
  69. virtual void resize_event(ResizeEvent&) override;
  70. };
  71. ColorPicker::ColorPicker(Color color, Window* parent_window, String title)
  72. : Dialog(parent_window)
  73. , m_color(color)
  74. {
  75. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/color-chooser.png"));
  76. set_title(title);
  77. set_resizable(false);
  78. resize(530, 325);
  79. build_ui();
  80. }
  81. ColorPicker::~ColorPicker()
  82. {
  83. }
  84. void ColorPicker::build_ui()
  85. {
  86. auto& root_container = set_main_widget<Widget>();
  87. root_container.set_layout<VerticalBoxLayout>();
  88. root_container.layout()->set_margins({ 4, 4, 4, 4 });
  89. root_container.set_fill_with_background_color(true);
  90. auto& tab_widget = root_container.add<GUI::TabWidget>();
  91. auto& tab_palette = tab_widget.add_tab<Widget>("Palette");
  92. tab_palette.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  93. tab_palette.set_layout<VerticalBoxLayout>();
  94. tab_palette.layout()->set_margins({ 4, 4, 4, 4 });
  95. tab_palette.layout()->set_spacing(4);
  96. build_ui_palette(tab_palette);
  97. auto& tab_custom_color = tab_widget.add_tab<Widget>("Custom Color");
  98. tab_custom_color.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  99. tab_custom_color.set_layout<VerticalBoxLayout>();
  100. tab_custom_color.layout()->set_margins({ 4, 4, 4, 4 });
  101. tab_custom_color.layout()->set_spacing(4);
  102. build_ui_custom(tab_custom_color);
  103. auto& button_container = root_container.add<Widget>();
  104. button_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  105. button_container.set_preferred_size(0, 22);
  106. button_container.set_layout<HorizontalBoxLayout>();
  107. button_container.layout()->set_spacing(4);
  108. button_container.layout()->add_spacer();
  109. auto& cancel_button = button_container.add<Button>();
  110. cancel_button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  111. cancel_button.set_preferred_size(80, 0);
  112. cancel_button.set_text("Cancel");
  113. cancel_button.on_click = [this] {
  114. done(ExecCancel);
  115. };
  116. auto& ok_button = button_container.add<Button>();
  117. ok_button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  118. ok_button.set_preferred_size(80, 0);
  119. ok_button.set_text("Select");
  120. ok_button.on_click = [this] {
  121. done(ExecOK);
  122. };
  123. }
  124. void ColorPicker::build_ui_palette(Widget& root_container)
  125. {
  126. unsigned colors[4][9] = {
  127. { 0xef2929, 0xf0b143, 0xfce94f, 0x9fe13a, 0x7c9ece, 0xa680a8, 0xe1ba70, 0x888a85, 0xeeeeec },
  128. { 0xba1e09, 0xf57900, 0xe9d51a, 0x8bd121, 0x4164a3, 0x6f517b, 0xb77f19, 0x555753, 0xd4d7cf },
  129. { 0x961605, 0xbf600c, 0xe9d51a, 0x619910, 0x2b4986, 0x573666, 0x875b09, 0x2f3436, 0xbbbdb6 },
  130. { 0x000000, 0x2f3436, 0x555753, 0x808080, 0xbabdb6, 0xd3d7cf, 0xeeeeec, 0xf3f3f3, 0xffffff }
  131. };
  132. for (int r = 0; r < 4; r++) {
  133. auto& colors_row = root_container.add<Widget>();
  134. colors_row.set_layout<HorizontalBoxLayout>();
  135. colors_row.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  136. for (int i = 0; i < 8; i++) {
  137. create_color_button(colors_row, colors[r][i]);
  138. }
  139. }
  140. }
  141. void ColorPicker::build_ui_custom(Widget& root_container)
  142. {
  143. enum RGBComponent {
  144. Red,
  145. Green,
  146. Blue
  147. };
  148. auto& horizontal_container = root_container.add<Widget>();
  149. horizontal_container.set_fill_with_background_color(true);
  150. horizontal_container.set_layout<HorizontalBoxLayout>();
  151. // Left Side
  152. m_custom_color = horizontal_container.add<GUI::CustomColorWidget>();
  153. m_custom_color->set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  154. m_custom_color->on_pick = [this](Color color) {
  155. if (m_color == color)
  156. return;
  157. m_color = color;
  158. update_color_widgets();
  159. };
  160. // Right Side
  161. auto& vertical_container = horizontal_container.add<Widget>();
  162. vertical_container.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  163. vertical_container.set_layout<VerticalBoxLayout>();
  164. vertical_container.layout()->set_margins({ 4, 0, 0, 0 });
  165. vertical_container.set_preferred_size(150, 0);
  166. // Preview
  167. m_preview_widget = vertical_container.add<Frame>();
  168. m_preview_widget->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  169. m_preview_widget->set_preferred_size(0, 150);
  170. m_preview_widget->set_fill_with_background_color(true);
  171. auto pal = m_preview_widget->palette();
  172. pal.set_color(ColorRole::Background, m_color);
  173. m_preview_widget->set_palette(pal);
  174. vertical_container.layout()->add_spacer();
  175. // HTML
  176. auto& html_container = vertical_container.add<GUI::Widget>();
  177. html_container.set_layout<GUI::HorizontalBoxLayout>();
  178. html_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  179. html_container.set_preferred_size(0, 22);
  180. auto& html_label = html_container.add<GUI::Label>();
  181. html_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  182. html_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  183. html_label.set_preferred_size({ 70, 0 });
  184. html_label.set_text("HTML:");
  185. m_html_text = html_container.add<GUI::TextBox>();
  186. m_html_text->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  187. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  188. m_html_text->on_change = [this]() {
  189. auto color_name = this->m_html_text->text();
  190. auto optional_color = Color::from_string(color_name);
  191. if (optional_color.has_value()) {
  192. auto color = optional_color.value();
  193. if (m_color == color)
  194. return;
  195. m_color = optional_color.value();
  196. this->m_custom_color->clear_last_position();
  197. update_color_widgets();
  198. }
  199. };
  200. // RGB Lines
  201. auto make_spinbox = [&](RGBComponent component, int initial_value) {
  202. auto& rgb_container = vertical_container.add<GUI::Widget>();
  203. rgb_container.set_layout<GUI::HorizontalBoxLayout>();
  204. rgb_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  205. rgb_container.set_preferred_size(0, 22);
  206. auto& rgb_label = rgb_container.add<GUI::Label>();
  207. rgb_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  208. rgb_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  209. rgb_label.set_preferred_size({ 70, 0 });
  210. auto& spinbox = rgb_container.add<SpinBox>();
  211. spinbox.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  212. spinbox.set_preferred_size(0, 20);
  213. spinbox.set_min(0);
  214. spinbox.set_max(255);
  215. spinbox.set_value(initial_value);
  216. spinbox.on_change = [this, component](auto value) {
  217. auto color = m_color;
  218. if (component == Red)
  219. color.set_red(value);
  220. if (component == Green)
  221. color.set_green(value);
  222. if (component == Blue)
  223. color.set_blue(value);
  224. if (m_color == color)
  225. return;
  226. m_color = color;
  227. this->m_custom_color->clear_last_position();
  228. update_color_widgets();
  229. };
  230. if (component == Red) {
  231. rgb_label.set_text("Red:");
  232. m_red_spinbox = spinbox;
  233. } else if (component == Green) {
  234. rgb_label.set_text("Green:");
  235. m_green_spinbox = spinbox;
  236. } else if (component == Blue) {
  237. rgb_label.set_text("Blue:");
  238. m_blue_spinbox = spinbox;
  239. }
  240. };
  241. make_spinbox(Red, m_color.red());
  242. make_spinbox(Green, m_color.green());
  243. make_spinbox(Blue, m_color.blue());
  244. }
  245. void ColorPicker::update_color_widgets()
  246. {
  247. auto pal = m_preview_widget->palette();
  248. pal.set_color(ColorRole::Background, m_color);
  249. m_preview_widget->set_palette(pal);
  250. m_preview_widget->update();
  251. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  252. m_red_spinbox->set_value(m_color.red());
  253. m_green_spinbox->set_value(m_color.green());
  254. m_blue_spinbox->set_value(m_color.blue());
  255. }
  256. void ColorPicker::create_color_button(Widget& container, unsigned rgb)
  257. {
  258. Color color = Color::from_rgb(rgb);
  259. auto& widget = container.add<ColorButton>(*this, color);
  260. widget.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  261. widget.on_click = [this](Color color) {
  262. for (auto& value : m_color_widgets) {
  263. value->set_selected(false);
  264. value->update();
  265. }
  266. this->m_color = color;
  267. };
  268. if (color == m_color) {
  269. widget.set_selected(true);
  270. }
  271. m_color_widgets.append(&widget);
  272. }
  273. ColorButton::ColorButton(ColorPicker& picker, Color color)
  274. : m_picker(picker)
  275. {
  276. m_color = color;
  277. }
  278. ColorButton::~ColorButton()
  279. {
  280. }
  281. void ColorButton::set_selected(bool selected)
  282. {
  283. m_selected = selected;
  284. }
  285. void ColorButton::doubleclick_event(GUI::MouseEvent&)
  286. {
  287. click();
  288. m_selected = true;
  289. m_picker.done(Dialog::ExecOK);
  290. }
  291. void ColorButton::paint_event(PaintEvent& event)
  292. {
  293. Painter painter(*this);
  294. painter.add_clip_rect(event.rect());
  295. Gfx::StylePainter::paint_button(painter, rect(), palette(), Gfx::ButtonStyle::Normal, is_being_pressed(), is_hovered(), is_checked(), is_enabled());
  296. painter.fill_rect({ 1, 1, rect().width() - 2, rect().height() - 2 }, m_color);
  297. if (m_selected) {
  298. painter.fill_rect({ 3, 3, rect().width() - 6, rect().height() - 6 }, Color::Black);
  299. painter.fill_rect({ 5, 5, rect().width() - 10, rect().height() - 10 }, Color::White);
  300. painter.fill_rect({ 7, 6, rect().width() - 14, rect().height() - 14 }, m_color);
  301. }
  302. }
  303. void ColorButton::click()
  304. {
  305. if (on_click)
  306. on_click(m_color);
  307. m_selected = true;
  308. }
  309. CustomColorWidget::CustomColorWidget()
  310. {
  311. m_custom_colors = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 360, 512 });
  312. auto painter = Gfx::Painter(*m_custom_colors);
  313. for (int h = 0; h < 360; h++) {
  314. Gfx::HSV hsv;
  315. hsv.hue = h / 2;
  316. hsv.saturation = 255;
  317. for (int v = 0; v < 256; v++) {
  318. hsv.value = v;
  319. Color color = Color::from_hsv(hsv);
  320. painter.set_pixel({ h, v }, color);
  321. }
  322. hsv.value = 255;
  323. for (int s = 0; s < 256; s++) {
  324. hsv.saturation = 255 - s;
  325. Color color = Color::from_hsv(hsv);
  326. painter.set_pixel({ h, 256 + s }, color);
  327. }
  328. }
  329. }
  330. void CustomColorWidget::clear_last_position()
  331. {
  332. m_last_position = { -1, -1 };
  333. update();
  334. }
  335. void CustomColorWidget::pick_color_at_position(GUI::MouseEvent& event)
  336. {
  337. if (!m_being_pressed)
  338. return;
  339. auto position = event.position();
  340. if (!rect().contains(position))
  341. return;
  342. auto color = m_custom_colors->get_pixel(position);
  343. m_last_position = position;
  344. if (on_pick)
  345. on_pick(color);
  346. update();
  347. }
  348. void CustomColorWidget::mousedown_event(GUI::MouseEvent& event)
  349. {
  350. if (event.button() == GUI::MouseButton::Left) {
  351. m_being_pressed = true;
  352. pick_color_at_position(event);
  353. }
  354. }
  355. void CustomColorWidget::mouseup_event(GUI::MouseEvent& event)
  356. {
  357. if (event.button() == GUI::MouseButton::Left) {
  358. m_being_pressed = false;
  359. pick_color_at_position(event);
  360. }
  361. }
  362. void CustomColorWidget::mousemove_event(GUI::MouseEvent& event)
  363. {
  364. if (event.buttons() & GUI::MouseButton::Left)
  365. pick_color_at_position(event);
  366. }
  367. void CustomColorWidget::paint_event(GUI::PaintEvent& event)
  368. {
  369. GUI::Painter painter(*this);
  370. Gfx::Rect rect = event.rect();
  371. painter.add_clip_rect(rect);
  372. painter.draw_scaled_bitmap(rect, *m_custom_colors, m_custom_colors->rect());
  373. painter.draw_line({ m_last_position.x(), 0 }, { m_last_position.x(), rect.height() }, Color::Black);
  374. painter.draw_line({ 0, m_last_position.y() }, { rect.width(), m_last_position.y() }, Color::Black);
  375. }
  376. void CustomColorWidget::resize_event(ResizeEvent&)
  377. {
  378. clear_last_position();
  379. }
  380. }