ColorPicker.cpp 20 KB

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