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