ColorPicker.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibGUI/BoxLayout.h>
  27. #include <LibGUI/Button.h>
  28. #include <LibGUI/ColorPicker.h>
  29. #include <LibGUI/Frame.h>
  30. #include <LibGUI/Label.h>
  31. #include <LibGUI/Painter.h>
  32. #include <LibGUI/SpinBox.h>
  33. #include <LibGUI/TabWidget.h>
  34. #include <LibGUI/TextBox.h>
  35. #include <LibGfx/Palette.h>
  36. namespace GUI {
  37. class ColorButton : public AbstractButton {
  38. C_OBJECT(ColorButton);
  39. public:
  40. virtual ~ColorButton() override;
  41. void set_selected(bool selected);
  42. Color color() const { return m_color; }
  43. Function<void(const Color)> on_click;
  44. protected:
  45. virtual void click(unsigned modifiers = 0) override;
  46. virtual void doubleclick_event(GUI::MouseEvent&) override;
  47. virtual void paint_event(PaintEvent&) override;
  48. private:
  49. explicit ColorButton(ColorPicker& picker, Color color = {});
  50. ColorPicker& m_picker;
  51. Color m_color;
  52. bool m_selected { false };
  53. };
  54. class ColorField final : public GUI::Frame {
  55. C_OBJECT(ColorField);
  56. public:
  57. Function<void(Color)> on_pick;
  58. void set_color(Color);
  59. void set_hue(double);
  60. void set_hue_from_pick(double);
  61. private:
  62. ColorField(Color color);
  63. Color m_color;
  64. // save hue separately so full white color doesn't reset it to 0
  65. double m_hue;
  66. RefPtr<Gfx::Bitmap> m_color_bitmap;
  67. bool m_being_pressed { false };
  68. Gfx::IntPoint m_last_position;
  69. void create_color_bitmap();
  70. void pick_color_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 ColorSlider final : public GUI::Frame {
  79. C_OBJECT(ColorSlider);
  80. public:
  81. Function<void(double)> on_pick;
  82. void set_value(double);
  83. private:
  84. ColorSlider(double value);
  85. double m_value;
  86. RefPtr<Gfx::Bitmap> m_color_bitmap;
  87. bool m_being_pressed { false };
  88. int m_last_position;
  89. void pick_value_at_position(GUI::MouseEvent& event);
  90. void recalculate_position();
  91. virtual void mousedown_event(GUI::MouseEvent&) override;
  92. virtual void mouseup_event(GUI::MouseEvent&) override;
  93. virtual void mousemove_event(GUI::MouseEvent&) override;
  94. virtual void paint_event(GUI::PaintEvent&) override;
  95. virtual void resize_event(ResizeEvent&) override;
  96. };
  97. class ColorPreview final : public GUI::Widget {
  98. C_OBJECT(ColorPreview);
  99. public:
  100. void set_color(Color);
  101. private:
  102. ColorPreview(Color);
  103. Color m_color;
  104. virtual void paint_event(GUI::PaintEvent&) override;
  105. };
  106. class CustomColorWidget final : public GUI::Widget {
  107. C_OBJECT(CustomColorWidget);
  108. public:
  109. Function<void(Color)> on_pick;
  110. void set_color(Color);
  111. private:
  112. CustomColorWidget(Color);
  113. RefPtr<ColorField> m_color_field;
  114. RefPtr<ColorSlider> m_color_slider;
  115. };
  116. ColorPicker::ColorPicker(Color color, Window* parent_window, String title)
  117. : Dialog(parent_window)
  118. , m_color(color)
  119. {
  120. set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/color-chooser.png"));
  121. set_title(title);
  122. set_resizable(false);
  123. resize(458, 326);
  124. build_ui();
  125. }
  126. ColorPicker::~ColorPicker()
  127. {
  128. }
  129. void ColorPicker::set_color_has_alpha_channel(bool has_alpha)
  130. {
  131. if (m_color_has_alpha_channel == has_alpha)
  132. return;
  133. m_color_has_alpha_channel = has_alpha;
  134. update_color_widgets();
  135. }
  136. void ColorPicker::build_ui()
  137. {
  138. auto& root_container = set_main_widget<Widget>();
  139. root_container.set_layout<VerticalBoxLayout>();
  140. root_container.layout()->set_margins({ 4, 4, 4, 4 });
  141. root_container.set_fill_with_background_color(true);
  142. auto& tab_widget = root_container.add<GUI::TabWidget>();
  143. auto& tab_palette = tab_widget.add_tab<Widget>("Palette");
  144. tab_palette.set_layout<VerticalBoxLayout>();
  145. tab_palette.layout()->set_margins({ 4, 4, 4, 4 });
  146. tab_palette.layout()->set_spacing(4);
  147. build_ui_palette(tab_palette);
  148. auto& tab_custom_color = tab_widget.add_tab<Widget>("Custom Color");
  149. tab_custom_color.set_layout<VerticalBoxLayout>();
  150. tab_custom_color.layout()->set_margins({ 4, 4, 4, 4 });
  151. tab_custom_color.layout()->set_spacing(4);
  152. build_ui_custom(tab_custom_color);
  153. auto& button_container = root_container.add<Widget>();
  154. button_container.set_fixed_height(22);
  155. button_container.set_layout<HorizontalBoxLayout>();
  156. button_container.layout()->set_spacing(4);
  157. button_container.layout()->add_spacer();
  158. auto& ok_button = button_container.add<Button>();
  159. ok_button.set_fixed_width(80);
  160. ok_button.set_text("OK");
  161. ok_button.on_click = [this](auto) {
  162. done(ExecOK);
  163. };
  164. auto& cancel_button = button_container.add<Button>();
  165. cancel_button.set_fixed_width(80);
  166. cancel_button.set_text("Cancel");
  167. cancel_button.on_click = [this](auto) {
  168. done(ExecCancel);
  169. };
  170. }
  171. void ColorPicker::build_ui_palette(Widget& root_container)
  172. {
  173. unsigned colors[4][9] = {
  174. { 0xef2929, 0xf0b143, 0xfce94f, 0x9fe13a, 0x7c9ece, 0xa680a8, 0xe1ba70, 0x888a85, 0xeeeeec },
  175. { 0xba1e09, 0xf57900, 0xe9d51a, 0x8bd121, 0x4164a3, 0x6f517b, 0xb77f19, 0x555753, 0xd4d7cf },
  176. { 0x961605, 0xbf600c, 0xe9d51a, 0x619910, 0x2b4986, 0x573666, 0x875b09, 0x2f3436, 0xbbbdb6 },
  177. { 0x000000, 0x2f3436, 0x555753, 0x808080, 0xbabdb6, 0xd3d7cf, 0xeeeeec, 0xf3f3f3, 0xffffff }
  178. };
  179. for (int r = 0; r < 4; r++) {
  180. auto& colors_row = root_container.add<Widget>();
  181. colors_row.set_layout<HorizontalBoxLayout>();
  182. for (int i = 0; i < 8; i++) {
  183. create_color_button(colors_row, colors[r][i]);
  184. }
  185. }
  186. }
  187. void ColorPicker::build_ui_custom(Widget& root_container)
  188. {
  189. enum RGBComponent {
  190. Red,
  191. Green,
  192. Blue,
  193. Alpha
  194. };
  195. auto& horizontal_container = root_container.add<Widget>();
  196. horizontal_container.set_fill_with_background_color(true);
  197. horizontal_container.set_layout<HorizontalBoxLayout>();
  198. // Left Side
  199. m_custom_color = horizontal_container.add<CustomColorWidget>(m_color);
  200. m_custom_color->set_fixed_size(299, 260);
  201. m_custom_color->on_pick = [this](Color color) {
  202. if (m_color == color)
  203. return;
  204. m_color = color;
  205. update_color_widgets();
  206. };
  207. // Right Side
  208. auto& vertical_container = horizontal_container.add<Widget>();
  209. vertical_container.set_layout<VerticalBoxLayout>();
  210. vertical_container.layout()->set_margins({ 8, 0, 0, 0 });
  211. vertical_container.set_fixed_width(128);
  212. auto& preview_container = vertical_container.add<Frame>();
  213. preview_container.set_layout<VerticalBoxLayout>();
  214. preview_container.layout()->set_margins({ 2, 2, 2, 2 });
  215. preview_container.layout()->set_spacing(0);
  216. preview_container.set_fixed_height(128);
  217. // Current color
  218. preview_container.add<ColorPreview>(m_color);
  219. // Preview selected color
  220. m_preview_widget = preview_container.add<ColorPreview>(m_color);
  221. vertical_container.layout()->add_spacer();
  222. // HTML
  223. auto& html_container = vertical_container.add<GUI::Widget>();
  224. html_container.set_layout<GUI::HorizontalBoxLayout>();
  225. html_container.set_fixed_height(22);
  226. auto& html_label = html_container.add<GUI::Label>();
  227. html_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  228. html_label.set_fixed_width(48);
  229. html_label.set_text("HTML:");
  230. m_html_text = html_container.add<GUI::TextBox>();
  231. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  232. m_html_text->on_change = [this]() {
  233. auto color_name = m_html_text->text();
  234. auto optional_color = Color::from_string(color_name);
  235. if (optional_color.has_value() && (!color_name.starts_with("#") || color_name.length() == ((m_color_has_alpha_channel) ? 9 : 7))) {
  236. // The color length must be 9/7 (unless it is a name like red), because:
  237. // - If we allowed 5/4 character rgb color, the field would reset to 9/7 characters after you deleted 4/3 characters.
  238. auto color = optional_color.value();
  239. if (m_color == color)
  240. return;
  241. m_color = optional_color.value();
  242. m_custom_color->set_color(color);
  243. update_color_widgets();
  244. }
  245. };
  246. // RGB Lines
  247. auto make_spinbox = [&](RGBComponent component, int initial_value) {
  248. auto& rgb_container = vertical_container.add<GUI::Widget>();
  249. rgb_container.set_layout<GUI::HorizontalBoxLayout>();
  250. rgb_container.set_fixed_height(22);
  251. auto& rgb_label = rgb_container.add<GUI::Label>();
  252. rgb_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  253. rgb_label.set_fixed_width(48);
  254. auto& spinbox = rgb_container.add<SpinBox>();
  255. spinbox.set_fixed_height(20);
  256. spinbox.set_min(0);
  257. spinbox.set_max(255);
  258. spinbox.set_value(initial_value);
  259. spinbox.set_enabled(m_color_has_alpha_channel);
  260. spinbox.on_change = [this, component](auto value) {
  261. auto color = m_color;
  262. if (component == Red)
  263. color.set_red(value);
  264. if (component == Green)
  265. color.set_green(value);
  266. if (component == Blue)
  267. color.set_blue(value);
  268. if (component == Alpha)
  269. color.set_alpha(value);
  270. if (m_color == color)
  271. return;
  272. m_color = color;
  273. m_custom_color->set_color(color);
  274. update_color_widgets();
  275. };
  276. if (component == Red) {
  277. rgb_label.set_text("Red:");
  278. m_red_spinbox = spinbox;
  279. } else if (component == Green) {
  280. rgb_label.set_text("Green:");
  281. m_green_spinbox = spinbox;
  282. } else if (component == Blue) {
  283. rgb_label.set_text("Blue:");
  284. m_blue_spinbox = spinbox;
  285. } else if (component == Alpha) {
  286. rgb_label.set_text("Alpha:");
  287. m_alpha_spinbox = spinbox;
  288. }
  289. };
  290. make_spinbox(Red, m_color.red());
  291. make_spinbox(Green, m_color.green());
  292. make_spinbox(Blue, m_color.blue());
  293. make_spinbox(Alpha, m_color.alpha());
  294. }
  295. void ColorPicker::update_color_widgets()
  296. {
  297. m_preview_widget->set_color(m_color);
  298. m_html_text->set_text(m_color_has_alpha_channel ? m_color.to_string() : m_color.to_string_without_alpha());
  299. m_red_spinbox->set_value(m_color.red());
  300. m_green_spinbox->set_value(m_color.green());
  301. m_blue_spinbox->set_value(m_color.blue());
  302. m_alpha_spinbox->set_value(m_color.alpha());
  303. m_alpha_spinbox->set_enabled(m_color_has_alpha_channel);
  304. }
  305. void ColorPicker::create_color_button(Widget& container, unsigned rgb)
  306. {
  307. Color color = Color::from_rgb(rgb);
  308. auto& widget = container.add<ColorButton>(*this, color);
  309. widget.on_click = [this](Color color) {
  310. for (auto& value : m_color_widgets) {
  311. value->set_selected(false);
  312. value->update();
  313. }
  314. m_color = color;
  315. m_custom_color->set_color(color);
  316. update_color_widgets();
  317. };
  318. if (color == m_color) {
  319. widget.set_selected(true);
  320. }
  321. m_color_widgets.append(&widget);
  322. }
  323. ColorButton::ColorButton(ColorPicker& picker, Color color)
  324. : m_picker(picker)
  325. {
  326. m_color = color;
  327. }
  328. ColorButton::~ColorButton()
  329. {
  330. }
  331. void ColorButton::set_selected(bool selected)
  332. {
  333. m_selected = selected;
  334. }
  335. void ColorButton::doubleclick_event(GUI::MouseEvent&)
  336. {
  337. click();
  338. m_selected = true;
  339. m_picker.done(Dialog::ExecOK);
  340. }
  341. void ColorButton::paint_event(PaintEvent& event)
  342. {
  343. Painter painter(*this);
  344. painter.add_clip_rect(event.rect());
  345. Gfx::StylePainter::paint_button(painter, rect(), palette(), Gfx::ButtonStyle::Normal, is_being_pressed(), is_hovered(), is_checked(), is_enabled(), is_focused());
  346. painter.fill_rect(rect().shrunken(2, 2), m_color);
  347. if (m_selected) {
  348. painter.fill_rect(rect().shrunken(6, 6), Color::Black);
  349. painter.fill_rect(rect().shrunken(10, 10), Color::White);
  350. painter.fill_rect(rect().shrunken(14, 14), m_color);
  351. }
  352. }
  353. void ColorButton::click(unsigned)
  354. {
  355. if (on_click)
  356. on_click(m_color);
  357. m_selected = true;
  358. }
  359. CustomColorWidget::CustomColorWidget(Color color)
  360. {
  361. set_layout<HorizontalBoxLayout>();
  362. m_color_field = add<ColorField>(color);
  363. auto size = 256 + (m_color_field->frame_thickness() * 2);
  364. m_color_field->set_fixed_size(size, size);
  365. m_color_field->on_pick = [this](Color color) {
  366. if (on_pick)
  367. on_pick(color);
  368. };
  369. m_color_slider = add<ColorSlider>(color.to_hsv().hue);
  370. auto slider_width = 24 + (m_color_slider->frame_thickness() * 2);
  371. m_color_slider->set_fixed_size(slider_width, size);
  372. m_color_slider->on_pick = [this](double value) {
  373. m_color_field->set_hue_from_pick(value);
  374. };
  375. }
  376. void CustomColorWidget::set_color(Color color)
  377. {
  378. m_color_field->set_color(color);
  379. m_color_field->set_hue(color.to_hsv().hue);
  380. }
  381. ColorField::ColorField(Color color)
  382. : m_color(color)
  383. , m_hue(color.to_hsv().hue)
  384. {
  385. create_color_bitmap();
  386. }
  387. void ColorField::create_color_bitmap()
  388. {
  389. m_color_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 256, 256 });
  390. auto painter = Gfx::Painter(*m_color_bitmap);
  391. Gfx::HSV hsv;
  392. hsv.hue = m_hue;
  393. for (int x = 0; x < 256; x++) {
  394. hsv.saturation = x / 255.0f;
  395. for (int y = 0; y < 256; y++) {
  396. hsv.value = (255 - y) / 255.0f;
  397. Color color = Color::from_hsv(hsv);
  398. painter.set_pixel({ x, y }, color);
  399. }
  400. }
  401. }
  402. void ColorField::set_color(Color color)
  403. {
  404. if (m_color == color)
  405. return;
  406. m_color = color;
  407. // don't save m_hue here by default, we don't want to set it to 0 in case color is full white
  408. // m_hue = color.to_hsv().hue;
  409. recalculate_position();
  410. }
  411. void ColorField::recalculate_position()
  412. {
  413. Gfx::HSV hsv = m_color.to_hsv();
  414. auto x = hsv.saturation * width();
  415. auto y = (1 - hsv.value) * height();
  416. m_last_position = Gfx::IntPoint(x, y);
  417. update();
  418. }
  419. void ColorField::set_hue(double hue)
  420. {
  421. if (m_hue == hue)
  422. return;
  423. auto hsv = m_color.to_hsv();
  424. hsv.hue = hue;
  425. m_hue = hue;
  426. create_color_bitmap();
  427. auto color = Color::from_hsv(hsv);
  428. color.set_alpha(m_color.alpha());
  429. set_color(color);
  430. }
  431. void ColorField::set_hue_from_pick(double hue)
  432. {
  433. set_hue(hue);
  434. if (on_pick)
  435. on_pick(m_color);
  436. }
  437. void ColorField::pick_color_at_position(GUI::MouseEvent& event)
  438. {
  439. if (!m_being_pressed)
  440. return;
  441. auto inner_rect = frame_inner_rect();
  442. auto position = event.position().constrained(inner_rect).translated(-frame_thickness(), -frame_thickness());
  443. auto color = Color::from_hsv(m_hue, (double)position.x() / inner_rect.width(), (double)(inner_rect.height() - position.y()) / inner_rect.height());
  444. color.set_alpha(m_color.alpha());
  445. m_last_position = position;
  446. m_color = color;
  447. if (on_pick)
  448. on_pick(color);
  449. update();
  450. }
  451. void ColorField::mousedown_event(GUI::MouseEvent& event)
  452. {
  453. if (event.button() == GUI::MouseButton::Left) {
  454. m_being_pressed = true;
  455. pick_color_at_position(event);
  456. }
  457. }
  458. void ColorField::mouseup_event(GUI::MouseEvent& event)
  459. {
  460. if (event.button() == GUI::MouseButton::Left) {
  461. m_being_pressed = false;
  462. pick_color_at_position(event);
  463. }
  464. }
  465. void ColorField::mousemove_event(GUI::MouseEvent& event)
  466. {
  467. if (event.buttons() & GUI::MouseButton::Left)
  468. pick_color_at_position(event);
  469. }
  470. void ColorField::paint_event(GUI::PaintEvent& event)
  471. {
  472. Frame::paint_event(event);
  473. Painter painter(*this);
  474. painter.add_clip_rect(event.rect());
  475. painter.add_clip_rect(frame_inner_rect());
  476. painter.draw_scaled_bitmap(frame_inner_rect(), *m_color_bitmap, m_color_bitmap->rect());
  477. painter.translate(frame_thickness(), frame_thickness());
  478. painter.draw_line({ m_last_position.x() - 1, 0 }, { m_last_position.x() - 1, height() }, Color::White);
  479. painter.draw_line({ m_last_position.x() + 1, 0 }, { m_last_position.x() + 1, height() }, Color::White);
  480. painter.draw_line({ 0, m_last_position.y() - 1 }, { width(), m_last_position.y() - 1 }, Color::White);
  481. painter.draw_line({ 0, m_last_position.y() + 1 }, { width(), m_last_position.y() + 1 }, Color::White);
  482. painter.draw_line({ m_last_position.x(), 0 }, { m_last_position.x(), height() }, Color::Black);
  483. painter.draw_line({ 0, m_last_position.y() }, { width(), m_last_position.y() }, Color::Black);
  484. }
  485. void ColorField::resize_event(ResizeEvent&)
  486. {
  487. recalculate_position();
  488. }
  489. ColorSlider::ColorSlider(double value)
  490. : m_value(value)
  491. {
  492. m_color_bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::RGB32, { 32, 360 });
  493. auto painter = Gfx::Painter(*m_color_bitmap);
  494. for (int h = 0; h < 360; h++) {
  495. Gfx::HSV hsv;
  496. hsv.hue = h;
  497. hsv.saturation = 1.0;
  498. hsv.value = 1.0;
  499. Color color = Color::from_hsv(hsv);
  500. painter.draw_line({ 0, h }, { 32, h }, color);
  501. }
  502. }
  503. void ColorSlider::set_value(double value)
  504. {
  505. if (m_value == value)
  506. return;
  507. m_value = value;
  508. recalculate_position();
  509. }
  510. void ColorSlider::recalculate_position()
  511. {
  512. m_last_position = (m_value / 360.0) * height();
  513. update();
  514. }
  515. void ColorSlider::pick_value_at_position(GUI::MouseEvent& event)
  516. {
  517. if (!m_being_pressed)
  518. return;
  519. auto inner_rect = frame_inner_rect();
  520. auto position = event.position().constrained(inner_rect).translated(-frame_thickness(), -frame_thickness());
  521. auto hue = (double)position.y() / inner_rect.height() * 360;
  522. m_last_position = position.y();
  523. m_value = hue;
  524. if (on_pick)
  525. on_pick(m_value);
  526. update();
  527. }
  528. void ColorSlider::mousedown_event(GUI::MouseEvent& event)
  529. {
  530. if (event.button() == GUI::MouseButton::Left) {
  531. m_being_pressed = true;
  532. pick_value_at_position(event);
  533. }
  534. }
  535. void ColorSlider::mouseup_event(GUI::MouseEvent& event)
  536. {
  537. if (event.button() == GUI::MouseButton::Left) {
  538. m_being_pressed = false;
  539. pick_value_at_position(event);
  540. }
  541. }
  542. void ColorSlider::mousemove_event(GUI::MouseEvent& event)
  543. {
  544. if (event.buttons() & GUI::MouseButton::Left)
  545. pick_value_at_position(event);
  546. }
  547. void ColorSlider::paint_event(GUI::PaintEvent& event)
  548. {
  549. Frame::paint_event(event);
  550. Painter painter(*this);
  551. painter.add_clip_rect(event.rect());
  552. painter.add_clip_rect(frame_inner_rect());
  553. painter.draw_scaled_bitmap(frame_inner_rect(), *m_color_bitmap, m_color_bitmap->rect());
  554. painter.translate(frame_thickness(), frame_thickness());
  555. painter.draw_line({ 0, m_last_position - 1 }, { width(), m_last_position - 1 }, Color::White);
  556. painter.draw_line({ 0, m_last_position + 1 }, { width(), m_last_position + 1 }, Color::White);
  557. painter.draw_line({ 0, m_last_position }, { width(), m_last_position }, Color::Black);
  558. }
  559. void ColorSlider::resize_event(ResizeEvent&)
  560. {
  561. recalculate_position();
  562. }
  563. ColorPreview::ColorPreview(Color color)
  564. : m_color(color)
  565. {
  566. }
  567. void ColorPreview::set_color(Color color)
  568. {
  569. if (m_color == color)
  570. return;
  571. m_color = color;
  572. update();
  573. }
  574. void ColorPreview::paint_event(PaintEvent& event)
  575. {
  576. Painter painter(*this);
  577. painter.add_clip_rect(event.rect());
  578. if (m_color.alpha() < 255) {
  579. Gfx::StylePainter::paint_transparency_grid(painter, rect(), palette());
  580. painter.fill_rect(rect(), m_color);
  581. painter.fill_rect({ 0, 0, rect().width() / 4, rect().height() }, m_color.with_alpha(255));
  582. } else {
  583. painter.fill_rect(rect(), m_color);
  584. }
  585. }
  586. }