ColorPicker.cpp 4.3 KB

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