ColorPicker.cpp 22 KB

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