ColorPicker.cpp 23 KB

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