ColorPicker.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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/SpinBox.h>
  31. #include <LibGUI/Widget.h>
  32. namespace GUI {
  33. ColorPicker::ColorPicker(Color color, Core::Object* parent)
  34. : Dialog(parent)
  35. , m_color(color)
  36. {
  37. set_title("Edit Color");
  38. build();
  39. }
  40. ColorPicker::~ColorPicker()
  41. {
  42. }
  43. void ColorPicker::build()
  44. {
  45. auto horizontal_container = Widget::construct();
  46. horizontal_container->set_fill_with_background_color(true);
  47. horizontal_container->set_layout(make<HorizontalBoxLayout>());
  48. horizontal_container->layout()->set_margins({ 4, 4, 4, 4 });
  49. set_main_widget(horizontal_container);
  50. auto left_vertical_container = Widget::construct(horizontal_container.ptr());
  51. left_vertical_container->set_layout(make<VerticalBoxLayout>());
  52. auto right_vertical_container = Widget::construct(horizontal_container.ptr());
  53. right_vertical_container->set_layout(make<VerticalBoxLayout>());
  54. enum RGBComponent {
  55. Red,
  56. Green,
  57. Blue
  58. };
  59. m_preview_widget = Frame::construct(right_vertical_container);
  60. auto pal = m_preview_widget->palette();
  61. pal.set_color(ColorRole::Background, m_color);
  62. m_preview_widget->set_fill_with_background_color(true);
  63. m_preview_widget->set_palette(pal);
  64. right_vertical_container->layout()->add_spacer();
  65. auto cancel_button = Button::construct("Cancel", right_vertical_container);
  66. cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  67. cancel_button->set_preferred_size(0, 20);
  68. cancel_button->on_click = [&](auto&) {
  69. done(Dialog::ExecCancel);
  70. };
  71. auto ok_button = Button::construct("Okay", right_vertical_container);
  72. ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  73. ok_button->set_preferred_size(0, 20);
  74. ok_button->on_click = [&](auto&) {
  75. done(Dialog::ExecOK);
  76. };
  77. auto make_spinbox = [&](RGBComponent component, int initial_value) {
  78. auto spinbox = SpinBox::construct(left_vertical_container);
  79. spinbox->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
  80. spinbox->set_preferred_size(0, 20);
  81. spinbox->set_min(0);
  82. spinbox->set_max(255);
  83. spinbox->set_value(initial_value);
  84. spinbox->on_change = [this, component](auto value) {
  85. if (component == Red)
  86. m_color.set_red(value);
  87. if (component == Green)
  88. m_color.set_green(value);
  89. if (component == Blue)
  90. m_color.set_blue(value);
  91. auto pal = m_preview_widget->palette();
  92. pal.set_color(ColorRole::Background, m_color);
  93. m_preview_widget->set_palette(pal);
  94. m_preview_widget->update();
  95. };
  96. return spinbox;
  97. };
  98. make_spinbox(Red, m_color.red());
  99. make_spinbox(Green, m_color.green());
  100. make_spinbox(Blue, m_color.blue());
  101. }
  102. }