ColorPicker.cpp 22 KB

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