ColorPicker.cpp 20 KB

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