ColorPicker.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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(unsigned modifiers = 0) 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 ColorField final : public GUI::Frame {
  55. C_OBJECT(ColorField);
  56. public:
  57. Function<void(Color)> on_pick;
  58. void set_color(Color);
  59. void set_hue(double);
  60. private:
  61. ColorField(Color color);
  62. Color m_color;
  63. // save hue seperately so full white color doesn't reset it to 0
  64. double m_hue;
  65. RefPtr<Gfx::Bitmap> m_color_bitmap;
  66. bool m_being_pressed { false };
  67. Gfx::IntPoint m_last_position;
  68. void create_color_bitmap();
  69. void pick_color_at_position(GUI::MouseEvent& event);
  70. void recalculate_position();
  71. virtual void mousedown_event(GUI::MouseEvent&) override;
  72. virtual void mouseup_event(GUI::MouseEvent&) override;
  73. virtual void mousemove_event(GUI::MouseEvent&) override;
  74. virtual void paint_event(GUI::PaintEvent&) override;
  75. virtual void resize_event(ResizeEvent&) override;
  76. };
  77. class ColorSlider final : public GUI::Frame {
  78. C_OBJECT(ColorSlider);
  79. public:
  80. Function<void(double)> on_pick;
  81. void set_value(double);
  82. private:
  83. ColorSlider(double value);
  84. double m_value;
  85. RefPtr<Gfx::Bitmap> m_color_bitmap;
  86. bool m_being_pressed { false };
  87. int m_last_position;
  88. void pick_value_at_position(GUI::MouseEvent& event);
  89. void recalculate_position();
  90. virtual void mousedown_event(GUI::MouseEvent&) override;
  91. virtual void mouseup_event(GUI::MouseEvent&) override;
  92. virtual void mousemove_event(GUI::MouseEvent&) override;
  93. virtual void paint_event(GUI::PaintEvent&) override;
  94. virtual void resize_event(ResizeEvent&) override;
  95. };
  96. class CustomColorWidget final : public GUI::Widget {
  97. C_OBJECT(CustomColorWidget);
  98. public:
  99. Function<void(Color)> on_pick;
  100. void set_color(Color);
  101. private:
  102. CustomColorWidget(Color);
  103. RefPtr<ColorField> m_color_field;
  104. RefPtr<ColorSlider> m_color_slider;
  105. };
  106. ColorPicker::ColorPicker(Color color, Window* parent_window, String title)
  107. : Dialog(parent_window)
  108. , m_color(color)
  109. {
  110. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/color-chooser.png"));
  111. set_title(title);
  112. set_resizable(false);
  113. resize(500, 326);
  114. build_ui();
  115. }
  116. ColorPicker::~ColorPicker()
  117. {
  118. }
  119. void ColorPicker::build_ui()
  120. {
  121. auto& root_container = set_main_widget<Widget>();
  122. root_container.set_layout<VerticalBoxLayout>();
  123. root_container.layout()->set_margins({ 4, 4, 4, 4 });
  124. root_container.set_fill_with_background_color(true);
  125. auto& tab_widget = root_container.add<GUI::TabWidget>();
  126. auto& tab_palette = tab_widget.add_tab<Widget>("Palette");
  127. tab_palette.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  128. tab_palette.set_layout<VerticalBoxLayout>();
  129. tab_palette.layout()->set_margins({ 4, 4, 4, 4 });
  130. tab_palette.layout()->set_spacing(4);
  131. build_ui_palette(tab_palette);
  132. auto& tab_custom_color = tab_widget.add_tab<Widget>("Custom Color");
  133. tab_custom_color.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  134. tab_custom_color.set_layout<VerticalBoxLayout>();
  135. tab_custom_color.layout()->set_margins({ 4, 4, 4, 4 });
  136. tab_custom_color.layout()->set_spacing(4);
  137. build_ui_custom(tab_custom_color);
  138. auto& button_container = root_container.add<Widget>();
  139. button_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  140. button_container.set_preferred_size(0, 22);
  141. button_container.set_layout<HorizontalBoxLayout>();
  142. button_container.layout()->set_spacing(4);
  143. button_container.layout()->add_spacer();
  144. auto& ok_button = button_container.add<Button>();
  145. ok_button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  146. ok_button.set_preferred_size(80, 0);
  147. ok_button.set_text("OK");
  148. ok_button.on_click = [this](auto) {
  149. done(ExecOK);
  150. };
  151. auto& cancel_button = button_container.add<Button>();
  152. cancel_button.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  153. cancel_button.set_preferred_size(80, 0);
  154. cancel_button.set_text("Cancel");
  155. cancel_button.on_click = [this](auto) {
  156. done(ExecCancel);
  157. };
  158. }
  159. void ColorPicker::build_ui_palette(Widget& root_container)
  160. {
  161. unsigned colors[4][9] = {
  162. { 0xef2929, 0xf0b143, 0xfce94f, 0x9fe13a, 0x7c9ece, 0xa680a8, 0xe1ba70, 0x888a85, 0xeeeeec },
  163. { 0xba1e09, 0xf57900, 0xe9d51a, 0x8bd121, 0x4164a3, 0x6f517b, 0xb77f19, 0x555753, 0xd4d7cf },
  164. { 0x961605, 0xbf600c, 0xe9d51a, 0x619910, 0x2b4986, 0x573666, 0x875b09, 0x2f3436, 0xbbbdb6 },
  165. { 0x000000, 0x2f3436, 0x555753, 0x808080, 0xbabdb6, 0xd3d7cf, 0xeeeeec, 0xf3f3f3, 0xffffff }
  166. };
  167. for (int r = 0; r < 4; r++) {
  168. auto& colors_row = root_container.add<Widget>();
  169. colors_row.set_layout<HorizontalBoxLayout>();
  170. colors_row.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  171. for (int i = 0; i < 8; i++) {
  172. create_color_button(colors_row, colors[r][i]);
  173. }
  174. }
  175. }
  176. void ColorPicker::build_ui_custom(Widget& root_container)
  177. {
  178. enum RGBComponent {
  179. Red,
  180. Green,
  181. Blue
  182. };
  183. auto& horizontal_container = root_container.add<Widget>();
  184. horizontal_container.set_fill_with_background_color(true);
  185. horizontal_container.set_layout<HorizontalBoxLayout>();
  186. // Left Side
  187. m_custom_color = horizontal_container.add<CustomColorWidget>(m_color);
  188. m_custom_color->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  189. m_custom_color->set_preferred_size(299, 260);
  190. m_custom_color->on_pick = [this](Color color) {
  191. if (m_color == color)
  192. return;
  193. m_color = color;
  194. update_color_widgets();
  195. };
  196. // Right Side
  197. auto& vertical_container = horizontal_container.add<Widget>();
  198. vertical_container.set_size_policy(SizePolicy::Fixed, SizePolicy::Fill);
  199. vertical_container.set_layout<VerticalBoxLayout>();
  200. vertical_container.layout()->set_margins({ 12, 0, 0, 0 });
  201. vertical_container.set_preferred_size(128, 0);
  202. auto& preview_container = vertical_container.add<Frame>();
  203. preview_container.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  204. preview_container.set_layout<VerticalBoxLayout>();
  205. preview_container.layout()->set_margins({ 2, 2, 2, 2 });
  206. preview_container.layout()->set_spacing(0);
  207. preview_container.set_preferred_size(0, 128);
  208. // Current color
  209. auto& current_color_widget = preview_container.add<Widget>();
  210. current_color_widget.set_fill_with_background_color(true);
  211. auto pal1 = current_color_widget.palette();
  212. pal1.set_color(ColorRole::Background, m_color);
  213. current_color_widget.set_palette(pal1);
  214. // Preview selected color
  215. m_preview_widget = preview_container.add<Widget>();
  216. m_preview_widget->set_fill_with_background_color(true);
  217. auto pal2 = m_preview_widget->palette();
  218. pal2.set_color(ColorRole::Background, m_color);
  219. m_preview_widget->set_palette(pal2);
  220. vertical_container.layout()->add_spacer();
  221. // HTML
  222. auto& html_container = vertical_container.add<GUI::Widget>();
  223. html_container.set_layout<GUI::HorizontalBoxLayout>();
  224. html_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  225. html_container.set_preferred_size(0, 22);
  226. auto& html_label = html_container.add<GUI::Label>();
  227. html_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  228. html_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  229. html_label.set_preferred_size({ 70, 0 });
  230. html_label.set_text("HTML:");
  231. m_html_text = html_container.add<GUI::TextBox>();
  232. m_html_text->set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fill);
  233. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  234. m_html_text->on_change = [this]() {
  235. auto color_name = m_html_text->text();
  236. auto optional_color = Color::from_string(color_name);
  237. if (optional_color.has_value()) {
  238. auto color = optional_color.value();
  239. if (m_color == color)
  240. return;
  241. m_color = optional_color.value();
  242. m_custom_color->set_color(color);
  243. update_color_widgets();
  244. }
  245. };
  246. // RGB Lines
  247. auto make_spinbox = [&](RGBComponent component, int initial_value) {
  248. auto& rgb_container = vertical_container.add<GUI::Widget>();
  249. rgb_container.set_layout<GUI::HorizontalBoxLayout>();
  250. rgb_container.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  251. rgb_container.set_preferred_size(0, 22);
  252. auto& rgb_label = rgb_container.add<GUI::Label>();
  253. rgb_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  254. rgb_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  255. rgb_label.set_preferred_size({ 70, 0 });
  256. auto& spinbox = rgb_container.add<SpinBox>();
  257. spinbox.set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  258. spinbox.set_preferred_size(0, 20);
  259. spinbox.set_min(0);
  260. spinbox.set_max(255);
  261. spinbox.set_value(initial_value);
  262. spinbox.on_change = [this, component](auto value) {
  263. auto color = m_color;
  264. if (component == Red)
  265. color.set_red(value);
  266. if (component == Green)
  267. color.set_green(value);
  268. if (component == Blue)
  269. color.set_blue(value);
  270. if (m_color == color)
  271. return;
  272. m_color = color;
  273. m_custom_color->set_color(color);
  274. update_color_widgets();
  275. };
  276. if (component == Red) {
  277. rgb_label.set_text("Red:");
  278. m_red_spinbox = spinbox;
  279. } else if (component == Green) {
  280. rgb_label.set_text("Green:");
  281. m_green_spinbox = spinbox;
  282. } else if (component == Blue) {
  283. rgb_label.set_text("Blue:");
  284. m_blue_spinbox = spinbox;
  285. }
  286. };
  287. make_spinbox(Red, m_color.red());
  288. make_spinbox(Green, m_color.green());
  289. make_spinbox(Blue, m_color.blue());
  290. }
  291. void ColorPicker::update_color_widgets()
  292. {
  293. auto pal = m_preview_widget->palette();
  294. pal.set_color(ColorRole::Background, m_color);
  295. m_preview_widget->set_palette(pal);
  296. m_preview_widget->update();
  297. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  298. m_red_spinbox->set_value(m_color.red());
  299. m_green_spinbox->set_value(m_color.green());
  300. m_blue_spinbox->set_value(m_color.blue());
  301. }
  302. void ColorPicker::create_color_button(Widget& container, unsigned rgb)
  303. {
  304. Color color = Color::from_rgb(rgb);
  305. auto& widget = container.add<ColorButton>(*this, color);
  306. widget.set_size_policy(SizePolicy::Fill, SizePolicy::Fill);
  307. widget.on_click = [this](Color color) {
  308. for (auto& value : m_color_widgets) {
  309. value->set_selected(false);
  310. value->update();
  311. }
  312. m_color = color;
  313. m_custom_color->set_color(color);
  314. update_color_widgets();
  315. };
  316. if (color == m_color) {
  317. widget.set_selected(true);
  318. }
  319. m_color_widgets.append(&widget);
  320. }
  321. ColorButton::ColorButton(ColorPicker& picker, Color color)
  322. : m_picker(picker)
  323. {
  324. m_color = color;
  325. }
  326. ColorButton::~ColorButton()
  327. {
  328. }
  329. void ColorButton::set_selected(bool selected)
  330. {
  331. m_selected = selected;
  332. }
  333. void ColorButton::doubleclick_event(GUI::MouseEvent&)
  334. {
  335. click();
  336. m_selected = true;
  337. m_picker.done(Dialog::ExecOK);
  338. }
  339. void ColorButton::paint_event(PaintEvent& event)
  340. {
  341. Painter painter(*this);
  342. painter.add_clip_rect(event.rect());
  343. Gfx::StylePainter::paint_button(painter, rect(), palette(), Gfx::ButtonStyle::Normal, is_being_pressed(), is_hovered(), is_checked(), is_enabled());
  344. painter.fill_rect({ 1, 1, rect().width() - 2, rect().height() - 2 }, m_color);
  345. if (m_selected) {
  346. painter.fill_rect({ 3, 3, rect().width() - 6, rect().height() - 6 }, Color::Black);
  347. painter.fill_rect({ 5, 5, rect().width() - 10, rect().height() - 10 }, Color::White);
  348. painter.fill_rect({ 7, 6, rect().width() - 14, rect().height() - 14 }, m_color);
  349. }
  350. }
  351. void ColorButton::click(unsigned)
  352. {
  353. if (on_click)
  354. on_click(m_color);
  355. m_selected = true;
  356. }
  357. CustomColorWidget::CustomColorWidget(Color color)
  358. {
  359. set_layout<HorizontalBoxLayout>();
  360. m_color_field = add<ColorField>(color);
  361. m_color_field->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  362. auto size = 256 + (m_color_field->frame_thickness() * 2);
  363. m_color_field->set_preferred_size(size, size);
  364. m_color_field->on_pick = [this](Color color) {
  365. if (on_pick)
  366. on_pick(color);
  367. };
  368. m_color_slider = add<ColorSlider>(color.to_hsv().hue);
  369. m_color_slider->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
  370. auto slider_width = 32 + (m_color_slider->frame_thickness() * 2);
  371. m_color_slider->set_preferred_size(slider_width, size);
  372. m_color_slider->on_pick = [this](double value) {
  373. m_color_field->set_hue(value);
  374. };
  375. }
  376. void CustomColorWidget::set_color(Color color)
  377. {
  378. m_color_field->set_color(color);
  379. m_color_field->set_hue(color.to_hsv().hue);
  380. }
  381. ColorField::ColorField(Color color)
  382. : m_color(color)
  383. , m_hue(color.to_hsv().hue)
  384. {
  385. create_color_bitmap();
  386. }
  387. void ColorField::create_color_bitmap()
  388. {
  389. m_color_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 256, 256 });
  390. auto painter = Gfx::Painter(*m_color_bitmap);
  391. Gfx::HSV hsv;
  392. hsv.hue = m_hue;
  393. for (int x = 0; x < 256; x++) {
  394. hsv.saturation = x / 255.0f;
  395. for (int y = 0; y < 256; y++) {
  396. hsv.value = (255 - y) / 255.0f;
  397. Color color = Color::from_hsv(hsv);
  398. painter.set_pixel({ x, y }, color);
  399. }
  400. }
  401. }
  402. void ColorField::set_color(Color color)
  403. {
  404. if (m_color == color)
  405. return;
  406. m_color = color;
  407. // don't save m_hue here by default, we dont want to set it to 0 in case color is full white
  408. // m_hue = color.to_hsv().hue;
  409. recalculate_position();
  410. }
  411. void ColorField::recalculate_position()
  412. {
  413. Gfx::HSV hsv = m_color.to_hsv();
  414. auto x = hsv.saturation * width();
  415. auto y = (1 - hsv.value) * height();
  416. m_last_position = Gfx::IntPoint(x, y);
  417. update();
  418. }
  419. void ColorField::set_hue(double hue)
  420. {
  421. if (m_hue == hue)
  422. return;
  423. auto hsv = m_color.to_hsv();
  424. hsv.hue = hue;
  425. m_hue = hue;
  426. create_color_bitmap();
  427. auto color = Color::from_hsv(hsv);
  428. set_color(color);
  429. if (on_pick)
  430. on_pick(color);
  431. }
  432. void ColorField::pick_color_at_position(GUI::MouseEvent& event)
  433. {
  434. if (!m_being_pressed)
  435. return;
  436. auto inner_rect = frame_inner_rect();
  437. auto position = event.position().constrained(inner_rect).translated(-frame_thickness(), -frame_thickness());
  438. auto color = Color::from_hsv(m_hue, (double)position.x() / inner_rect.width(), (double)(inner_rect.height() - position.y()) / inner_rect.height());
  439. m_last_position = position;
  440. m_color = color;
  441. if (on_pick)
  442. on_pick(color);
  443. update();
  444. }
  445. void ColorField::mousedown_event(GUI::MouseEvent& event)
  446. {
  447. if (event.button() == GUI::MouseButton::Left) {
  448. m_being_pressed = true;
  449. pick_color_at_position(event);
  450. }
  451. }
  452. void ColorField::mouseup_event(GUI::MouseEvent& event)
  453. {
  454. if (event.button() == GUI::MouseButton::Left) {
  455. m_being_pressed = false;
  456. pick_color_at_position(event);
  457. }
  458. }
  459. void ColorField::mousemove_event(GUI::MouseEvent& event)
  460. {
  461. if (event.buttons() & GUI::MouseButton::Left)
  462. pick_color_at_position(event);
  463. }
  464. void ColorField::paint_event(GUI::PaintEvent& event)
  465. {
  466. Frame::paint_event(event);
  467. Painter painter(*this);
  468. painter.add_clip_rect(event.rect());
  469. painter.add_clip_rect(frame_inner_rect());
  470. painter.draw_scaled_bitmap(frame_inner_rect(), *m_color_bitmap, m_color_bitmap->rect());
  471. painter.translate(frame_thickness(), frame_thickness());
  472. painter.draw_line({ m_last_position.x() - 1, 0 }, { m_last_position.x() - 1, height() }, Color::White);
  473. painter.draw_line({ m_last_position.x() + 1, 0 }, { m_last_position.x() + 1, height() }, Color::White);
  474. painter.draw_line({ 0, m_last_position.y() - 1 }, { width(), m_last_position.y() - 1 }, Color::White);
  475. painter.draw_line({ 0, m_last_position.y() + 1 }, { width(), m_last_position.y() + 1 }, Color::White);
  476. painter.draw_line({ m_last_position.x(), 0 }, { m_last_position.x(), height() }, Color::Black);
  477. painter.draw_line({ 0, m_last_position.y() }, { width(), m_last_position.y() }, Color::Black);
  478. }
  479. void ColorField::resize_event(ResizeEvent&)
  480. {
  481. recalculate_position();
  482. }
  483. ColorSlider::ColorSlider(double value)
  484. : m_value(value)
  485. {
  486. m_color_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 32, 360 });
  487. auto painter = Gfx::Painter(*m_color_bitmap);
  488. for (int h = 0; h < 360; h++) {
  489. Gfx::HSV hsv;
  490. hsv.hue = h;
  491. hsv.saturation = 1.0;
  492. hsv.value = 1.0;
  493. Color color = Color::from_hsv(hsv);
  494. painter.draw_line({ 0, h }, { 32, h }, color);
  495. }
  496. }
  497. void ColorSlider::set_value(double value)
  498. {
  499. if (m_value == value)
  500. return;
  501. m_value = value;
  502. recalculate_position();
  503. }
  504. void ColorSlider::recalculate_position()
  505. {
  506. m_last_position = (m_value / 360.0) * height();
  507. update();
  508. }
  509. void ColorSlider::pick_value_at_position(GUI::MouseEvent& event)
  510. {
  511. if (!m_being_pressed)
  512. return;
  513. auto inner_rect = frame_inner_rect();
  514. auto position = event.position().constrained(inner_rect).translated(-frame_thickness(), -frame_thickness());
  515. auto hue = (double)position.y() / inner_rect.height() * 360;
  516. m_last_position = position.y();
  517. m_value = hue;
  518. if (on_pick)
  519. on_pick(m_value);
  520. update();
  521. }
  522. void ColorSlider::mousedown_event(GUI::MouseEvent& event)
  523. {
  524. if (event.button() == GUI::MouseButton::Left) {
  525. m_being_pressed = true;
  526. pick_value_at_position(event);
  527. }
  528. }
  529. void ColorSlider::mouseup_event(GUI::MouseEvent& event)
  530. {
  531. if (event.button() == GUI::MouseButton::Left) {
  532. m_being_pressed = false;
  533. pick_value_at_position(event);
  534. }
  535. }
  536. void ColorSlider::mousemove_event(GUI::MouseEvent& event)
  537. {
  538. if (event.buttons() & GUI::MouseButton::Left)
  539. pick_value_at_position(event);
  540. }
  541. void ColorSlider::paint_event(GUI::PaintEvent& event)
  542. {
  543. Frame::paint_event(event);
  544. Painter painter(*this);
  545. painter.add_clip_rect(event.rect());
  546. painter.add_clip_rect(frame_inner_rect());
  547. painter.draw_scaled_bitmap(frame_inner_rect(), *m_color_bitmap, m_color_bitmap->rect());
  548. painter.translate(frame_thickness(), frame_thickness());
  549. painter.draw_line({ 0, m_last_position - 1 }, { width(), m_last_position - 1 }, Color::White);
  550. painter.draw_line({ 0, m_last_position + 1 }, { width(), m_last_position + 1 }, Color::White);
  551. painter.draw_line({ 0, m_last_position }, { width(), m_last_position }, Color::Black);
  552. }
  553. void ColorSlider::resize_event(ResizeEvent&)
  554. {
  555. recalculate_position();
  556. }
  557. }