ColorPicker.cpp 22 KB

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