ColorPicker.cpp 23 KB

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