ColorPicker.cpp 22 KB

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