ColorPicker.cpp 23 KB

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