PaletteWidget.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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_style(Gfx::FrameStyle::NoFrame);
  80. set_fill_with_background_color(true);
  81. set_fixed_height(35);
  82. m_secondary_color_widget = add<SelectedColorWidget>();
  83. m_secondary_color_widget->on_color_change = [&](auto color) {
  84. set_secondary_color(color);
  85. };
  86. m_secondary_color_widget->set_relative_rect({ 0, 2, 60, 33 });
  87. m_secondary_color_widget->set_fill_with_background_color(true);
  88. m_primary_color_widget = add<SelectedColorWidget>();
  89. m_primary_color_widget->on_color_change = [&](auto color) {
  90. set_primary_color(color);
  91. };
  92. auto rect = Gfx::IntRect(0, 0, 35, 17).centered_within(m_secondary_color_widget->relative_rect());
  93. m_primary_color_widget->set_relative_rect(rect);
  94. m_primary_color_widget->set_fill_with_background_color(true);
  95. m_color_container = add<GUI::Widget>();
  96. m_color_container->set_relative_rect(m_secondary_color_widget->relative_rect().right() + 1, 2, 500, 33);
  97. m_color_container->set_layout<GUI::VerticalBoxLayout>(GUI::Margins {}, 1);
  98. auto& top_color_container = m_color_container->add<GUI::Widget>();
  99. top_color_container.set_name("top_color_container");
  100. top_color_container.set_layout<GUI::HorizontalBoxLayout>(GUI::Margins {}, 1);
  101. auto& bottom_color_container = m_color_container->add<GUI::Widget>();
  102. bottom_color_container.set_name("bottom_color_container");
  103. bottom_color_container.set_layout<GUI::HorizontalBoxLayout>(GUI::Margins {}, 1);
  104. auto result = load_palette_path("/res/color-palettes/default.palette");
  105. if (result.is_error()) {
  106. GUI::MessageBox::show_error(window(), MUST(String::formatted("Loading default palette failed: {}", result.release_error())));
  107. display_color_list(fallback_colors());
  108. return;
  109. }
  110. display_color_list(result.value());
  111. }
  112. void PaletteWidget::set_image_editor(ImageEditor* editor)
  113. {
  114. m_editor = editor;
  115. if (!m_editor)
  116. return;
  117. set_primary_color(editor->primary_color());
  118. set_secondary_color(editor->secondary_color());
  119. }
  120. void PaletteWidget::set_primary_color(Color color)
  121. {
  122. if (m_editor)
  123. m_editor->set_primary_color(color);
  124. m_primary_color_widget->set_background_color(color);
  125. }
  126. void PaletteWidget::set_secondary_color(Color color)
  127. {
  128. if (m_editor)
  129. m_editor->set_secondary_color(color);
  130. m_secondary_color_widget->set_background_color(color);
  131. }
  132. void PaletteWidget::display_color_list(Vector<Color> const& colors)
  133. {
  134. int colors_to_add = colors.size();
  135. if (colors_to_add == 0) {
  136. dbgln("Empty color list given. Using fallback colors.");
  137. display_color_list(fallback_colors());
  138. return;
  139. }
  140. auto& top_color_container = *m_color_container->find_descendant_of_type_named<GUI::Widget>("top_color_container");
  141. top_color_container.remove_all_children();
  142. auto& bottom_color_container = *m_color_container->find_descendant_of_type_named<GUI::Widget>("bottom_color_container");
  143. bottom_color_container.remove_all_children();
  144. auto add_color_widget = [&](GUI::Widget& container, Color color) {
  145. auto& color_widget = container.add<ColorWidget>(color, *this);
  146. color_widget.set_fill_with_background_color(true);
  147. color_widget.set_fixed_size(16, 16);
  148. auto pal = color_widget.palette();
  149. pal.set_color(ColorRole::Background, color);
  150. color_widget.set_palette(pal);
  151. };
  152. int colors_per_row = ceil(colors_to_add / 2);
  153. int number_of_added_colors = 0;
  154. for (auto& color : colors) {
  155. if (number_of_added_colors < colors_per_row)
  156. add_color_widget(top_color_container, color);
  157. else
  158. add_color_widget(bottom_color_container, color);
  159. ++number_of_added_colors;
  160. }
  161. }
  162. Vector<Color> PaletteWidget::colors()
  163. {
  164. Vector<Color> colors;
  165. for (auto& color_container : m_color_container->child_widgets()) {
  166. color_container.for_each_child_of_type<ColorWidget>([&](auto& color_widget) {
  167. colors.append(color_widget.color());
  168. return IterationDecision::Continue;
  169. });
  170. }
  171. return colors;
  172. }
  173. ErrorOr<Vector<Color>> PaletteWidget::load_palette_file(NonnullOwnPtr<Core::File> file)
  174. {
  175. Vector<Color> palette;
  176. Array<u8, PAGE_SIZE> buffer;
  177. auto buffered_file = TRY(Core::InputBufferedFile::create(move(file)));
  178. while (TRY(buffered_file->can_read_line())) {
  179. auto line = TRY(buffered_file->read_line(buffer));
  180. if (line.is_whitespace())
  181. continue;
  182. auto color = Color::from_string(line);
  183. if (!color.has_value()) {
  184. dbgln("Could not parse \"{}\" as a color", line);
  185. continue;
  186. }
  187. palette.append(color.value());
  188. }
  189. if (palette.is_empty())
  190. return Error::from_string_literal("The palette file did not contain any usable colors");
  191. return palette;
  192. }
  193. ErrorOr<Vector<Color>> PaletteWidget::load_palette_path(DeprecatedString const& file_path)
  194. {
  195. auto file = TRY(Core::File::open(file_path, Core::File::OpenMode::Read));
  196. return load_palette_file(move(file));
  197. }
  198. ErrorOr<void> PaletteWidget::save_palette_file(Vector<Color> palette, NonnullOwnPtr<Core::File> file)
  199. {
  200. for (auto& color : palette) {
  201. TRY(file->write_until_depleted(color.to_deprecated_string_without_alpha().bytes()));
  202. TRY(file->write_until_depleted({ "\n", 1 }));
  203. }
  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. }