ColorPicker.cpp 14 KB

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