PaletteWidget.cpp 7.9 KB

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