ColorPicker.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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<DialogButton>();
  185. ok_button.set_text("OK");
  186. ok_button.on_click = [this](auto) {
  187. done(ExecResult::OK);
  188. };
  189. ok_button.set_default(true);
  190. auto& cancel_button = button_container.add<DialogButton>();
  191. cancel_button.set_text("Cancel");
  192. cancel_button.on_click = [this](auto) {
  193. done(ExecResult::Cancel);
  194. };
  195. }
  196. void ColorPicker::build_ui_palette(Widget& root_container)
  197. {
  198. unsigned colors[4][9] = {
  199. { 0xef2929, 0xf0b143, 0xfce94f, 0x9fe13a, 0x7c9ece, 0xa680a8, 0xe1ba70, 0x888a85, 0xeeeeec },
  200. { 0xba1e09, 0xf57900, 0xe9d51a, 0x8bd121, 0x4164a3, 0x6f517b, 0xb77f19, 0x555753, 0xd4d7cf },
  201. { 0x961605, 0xbf600c, 0xe9d51a, 0x619910, 0x2b4986, 0x573666, 0x875b09, 0x2f3436, 0xbbbdb6 },
  202. { 0x000000, 0x2f3436, 0x555753, 0x808080, 0xbabdb6, 0xd3d7cf, 0xeeeeec, 0xf3f3f3, 0xffffff }
  203. };
  204. for (int r = 0; r < 4; r++) {
  205. auto& colors_row = root_container.add<Widget>();
  206. colors_row.set_layout<HorizontalBoxLayout>();
  207. for (int i = 0; i < 8; i++) {
  208. create_color_button(colors_row, colors[r][i]);
  209. }
  210. }
  211. }
  212. void ColorPicker::build_ui_custom(Widget& root_container)
  213. {
  214. enum RGBComponent {
  215. Red,
  216. Green,
  217. Blue,
  218. Alpha
  219. };
  220. auto& horizontal_container = root_container.add<Widget>();
  221. horizontal_container.set_fill_with_background_color(true);
  222. horizontal_container.set_layout<HorizontalBoxLayout>();
  223. // Left Side
  224. m_custom_color = horizontal_container.add<CustomColorWidget>(m_color);
  225. m_custom_color->set_fixed_size(299, 260);
  226. m_custom_color->on_pick = [this](Color color) {
  227. if (m_color == color)
  228. return;
  229. m_color = color;
  230. update_color_widgets();
  231. };
  232. // Right Side
  233. auto& vertical_container = horizontal_container.add<Widget>();
  234. vertical_container.set_layout<VerticalBoxLayout>();
  235. vertical_container.layout()->set_margins({ 0, 0, 0, 8 });
  236. vertical_container.set_fixed_width(128);
  237. auto& preview_container = vertical_container.add<Frame>();
  238. preview_container.set_layout<VerticalBoxLayout>();
  239. preview_container.layout()->set_margins(2);
  240. preview_container.layout()->set_spacing(0);
  241. preview_container.set_fixed_height(100);
  242. // Current color
  243. preview_container.add<ColorPreview>(m_color);
  244. // Preview selected color
  245. m_preview_widget = preview_container.add<ColorPreview>(m_color);
  246. vertical_container.layout()->add_spacer();
  247. // HTML
  248. auto& html_container = vertical_container.add<GUI::Widget>();
  249. html_container.set_layout<GUI::HorizontalBoxLayout>();
  250. html_container.set_fixed_height(22);
  251. auto& html_label = html_container.add<GUI::Label>();
  252. html_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  253. html_label.set_fixed_width(48);
  254. html_label.set_text("HTML:");
  255. m_html_text = html_container.add<GUI::TextBox>();
  256. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  257. m_html_text->on_change = [this]() {
  258. auto color_name = m_html_text->text();
  259. auto optional_color = Color::from_string(color_name);
  260. if (optional_color.has_value() && (!color_name.starts_with("#") || color_name.length() == ((m_color_has_alpha_channel) ? 9 : 7))) {
  261. // The color length must be 9/7 (unless it is a name like red), because:
  262. // - If we allowed 5/4 character rgb color, the field would reset to 9/7 characters after you deleted 4/3 characters.
  263. auto color = optional_color.value();
  264. if (m_color == color)
  265. return;
  266. m_color = optional_color.value();
  267. m_custom_color->set_color(color);
  268. update_color_widgets();
  269. }
  270. };
  271. // RGB Lines
  272. auto make_spinbox = [&](RGBComponent component, int initial_value) {
  273. auto& rgb_container = vertical_container.add<GUI::Widget>();
  274. rgb_container.set_layout<GUI::HorizontalBoxLayout>();
  275. rgb_container.set_fixed_height(22);
  276. auto& rgb_label = rgb_container.add<GUI::Label>();
  277. rgb_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  278. rgb_label.set_fixed_width(48);
  279. auto& spinbox = rgb_container.add<SpinBox>();
  280. spinbox.set_fixed_height(20);
  281. spinbox.set_min(0);
  282. spinbox.set_max(255);
  283. spinbox.set_value(initial_value);
  284. spinbox.set_enabled(m_color_has_alpha_channel);
  285. spinbox.on_change = [this, component](auto value) {
  286. auto color = m_color;
  287. if (component == Red)
  288. color.set_red(value);
  289. if (component == Green)
  290. color.set_green(value);
  291. if (component == Blue)
  292. color.set_blue(value);
  293. if (component == Alpha)
  294. color.set_alpha(value);
  295. if (m_color == color)
  296. return;
  297. m_color = color;
  298. m_custom_color->set_color(color);
  299. update_color_widgets();
  300. };
  301. if (component == Red) {
  302. rgb_label.set_text("Red:");
  303. m_red_spinbox = spinbox;
  304. } else if (component == Green) {
  305. rgb_label.set_text("Green:");
  306. m_green_spinbox = spinbox;
  307. } else if (component == Blue) {
  308. rgb_label.set_text("Blue:");
  309. m_blue_spinbox = spinbox;
  310. } else if (component == Alpha) {
  311. rgb_label.set_text("Alpha:");
  312. m_alpha_spinbox = spinbox;
  313. }
  314. };
  315. make_spinbox(Red, m_color.red());
  316. make_spinbox(Green, m_color.green());
  317. make_spinbox(Blue, m_color.blue());
  318. make_spinbox(Alpha, m_color.alpha());
  319. m_selector_button = vertical_container.add<GUI::Button>("Select on screen");
  320. m_selector_button->on_click = [this](auto) {
  321. auto selector = ColorSelectOverlay::construct();
  322. auto original_color = m_color;
  323. // This allows us to use the color preview widget as a live-preview for
  324. // the color currently under the cursor, which is helpful.
  325. selector->on_color_changed = [this](auto color) {
  326. m_color = color;
  327. update_color_widgets();
  328. };
  329. // Set the final color
  330. auto maybe_color = selector->exec();
  331. m_color = maybe_color.value_or(original_color);
  332. m_custom_color->set_color(m_color);
  333. update_color_widgets();
  334. };
  335. }
  336. void ColorPicker::update_color_widgets()
  337. {
  338. m_preview_widget->set_color(m_color);
  339. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  340. m_red_spinbox->set_value(m_color.red());
  341. m_green_spinbox->set_value(m_color.green());
  342. m_blue_spinbox->set_value(m_color.blue());
  343. m_alpha_spinbox->set_value(m_color.alpha());
  344. m_alpha_spinbox->set_enabled(m_color_has_alpha_channel);
  345. }
  346. void ColorPicker::create_color_button(Widget& container, unsigned rgb)
  347. {
  348. Color color = Color::from_rgb(rgb);
  349. auto& widget = container.add<ColorButton>(*this, color);
  350. widget.on_click = [this](Color color) {
  351. for (auto& value : m_color_widgets) {
  352. value.set_selected(false);
  353. value.update();
  354. }
  355. m_color = color;
  356. m_custom_color->set_color(color);
  357. update_color_widgets();
  358. };
  359. if (color == m_color) {
  360. widget.set_selected(true);
  361. }
  362. m_color_widgets.append(widget);
  363. }
  364. ColorButton::ColorButton(ColorPicker& picker, Color color)
  365. : m_picker(picker)
  366. {
  367. m_color = color;
  368. }
  369. void ColorButton::set_selected(bool selected)
  370. {
  371. m_selected = selected;
  372. }
  373. void ColorButton::doubleclick_event(GUI::MouseEvent&)
  374. {
  375. click();
  376. m_selected = true;
  377. m_picker.done(Dialog::ExecResult::OK);
  378. }
  379. void ColorButton::paint_event(PaintEvent& event)
  380. {
  381. Painter painter(*this);
  382. painter.add_clip_rect(event.rect());
  383. Gfx::StylePainter::paint_button(painter, rect(), palette(), Gfx::ButtonStyle::Normal, is_being_pressed(), is_hovered(), is_checked(), is_enabled(), is_focused());
  384. painter.fill_rect(rect().shrunken(2, 2), m_color);
  385. if (m_selected) {
  386. painter.fill_rect(rect().shrunken(6, 6), Color::Black);
  387. painter.fill_rect(rect().shrunken(10, 10), Color::White);
  388. painter.fill_rect(rect().shrunken(14, 14), m_color);
  389. }
  390. }
  391. void ColorButton::click(unsigned)
  392. {
  393. if (on_click)
  394. on_click(m_color);
  395. m_selected = true;
  396. }
  397. CustomColorWidget::CustomColorWidget(Color color)
  398. {
  399. set_layout<HorizontalBoxLayout>();
  400. m_color_field = add<ColorField>(color);
  401. auto size = 256 + (m_color_field->frame_thickness() * 2);
  402. m_color_field->set_fixed_size(size, size);
  403. m_color_field->on_pick = [this](Color color) {
  404. if (on_pick)
  405. on_pick(color);
  406. };
  407. m_color_slider = add<ColorSlider>(color.to_hsv().hue);
  408. auto slider_width = 24 + (m_color_slider->frame_thickness() * 2);
  409. m_color_slider->set_fixed_size(slider_width, size);
  410. m_color_slider->on_pick = [this](double value) {
  411. m_color_field->set_hue_from_pick(value);
  412. };
  413. }
  414. void CustomColorWidget::set_color(Color color)
  415. {
  416. m_color_field->set_color(color);
  417. m_color_field->set_hue(color.to_hsv().hue);
  418. }
  419. ColorField::ColorField(Color color)
  420. : m_color(color)
  421. , m_hue(color.to_hsv().hue)
  422. {
  423. create_color_bitmap();
  424. }
  425. void ColorField::create_color_bitmap()
  426. {
  427. m_color_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 256, 256 }).release_value_but_fixme_should_propagate_errors();
  428. auto painter = Gfx::Painter(*m_color_bitmap);
  429. Gfx::HSV hsv;
  430. hsv.hue = m_hue;
  431. for (int x = 0; x < 256; x++) {
  432. hsv.saturation = x / 255.0;
  433. for (int y = 0; y < 256; y++) {
  434. hsv.value = (255 - y) / 255.0;
  435. Color color = Color::from_hsv(hsv);
  436. painter.set_pixel({ x, y }, color);
  437. }
  438. }
  439. }
  440. void ColorField::set_color(Color color)
  441. {
  442. if (m_color == color)
  443. return;
  444. m_color = color;
  445. // don't save m_hue here by default, we don't want to set it to 0 in case color is full white
  446. // m_hue = color.to_hsv().hue;
  447. recalculate_position();
  448. }
  449. void ColorField::recalculate_position()
  450. {
  451. Gfx::HSV hsv = m_color.to_hsv();
  452. auto x = hsv.saturation * width();
  453. auto y = (1 - hsv.value) * height();
  454. m_last_position = Gfx::IntPoint(x, y);
  455. update();
  456. }
  457. void ColorField::set_hue(double hue)
  458. {
  459. if (m_hue == hue)
  460. return;
  461. auto hsv = m_color.to_hsv();
  462. hsv.hue = hue;
  463. m_hue = hue;
  464. create_color_bitmap();
  465. auto color = Color::from_hsv(hsv);
  466. color.set_alpha(m_color.alpha());
  467. set_color(color);
  468. }
  469. void ColorField::set_hue_from_pick(double hue)
  470. {
  471. set_hue(hue);
  472. if (on_pick)
  473. on_pick(m_color);
  474. }
  475. void ColorField::pick_color_at_position(GUI::MouseEvent& event)
  476. {
  477. if (!m_being_pressed)
  478. return;
  479. auto inner_rect = frame_inner_rect();
  480. auto position = event.position().constrained(inner_rect).translated(-frame_thickness(), -frame_thickness());
  481. auto color = Color::from_hsv(m_hue, (double)position.x() / inner_rect.width(), (double)(inner_rect.height() - position.y()) / inner_rect.height());
  482. color.set_alpha(m_color.alpha());
  483. m_last_position = position;
  484. m_color = color;
  485. if (on_pick)
  486. on_pick(color);
  487. update();
  488. }
  489. void ColorField::mousedown_event(GUI::MouseEvent& event)
  490. {
  491. if (event.button() == GUI::MouseButton::Primary) {
  492. m_being_pressed = true;
  493. pick_color_at_position(event);
  494. }
  495. }
  496. void ColorField::mouseup_event(GUI::MouseEvent& event)
  497. {
  498. if (event.button() == GUI::MouseButton::Primary) {
  499. m_being_pressed = false;
  500. pick_color_at_position(event);
  501. }
  502. }
  503. void ColorField::mousemove_event(GUI::MouseEvent& event)
  504. {
  505. if (event.buttons() & GUI::MouseButton::Primary)
  506. pick_color_at_position(event);
  507. }
  508. void ColorField::paint_event(GUI::PaintEvent& event)
  509. {
  510. Frame::paint_event(event);
  511. Painter painter(*this);
  512. painter.add_clip_rect(event.rect());
  513. painter.add_clip_rect(frame_inner_rect());
  514. painter.draw_scaled_bitmap(frame_inner_rect(), *m_color_bitmap, m_color_bitmap->rect());
  515. painter.translate(frame_thickness(), frame_thickness());
  516. painter.draw_line({ m_last_position.x() - 1, 0 }, { m_last_position.x() - 1, height() }, Color::White);
  517. painter.draw_line({ m_last_position.x() + 1, 0 }, { m_last_position.x() + 1, height() }, Color::White);
  518. painter.draw_line({ 0, m_last_position.y() - 1 }, { width(), m_last_position.y() - 1 }, Color::White);
  519. painter.draw_line({ 0, m_last_position.y() + 1 }, { width(), m_last_position.y() + 1 }, Color::White);
  520. painter.draw_line({ m_last_position.x(), 0 }, { m_last_position.x(), height() }, Color::Black);
  521. painter.draw_line({ 0, m_last_position.y() }, { width(), m_last_position.y() }, Color::Black);
  522. }
  523. void ColorField::resize_event(ResizeEvent&)
  524. {
  525. recalculate_position();
  526. }
  527. ColorSlider::ColorSlider(double value)
  528. : m_value(value)
  529. {
  530. m_color_bitmap = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, { 32, 360 }).release_value_but_fixme_should_propagate_errors();
  531. auto painter = Gfx::Painter(*m_color_bitmap);
  532. for (int h = 0; h < 360; h++) {
  533. Gfx::HSV hsv;
  534. hsv.hue = h;
  535. hsv.saturation = 1.0;
  536. hsv.value = 1.0;
  537. Color color = Color::from_hsv(hsv);
  538. painter.draw_line({ 0, h }, { 32, h }, color);
  539. }
  540. }
  541. void ColorSlider::set_value(double value)
  542. {
  543. if (m_value == value)
  544. return;
  545. m_value = value;
  546. recalculate_position();
  547. }
  548. void ColorSlider::recalculate_position()
  549. {
  550. m_last_position = (m_value / 360.0) * height();
  551. update();
  552. }
  553. void ColorSlider::pick_value_at_position(GUI::MouseEvent& event)
  554. {
  555. if (!m_being_pressed)
  556. return;
  557. auto inner_rect = frame_inner_rect();
  558. auto position = event.position().constrained(inner_rect).translated(-frame_thickness(), -frame_thickness());
  559. auto hue = (double)position.y() / inner_rect.height() * 360;
  560. m_last_position = position.y();
  561. m_value = hue;
  562. if (on_pick)
  563. on_pick(m_value);
  564. update();
  565. }
  566. void ColorSlider::mousedown_event(GUI::MouseEvent& event)
  567. {
  568. if (event.button() == GUI::MouseButton::Primary) {
  569. m_being_pressed = true;
  570. pick_value_at_position(event);
  571. }
  572. }
  573. void ColorSlider::mouseup_event(GUI::MouseEvent& event)
  574. {
  575. if (event.button() == GUI::MouseButton::Primary) {
  576. m_being_pressed = false;
  577. pick_value_at_position(event);
  578. }
  579. }
  580. void ColorSlider::mousemove_event(GUI::MouseEvent& event)
  581. {
  582. if (event.buttons() & GUI::MouseButton::Primary)
  583. pick_value_at_position(event);
  584. }
  585. void ColorSlider::paint_event(GUI::PaintEvent& event)
  586. {
  587. Frame::paint_event(event);
  588. Painter painter(*this);
  589. painter.add_clip_rect(event.rect());
  590. painter.add_clip_rect(frame_inner_rect());
  591. painter.draw_scaled_bitmap(frame_inner_rect(), *m_color_bitmap, m_color_bitmap->rect());
  592. painter.translate(frame_thickness(), frame_thickness());
  593. painter.draw_line({ 0, m_last_position - 1 }, { width(), m_last_position - 1 }, Color::White);
  594. painter.draw_line({ 0, m_last_position + 1 }, { width(), m_last_position + 1 }, Color::White);
  595. painter.draw_line({ 0, m_last_position }, { width(), m_last_position }, Color::Black);
  596. }
  597. void ColorSlider::resize_event(ResizeEvent&)
  598. {
  599. recalculate_position();
  600. }
  601. ColorPreview::ColorPreview(Color color)
  602. : m_color(color)
  603. {
  604. }
  605. void ColorPreview::set_color(Color color)
  606. {
  607. if (m_color == color)
  608. return;
  609. m_color = color;
  610. update();
  611. }
  612. void ColorPreview::paint_event(PaintEvent& event)
  613. {
  614. Painter painter(*this);
  615. painter.add_clip_rect(event.rect());
  616. if (m_color.alpha() < 255) {
  617. Gfx::StylePainter::paint_transparency_grid(painter, rect(), palette());
  618. painter.fill_rect(rect(), m_color);
  619. painter.fill_rect({ 0, 0, rect().width() / 4, rect().height() }, m_color.with_alpha(255));
  620. } else {
  621. painter.fill_rect(rect(), m_color);
  622. }
  623. }
  624. }