ColorPicker.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGUI/BoxLayout.h>
  7. #include <LibGUI/Button.h>
  8. #include <LibGUI/ColorPicker.h>
  9. #include <LibGUI/ConnectionToWindowServer.h>
  10. #include <LibGUI/Frame.h>
  11. #include <LibGUI/Label.h>
  12. #include <LibGUI/Painter.h>
  13. #include <LibGUI/SpinBox.h>
  14. #include <LibGUI/TabWidget.h>
  15. #include <LibGUI/TextBox.h>
  16. #include <LibGfx/Palette.h>
  17. namespace GUI {
  18. class ColorButton : public AbstractButton {
  19. C_OBJECT(ColorButton);
  20. public:
  21. virtual ~ColorButton() override;
  22. void set_selected(bool selected);
  23. Color color() const { return m_color; }
  24. Function<void(const Color)> on_click;
  25. protected:
  26. virtual void click(unsigned modifiers = 0) override;
  27. virtual void doubleclick_event(GUI::MouseEvent&) override;
  28. virtual void paint_event(PaintEvent&) override;
  29. private:
  30. explicit ColorButton(ColorPicker& picker, Color color = {});
  31. ColorPicker& m_picker;
  32. Color m_color;
  33. bool m_selected { false };
  34. };
  35. class ColorField final : public GUI::Frame {
  36. C_OBJECT(ColorField);
  37. public:
  38. Function<void(Color)> on_pick;
  39. void set_color(Color);
  40. void set_hue(double);
  41. void set_hue_from_pick(double);
  42. private:
  43. ColorField(Color color);
  44. Color m_color;
  45. // save hue separately so full white color doesn't reset it to 0
  46. double m_hue;
  47. RefPtr<Gfx::Bitmap> m_color_bitmap;
  48. bool m_being_pressed { false };
  49. Gfx::IntPoint m_last_position;
  50. void create_color_bitmap();
  51. void pick_color_at_position(GUI::MouseEvent& event);
  52. void recalculate_position();
  53. virtual void mousedown_event(GUI::MouseEvent&) override;
  54. virtual void mouseup_event(GUI::MouseEvent&) override;
  55. virtual void mousemove_event(GUI::MouseEvent&) override;
  56. virtual void paint_event(GUI::PaintEvent&) override;
  57. virtual void resize_event(ResizeEvent&) override;
  58. };
  59. class ColorSlider final : public GUI::Frame {
  60. C_OBJECT(ColorSlider);
  61. public:
  62. Function<void(double)> on_pick;
  63. void set_value(double);
  64. private:
  65. ColorSlider(double value);
  66. double m_value;
  67. RefPtr<Gfx::Bitmap> m_color_bitmap;
  68. bool m_being_pressed { false };
  69. int m_last_position;
  70. void pick_value_at_position(GUI::MouseEvent& event);
  71. void recalculate_position();
  72. virtual void mousedown_event(GUI::MouseEvent&) override;
  73. virtual void mouseup_event(GUI::MouseEvent&) override;
  74. virtual void mousemove_event(GUI::MouseEvent&) override;
  75. virtual void paint_event(GUI::PaintEvent&) override;
  76. virtual void resize_event(ResizeEvent&) override;
  77. };
  78. class ColorPreview final : public GUI::Widget {
  79. C_OBJECT(ColorPreview);
  80. public:
  81. void set_color(Color);
  82. private:
  83. ColorPreview(Color);
  84. Color m_color;
  85. virtual void paint_event(GUI::PaintEvent&) override;
  86. };
  87. class CustomColorWidget final : public GUI::Widget {
  88. C_OBJECT(CustomColorWidget);
  89. public:
  90. Function<void(Color)> on_pick;
  91. void set_color(Color);
  92. private:
  93. CustomColorWidget(Color);
  94. RefPtr<ColorField> m_color_field;
  95. RefPtr<ColorSlider> m_color_slider;
  96. };
  97. class ColorSelectOverlay final : public Widget {
  98. C_OBJECT(ColorSelectOverlay)
  99. public:
  100. Optional<Color> exec()
  101. {
  102. m_event_loop = make<Core::EventLoop>();
  103. // FIXME: Allow creation of fully transparent windows without a backing store.
  104. auto window = Window::construct();
  105. window->set_main_widget(this);
  106. window->set_has_alpha_channel(true);
  107. window->set_fullscreen(true);
  108. window->set_frameless(true);
  109. window->show();
  110. if (!m_event_loop->exec())
  111. return {};
  112. return m_col;
  113. }
  114. virtual ~ColorSelectOverlay() override { }
  115. Function<void(Color)> on_color_changed;
  116. private:
  117. ColorSelectOverlay()
  118. {
  119. set_override_cursor(Gfx::StandardCursor::Eyedropper);
  120. }
  121. virtual void mousedown_event(GUI::MouseEvent&) override { m_event_loop->quit(1); }
  122. virtual void mousemove_event(GUI::MouseEvent&) override
  123. {
  124. auto new_col = ConnectionToWindowServer::the().get_color_under_cursor();
  125. if (!new_col.has_value())
  126. return;
  127. if (new_col == m_col)
  128. return;
  129. m_col = new_col.value();
  130. if (on_color_changed)
  131. on_color_changed(m_col);
  132. }
  133. virtual void keydown_event(GUI::KeyEvent& event) override
  134. {
  135. if (event.key() == KeyCode::Key_Escape) {
  136. event.accept();
  137. m_event_loop->quit(0);
  138. return;
  139. }
  140. }
  141. OwnPtr<Core::EventLoop> m_event_loop;
  142. Color m_col;
  143. };
  144. ColorPicker::ColorPicker(Color color, Window* parent_window, String title)
  145. : Dialog(parent_window)
  146. , m_color(color)
  147. {
  148. set_icon(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/color-chooser.png").release_value_but_fixme_should_propagate_errors());
  149. set_title(title);
  150. set_resizable(false);
  151. resize(458, 326);
  152. build_ui();
  153. }
  154. ColorPicker::~ColorPicker()
  155. {
  156. }
  157. void ColorPicker::set_color_has_alpha_channel(bool has_alpha)
  158. {
  159. if (m_color_has_alpha_channel == has_alpha)
  160. return;
  161. m_color_has_alpha_channel = has_alpha;
  162. update_color_widgets();
  163. }
  164. void ColorPicker::build_ui()
  165. {
  166. auto& root_container = set_main_widget<Widget>();
  167. root_container.set_layout<VerticalBoxLayout>();
  168. root_container.layout()->set_margins(4);
  169. root_container.set_fill_with_background_color(true);
  170. auto& tab_widget = root_container.add<GUI::TabWidget>();
  171. auto& tab_palette = tab_widget.add_tab<Widget>("Palette");
  172. tab_palette.set_layout<VerticalBoxLayout>();
  173. tab_palette.layout()->set_margins(4);
  174. tab_palette.layout()->set_spacing(4);
  175. build_ui_palette(tab_palette);
  176. auto& tab_custom_color = tab_widget.add_tab<Widget>("Custom Color");
  177. tab_custom_color.set_layout<VerticalBoxLayout>();
  178. tab_custom_color.layout()->set_margins(4);
  179. tab_custom_color.layout()->set_spacing(4);
  180. build_ui_custom(tab_custom_color);
  181. auto& button_container = root_container.add<Widget>();
  182. button_container.set_fixed_height(22);
  183. button_container.set_layout<HorizontalBoxLayout>();
  184. button_container.layout()->set_spacing(4);
  185. button_container.layout()->add_spacer();
  186. auto& ok_button = button_container.add<Button>();
  187. ok_button.set_fixed_width(80);
  188. ok_button.set_text("OK");
  189. ok_button.on_click = [this](auto) {
  190. done(ExecOK);
  191. };
  192. auto& cancel_button = button_container.add<Button>();
  193. cancel_button.set_fixed_width(80);
  194. cancel_button.set_text("Cancel");
  195. cancel_button.on_click = [this](auto) {
  196. done(ExecCancel);
  197. };
  198. }
  199. void ColorPicker::build_ui_palette(Widget& root_container)
  200. {
  201. unsigned colors[4][9] = {
  202. { 0xef2929, 0xf0b143, 0xfce94f, 0x9fe13a, 0x7c9ece, 0xa680a8, 0xe1ba70, 0x888a85, 0xeeeeec },
  203. { 0xba1e09, 0xf57900, 0xe9d51a, 0x8bd121, 0x4164a3, 0x6f517b, 0xb77f19, 0x555753, 0xd4d7cf },
  204. { 0x961605, 0xbf600c, 0xe9d51a, 0x619910, 0x2b4986, 0x573666, 0x875b09, 0x2f3436, 0xbbbdb6 },
  205. { 0x000000, 0x2f3436, 0x555753, 0x808080, 0xbabdb6, 0xd3d7cf, 0xeeeeec, 0xf3f3f3, 0xffffff }
  206. };
  207. for (int r = 0; r < 4; r++) {
  208. auto& colors_row = root_container.add<Widget>();
  209. colors_row.set_layout<HorizontalBoxLayout>();
  210. for (int i = 0; i < 8; i++) {
  211. create_color_button(colors_row, colors[r][i]);
  212. }
  213. }
  214. }
  215. void ColorPicker::build_ui_custom(Widget& root_container)
  216. {
  217. enum RGBComponent {
  218. Red,
  219. Green,
  220. Blue,
  221. Alpha
  222. };
  223. auto& horizontal_container = root_container.add<Widget>();
  224. horizontal_container.set_fill_with_background_color(true);
  225. horizontal_container.set_layout<HorizontalBoxLayout>();
  226. // Left Side
  227. m_custom_color = horizontal_container.add<CustomColorWidget>(m_color);
  228. m_custom_color->set_fixed_size(299, 260);
  229. m_custom_color->on_pick = [this](Color color) {
  230. if (m_color == color)
  231. return;
  232. m_color = color;
  233. update_color_widgets();
  234. };
  235. // Right Side
  236. auto& vertical_container = horizontal_container.add<Widget>();
  237. vertical_container.set_layout<VerticalBoxLayout>();
  238. vertical_container.layout()->set_margins({ 0, 0, 0, 8 });
  239. vertical_container.set_fixed_width(128);
  240. auto& preview_container = vertical_container.add<Frame>();
  241. preview_container.set_layout<VerticalBoxLayout>();
  242. preview_container.layout()->set_margins(2);
  243. preview_container.layout()->set_spacing(0);
  244. preview_container.set_fixed_height(100);
  245. // Current color
  246. preview_container.add<ColorPreview>(m_color);
  247. // Preview selected color
  248. m_preview_widget = preview_container.add<ColorPreview>(m_color);
  249. vertical_container.layout()->add_spacer();
  250. // HTML
  251. auto& html_container = vertical_container.add<GUI::Widget>();
  252. html_container.set_layout<GUI::HorizontalBoxLayout>();
  253. html_container.set_fixed_height(22);
  254. auto& html_label = html_container.add<GUI::Label>();
  255. html_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  256. html_label.set_fixed_width(48);
  257. html_label.set_text("HTML:");
  258. m_html_text = html_container.add<GUI::TextBox>();
  259. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  260. m_html_text->on_change = [this]() {
  261. auto color_name = m_html_text->text();
  262. auto optional_color = Color::from_string(color_name);
  263. if (optional_color.has_value() && (!color_name.starts_with("#") || color_name.length() == ((m_color_has_alpha_channel) ? 9 : 7))) {
  264. // The color length must be 9/7 (unless it is a name like red), because:
  265. // - If we allowed 5/4 character rgb color, the field would reset to 9/7 characters after you deleted 4/3 characters.
  266. auto color = optional_color.value();
  267. if (m_color == color)
  268. return;
  269. m_color = optional_color.value();
  270. m_custom_color->set_color(color);
  271. update_color_widgets();
  272. }
  273. };
  274. // RGB Lines
  275. auto make_spinbox = [&](RGBComponent component, int initial_value) {
  276. auto& rgb_container = vertical_container.add<GUI::Widget>();
  277. rgb_container.set_layout<GUI::HorizontalBoxLayout>();
  278. rgb_container.set_fixed_height(22);
  279. auto& rgb_label = rgb_container.add<GUI::Label>();
  280. rgb_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  281. rgb_label.set_fixed_width(48);
  282. auto& spinbox = rgb_container.add<SpinBox>();
  283. spinbox.set_fixed_height(20);
  284. spinbox.set_min(0);
  285. spinbox.set_max(255);
  286. spinbox.set_value(initial_value);
  287. spinbox.set_enabled(m_color_has_alpha_channel);
  288. spinbox.on_change = [this, component](auto value) {
  289. auto color = m_color;
  290. if (component == Red)
  291. color.set_red(value);
  292. if (component == Green)
  293. color.set_green(value);
  294. if (component == Blue)
  295. color.set_blue(value);
  296. if (component == Alpha)
  297. color.set_alpha(value);
  298. if (m_color == color)
  299. return;
  300. m_color = color;
  301. m_custom_color->set_color(color);
  302. update_color_widgets();
  303. };
  304. if (component == Red) {
  305. rgb_label.set_text("Red:");
  306. m_red_spinbox = spinbox;
  307. } else if (component == Green) {
  308. rgb_label.set_text("Green:");
  309. m_green_spinbox = spinbox;
  310. } else if (component == Blue) {
  311. rgb_label.set_text("Blue:");
  312. m_blue_spinbox = spinbox;
  313. } else if (component == Alpha) {
  314. rgb_label.set_text("Alpha:");
  315. m_alpha_spinbox = spinbox;
  316. }
  317. };
  318. make_spinbox(Red, m_color.red());
  319. make_spinbox(Green, m_color.green());
  320. make_spinbox(Blue, m_color.blue());
  321. make_spinbox(Alpha, m_color.alpha());
  322. m_selector_button = vertical_container.add<GUI::Button>("Select on screen");
  323. m_selector_button->on_click = [this](auto) {
  324. auto selector = ColorSelectOverlay::construct();
  325. auto original_color = m_color;
  326. // This allows us to use the color preview widget as a live-preview for
  327. // the color currently under the cursor, which is helpful.
  328. selector->on_color_changed = [this](auto color) {
  329. m_color = color;
  330. update_color_widgets();
  331. };
  332. // Set the final color
  333. auto maybe_color = selector->exec();
  334. m_color = maybe_color.value_or(original_color);
  335. m_custom_color->set_color(m_color);
  336. update_color_widgets();
  337. };
  338. }
  339. void ColorPicker::update_color_widgets()
  340. {
  341. m_preview_widget->set_color(m_color);
  342. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  343. m_red_spinbox->set_value(m_color.red());
  344. m_green_spinbox->set_value(m_color.green());
  345. m_blue_spinbox->set_value(m_color.blue());
  346. m_alpha_spinbox->set_value(m_color.alpha());
  347. m_alpha_spinbox->set_enabled(m_color_has_alpha_channel);
  348. }
  349. void ColorPicker::create_color_button(Widget& container, unsigned rgb)
  350. {
  351. Color color = Color::from_rgb(rgb);
  352. auto& widget = container.add<ColorButton>(*this, color);
  353. widget.on_click = [this](Color color) {
  354. for (auto& value : m_color_widgets) {
  355. value.set_selected(false);
  356. value.update();
  357. }
  358. m_color = color;
  359. m_custom_color->set_color(color);
  360. update_color_widgets();
  361. };
  362. if (color == m_color) {
  363. widget.set_selected(true);
  364. }
  365. m_color_widgets.append(widget);
  366. }
  367. ColorButton::ColorButton(ColorPicker& picker, Color color)
  368. : m_picker(picker)
  369. {
  370. m_color = color;
  371. }
  372. ColorButton::~ColorButton()
  373. {
  374. }
  375. void ColorButton::set_selected(bool selected)
  376. {
  377. m_selected = selected;
  378. }
  379. void ColorButton::doubleclick_event(GUI::MouseEvent&)
  380. {
  381. click();
  382. m_selected = true;
  383. m_picker.done(Dialog::ExecOK);
  384. }
  385. void ColorButton::paint_event(PaintEvent& event)
  386. {
  387. Painter painter(*this);
  388. painter.add_clip_rect(event.rect());
  389. Gfx::StylePainter::paint_button(painter, rect(), palette(), Gfx::ButtonStyle::Normal, is_being_pressed(), is_hovered(), is_checked(), is_enabled(), is_focused());
  390. painter.fill_rect(rect().shrunken(2, 2), m_color);
  391. if (m_selected) {
  392. painter.fill_rect(rect().shrunken(6, 6), Color::Black);
  393. painter.fill_rect(rect().shrunken(10, 10), Color::White);
  394. painter.fill_rect(rect().shrunken(14, 14), m_color);
  395. }
  396. }
  397. void ColorButton::click(unsigned)
  398. {
  399. if (on_click)
  400. on_click(m_color);
  401. m_selected = true;
  402. }
  403. CustomColorWidget::CustomColorWidget(Color color)
  404. {
  405. set_layout<HorizontalBoxLayout>();
  406. m_color_field = add<ColorField>(color);
  407. auto size = 256 + (m_color_field->frame_thickness() * 2);
  408. m_color_field->set_fixed_size(size, size);
  409. m_color_field->on_pick = [this](Color color) {
  410. if (on_pick)
  411. on_pick(color);
  412. };
  413. m_color_slider = add<ColorSlider>(color.to_hsv().hue);
  414. auto slider_width = 24 + (m_color_slider->frame_thickness() * 2);
  415. m_color_slider->set_fixed_size(slider_width, size);
  416. m_color_slider->on_pick = [this](double value) {
  417. m_color_field->set_hue_from_pick(value);
  418. };
  419. }
  420. void CustomColorWidget::set_color(Color color)
  421. {
  422. m_color_field->set_color(color);
  423. m_color_field->set_hue(color.to_hsv().hue);
  424. }
  425. ColorField::ColorField(Color color)
  426. : m_color(color)
  427. , m_hue(color.to_hsv().hue)
  428. {
  429. create_color_bitmap();
  430. }
  431. void ColorField::create_color_bitmap()
  432. {
  433. m_color_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 256, 256 }).release_value_but_fixme_should_propagate_errors();
  434. auto painter = Gfx::Painter(*m_color_bitmap);
  435. Gfx::HSV hsv;
  436. hsv.hue = m_hue;
  437. for (int x = 0; x < 256; x++) {
  438. hsv.saturation = x / 255.0;
  439. for (int y = 0; y < 256; y++) {
  440. hsv.value = (255 - y) / 255.0;
  441. Color color = Color::from_hsv(hsv);
  442. painter.set_pixel({ x, y }, color);
  443. }
  444. }
  445. }
  446. void ColorField::set_color(Color color)
  447. {
  448. if (m_color == color)
  449. return;
  450. m_color = color;
  451. // don't save m_hue here by default, we don't want to set it to 0 in case color is full white
  452. // m_hue = color.to_hsv().hue;
  453. recalculate_position();
  454. }
  455. void ColorField::recalculate_position()
  456. {
  457. Gfx::HSV hsv = m_color.to_hsv();
  458. auto x = hsv.saturation * width();
  459. auto y = (1 - hsv.value) * height();
  460. m_last_position = Gfx::IntPoint(x, y);
  461. update();
  462. }
  463. void ColorField::set_hue(double hue)
  464. {
  465. if (m_hue == hue)
  466. return;
  467. auto hsv = m_color.to_hsv();
  468. hsv.hue = hue;
  469. m_hue = hue;
  470. create_color_bitmap();
  471. auto color = Color::from_hsv(hsv);
  472. color.set_alpha(m_color.alpha());
  473. set_color(color);
  474. }
  475. void ColorField::set_hue_from_pick(double hue)
  476. {
  477. set_hue(hue);
  478. if (on_pick)
  479. on_pick(m_color);
  480. }
  481. void ColorField::pick_color_at_position(GUI::MouseEvent& event)
  482. {
  483. if (!m_being_pressed)
  484. return;
  485. auto inner_rect = frame_inner_rect();
  486. auto position = event.position().constrained(inner_rect).translated(-frame_thickness(), -frame_thickness());
  487. auto color = Color::from_hsv(m_hue, (double)position.x() / inner_rect.width(), (double)(inner_rect.height() - position.y()) / inner_rect.height());
  488. color.set_alpha(m_color.alpha());
  489. m_last_position = position;
  490. m_color = color;
  491. if (on_pick)
  492. on_pick(color);
  493. update();
  494. }
  495. void ColorField::mousedown_event(GUI::MouseEvent& event)
  496. {
  497. if (event.button() == GUI::MouseButton::Primary) {
  498. m_being_pressed = true;
  499. pick_color_at_position(event);
  500. }
  501. }
  502. void ColorField::mouseup_event(GUI::MouseEvent& event)
  503. {
  504. if (event.button() == GUI::MouseButton::Primary) {
  505. m_being_pressed = false;
  506. pick_color_at_position(event);
  507. }
  508. }
  509. void ColorField::mousemove_event(GUI::MouseEvent& event)
  510. {
  511. if (event.buttons() & GUI::MouseButton::Primary)
  512. pick_color_at_position(event);
  513. }
  514. void ColorField::paint_event(GUI::PaintEvent& event)
  515. {
  516. Frame::paint_event(event);
  517. Painter painter(*this);
  518. painter.add_clip_rect(event.rect());
  519. painter.add_clip_rect(frame_inner_rect());
  520. painter.draw_scaled_bitmap(frame_inner_rect(), *m_color_bitmap, m_color_bitmap->rect());
  521. painter.translate(frame_thickness(), frame_thickness());
  522. painter.draw_line({ m_last_position.x() - 1, 0 }, { m_last_position.x() - 1, height() }, Color::White);
  523. painter.draw_line({ m_last_position.x() + 1, 0 }, { m_last_position.x() + 1, height() }, Color::White);
  524. painter.draw_line({ 0, m_last_position.y() - 1 }, { width(), m_last_position.y() - 1 }, Color::White);
  525. painter.draw_line({ 0, m_last_position.y() + 1 }, { width(), m_last_position.y() + 1 }, Color::White);
  526. painter.draw_line({ m_last_position.x(), 0 }, { m_last_position.x(), height() }, Color::Black);
  527. painter.draw_line({ 0, m_last_position.y() }, { width(), m_last_position.y() }, Color::Black);
  528. }
  529. void ColorField::resize_event(ResizeEvent&)
  530. {
  531. recalculate_position();
  532. }
  533. ColorSlider::ColorSlider(double value)
  534. : m_value(value)
  535. {
  536. m_color_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 32, 360 }).release_value_but_fixme_should_propagate_errors();
  537. auto painter = Gfx::Painter(*m_color_bitmap);
  538. for (int h = 0; h < 360; h++) {
  539. Gfx::HSV hsv;
  540. hsv.hue = h;
  541. hsv.saturation = 1.0;
  542. hsv.value = 1.0;
  543. Color color = Color::from_hsv(hsv);
  544. painter.draw_line({ 0, h }, { 32, h }, color);
  545. }
  546. }
  547. void ColorSlider::set_value(double value)
  548. {
  549. if (m_value == value)
  550. return;
  551. m_value = value;
  552. recalculate_position();
  553. }
  554. void ColorSlider::recalculate_position()
  555. {
  556. m_last_position = (m_value / 360.0) * height();
  557. update();
  558. }
  559. void ColorSlider::pick_value_at_position(GUI::MouseEvent& event)
  560. {
  561. if (!m_being_pressed)
  562. return;
  563. auto inner_rect = frame_inner_rect();
  564. auto position = event.position().constrained(inner_rect).translated(-frame_thickness(), -frame_thickness());
  565. auto hue = (double)position.y() / inner_rect.height() * 360;
  566. m_last_position = position.y();
  567. m_value = hue;
  568. if (on_pick)
  569. on_pick(m_value);
  570. update();
  571. }
  572. void ColorSlider::mousedown_event(GUI::MouseEvent& event)
  573. {
  574. if (event.button() == GUI::MouseButton::Primary) {
  575. m_being_pressed = true;
  576. pick_value_at_position(event);
  577. }
  578. }
  579. void ColorSlider::mouseup_event(GUI::MouseEvent& event)
  580. {
  581. if (event.button() == GUI::MouseButton::Primary) {
  582. m_being_pressed = false;
  583. pick_value_at_position(event);
  584. }
  585. }
  586. void ColorSlider::mousemove_event(GUI::MouseEvent& event)
  587. {
  588. if (event.buttons() & GUI::MouseButton::Primary)
  589. pick_value_at_position(event);
  590. }
  591. void ColorSlider::paint_event(GUI::PaintEvent& event)
  592. {
  593. Frame::paint_event(event);
  594. Painter painter(*this);
  595. painter.add_clip_rect(event.rect());
  596. painter.add_clip_rect(frame_inner_rect());
  597. painter.draw_scaled_bitmap(frame_inner_rect(), *m_color_bitmap, m_color_bitmap->rect());
  598. painter.translate(frame_thickness(), frame_thickness());
  599. painter.draw_line({ 0, m_last_position - 1 }, { width(), m_last_position - 1 }, Color::White);
  600. painter.draw_line({ 0, m_last_position + 1 }, { width(), m_last_position + 1 }, Color::White);
  601. painter.draw_line({ 0, m_last_position }, { width(), m_last_position }, Color::Black);
  602. }
  603. void ColorSlider::resize_event(ResizeEvent&)
  604. {
  605. recalculate_position();
  606. }
  607. ColorPreview::ColorPreview(Color color)
  608. : m_color(color)
  609. {
  610. }
  611. void ColorPreview::set_color(Color color)
  612. {
  613. if (m_color == color)
  614. return;
  615. m_color = color;
  616. update();
  617. }
  618. void ColorPreview::paint_event(PaintEvent& event)
  619. {
  620. Painter painter(*this);
  621. painter.add_clip_rect(event.rect());
  622. if (m_color.alpha() < 255) {
  623. Gfx::StylePainter::paint_transparency_grid(painter, rect(), palette());
  624. painter.fill_rect(rect(), m_color);
  625. painter.fill_rect({ 0, 0, rect().width() / 4, rect().height() }, m_color.with_alpha(255));
  626. } else {
  627. painter.fill_rect(rect(), m_color);
  628. }
  629. }
  630. }