ColorPicker.cpp 23 KB

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