PaletteWidget.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Felix Rauch <noreply@felixrau.ch>
  4. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  5. * Copyright (c) 2022, the SerenityOS developers.
  6. *
  7. * SPDX-License-Identifier: BSD-2-Clause
  8. */
  9. #include "PaletteWidget.h"
  10. #include "ImageEditor.h"
  11. #include <AK/Result.h>
  12. #include <AK/Vector.h>
  13. #include <LibCore/File.h>
  14. #include <LibGUI/BoxLayout.h>
  15. #include <LibGUI/ColorPicker.h>
  16. #include <LibGUI/MessageBox.h>
  17. #include <LibGfx/Palette.h>
  18. #include <string.h>
  19. REGISTER_WIDGET(PixelPaint, PaletteWidget);
  20. namespace PixelPaint {
  21. class ColorWidget : public GUI::Frame {
  22. C_OBJECT(ColorWidget);
  23. public:
  24. virtual ~ColorWidget() override = default;
  25. virtual Color color() { return m_color; }
  26. virtual void mousedown_event(GUI::MouseEvent& event) override
  27. {
  28. if (event.modifiers() & KeyModifier::Mod_Ctrl) {
  29. auto dialog = GUI::ColorPicker::construct(m_color, window());
  30. if (dialog->exec() == GUI::Dialog::ExecResult::OK) {
  31. m_color = dialog->color();
  32. auto pal = palette();
  33. pal.set_color(ColorRole::Background, m_color);
  34. set_palette(pal);
  35. update();
  36. }
  37. }
  38. if (event.button() == GUI::MouseButton::Primary)
  39. m_palette_widget.set_primary_color(m_color);
  40. else if (event.button() == GUI::MouseButton::Secondary)
  41. m_palette_widget.set_secondary_color(m_color);
  42. }
  43. private:
  44. explicit ColorWidget(Color color, PaletteWidget& palette_widget)
  45. : m_palette_widget(palette_widget)
  46. , m_color(color)
  47. {
  48. set_fixed_width(16);
  49. }
  50. PaletteWidget& m_palette_widget;
  51. Color m_color;
  52. };
  53. class SelectedColorWidget : public GUI::Frame {
  54. C_OBJECT(SelectedColorWidget);
  55. public:
  56. virtual ~SelectedColorWidget() override = default;
  57. virtual void mousedown_event(GUI::MouseEvent& event) override
  58. {
  59. if (event.button() != GUI::MouseButton::Primary || !on_color_change)
  60. return;
  61. auto dialog = GUI::ColorPicker::construct(m_color, window());
  62. if (dialog->exec() == GUI::Dialog::ExecResult::OK)
  63. on_color_change(dialog->color());
  64. }
  65. void set_background_color(Color const& color)
  66. {
  67. auto pal = palette();
  68. pal.set_color(ColorRole::Background, color);
  69. set_palette(pal);
  70. update();
  71. m_color = color;
  72. }
  73. Function<void(Color const&)> on_color_change;
  74. Color m_color = Color::White;
  75. private:
  76. SelectedColorWidget() = default;
  77. };
  78. PaletteWidget::PaletteWidget()
  79. {
  80. set_frame_shape(Gfx::FrameShape::Panel);
  81. set_frame_shadow(Gfx::FrameShadow::Raised);
  82. set_frame_thickness(0);
  83. set_fill_with_background_color(true);
  84. set_fixed_height(35);
  85. m_secondary_color_widget = add<SelectedColorWidget>();
  86. m_secondary_color_widget->on_color_change = [&](auto& color) {
  87. set_secondary_color(color);
  88. };
  89. m_secondary_color_widget->set_relative_rect({ 0, 2, 60, 33 });
  90. m_secondary_color_widget->set_fill_with_background_color(true);
  91. m_primary_color_widget = add<SelectedColorWidget>();
  92. m_primary_color_widget->on_color_change = [&](auto& color) {
  93. set_primary_color(color);
  94. };
  95. auto rect = Gfx::IntRect(0, 0, 35, 17).centered_within(m_secondary_color_widget->relative_rect());
  96. m_primary_color_widget->set_relative_rect(rect);
  97. m_primary_color_widget->set_fill_with_background_color(true);
  98. m_color_container = add<GUI::Widget>();
  99. m_color_container->set_relative_rect(m_secondary_color_widget->relative_rect().right() + 2, 2, 500, 33);
  100. m_color_container->set_layout<GUI::VerticalBoxLayout>();
  101. m_color_container->layout()->set_spacing(1);
  102. auto& top_color_container = m_color_container->add<GUI::Widget>();
  103. top_color_container.set_name("top_color_container");
  104. top_color_container.set_layout<GUI::HorizontalBoxLayout>();
  105. top_color_container.layout()->set_spacing(1);
  106. auto& bottom_color_container = m_color_container->add<GUI::Widget>();
  107. bottom_color_container.set_name("bottom_color_container");
  108. bottom_color_container.set_layout<GUI::HorizontalBoxLayout>();
  109. bottom_color_container.layout()->set_spacing(1);
  110. auto result = load_palette_path("/res/color-palettes/default.palette");
  111. if (result.is_error()) {
  112. GUI::MessageBox::show_error(window(), String::formatted("Loading default palette failed: {}", result.error()));
  113. display_color_list(fallback_colors());
  114. return;
  115. }
  116. display_color_list(result.value());
  117. }
  118. void PaletteWidget::set_image_editor(ImageEditor* editor)
  119. {
  120. m_editor = editor;
  121. if (!m_editor)
  122. return;
  123. set_primary_color(editor->primary_color());
  124. set_secondary_color(editor->secondary_color());
  125. editor->on_primary_color_change = [this](Color color) {
  126. set_primary_color(color);
  127. };
  128. editor->on_secondary_color_change = [this](Color color) {
  129. set_secondary_color(color);
  130. };
  131. }
  132. void PaletteWidget::set_primary_color(Color color)
  133. {
  134. if (m_editor)
  135. m_editor->set_primary_color(color);
  136. m_primary_color_widget->set_background_color(color);
  137. }
  138. void PaletteWidget::set_secondary_color(Color color)
  139. {
  140. if (m_editor)
  141. m_editor->set_secondary_color(color);
  142. m_secondary_color_widget->set_background_color(color);
  143. }
  144. void PaletteWidget::display_color_list(Vector<Color> const& colors)
  145. {
  146. int colors_to_add = colors.size();
  147. if (colors_to_add == 0) {
  148. dbgln("Empty color list given. Using fallback colors.");
  149. display_color_list(fallback_colors());
  150. return;
  151. }
  152. auto& top_color_container = *m_color_container->find_descendant_of_type_named<GUI::Widget>("top_color_container");
  153. top_color_container.remove_all_children();
  154. auto& bottom_color_container = *m_color_container->find_descendant_of_type_named<GUI::Widget>("bottom_color_container");
  155. bottom_color_container.remove_all_children();
  156. auto add_color_widget = [&](GUI::Widget& container, Color color) {
  157. auto& color_widget = container.add<ColorWidget>(color, *this);
  158. color_widget.set_fill_with_background_color(true);
  159. color_widget.set_fixed_size(16, 16);
  160. auto pal = color_widget.palette();
  161. pal.set_color(ColorRole::Background, color);
  162. color_widget.set_palette(pal);
  163. };
  164. int colors_per_row = ceil(colors_to_add / 2);
  165. int number_of_added_colors = 0;
  166. for (auto& color : colors) {
  167. if (number_of_added_colors < colors_per_row)
  168. add_color_widget(top_color_container, color);
  169. else
  170. add_color_widget(bottom_color_container, color);
  171. ++number_of_added_colors;
  172. }
  173. }
  174. Vector<Color> PaletteWidget::colors()
  175. {
  176. Vector<Color> colors;
  177. for (auto& color_container : m_color_container->child_widgets()) {
  178. color_container.for_each_child_of_type<ColorWidget>([&](auto& color_widget) {
  179. colors.append(color_widget.color());
  180. return IterationDecision::Continue;
  181. });
  182. }
  183. return colors;
  184. }
  185. Result<Vector<Color>, String> PaletteWidget::load_palette_file(Core::File& file)
  186. {
  187. Vector<Color> palette;
  188. for (auto line : file.lines()) {
  189. if (line.is_whitespace())
  190. continue;
  191. auto color = Color::from_string(line);
  192. if (!color.has_value()) {
  193. dbgln("Could not parse \"{}\" as a color", line);
  194. continue;
  195. }
  196. palette.append(color.value());
  197. }
  198. file.close();
  199. if (palette.is_empty())
  200. return String { "The palette file did not contain any usable colors"sv };
  201. return palette;
  202. }
  203. Result<Vector<Color>, String> PaletteWidget::load_palette_path(String const& file_path)
  204. {
  205. auto file_or_error = Core::File::open(file_path, Core::OpenMode::ReadOnly);
  206. if (file_or_error.is_error())
  207. return String { strerror(file_or_error.error().code()) };
  208. auto& file = *file_or_error.value();
  209. return load_palette_file(file);
  210. }
  211. Result<void, String> PaletteWidget::save_palette_file(Vector<Color> palette, Core::File& file)
  212. {
  213. for (auto& color : palette) {
  214. file.write(color.to_string_without_alpha());
  215. file.write("\n");
  216. }
  217. return {};
  218. }
  219. Vector<Color> PaletteWidget::fallback_colors()
  220. {
  221. Vector<Color> fallback_colors;
  222. fallback_colors.append(Color::from_rgb(0x000000));
  223. fallback_colors.append(Color::from_rgb(0xffffff));
  224. return fallback_colors;
  225. }
  226. }