PaletteWidget.cpp 8.1 KB

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