1 | // Aseprite |
2 | // Copyright (c) 2019-2022 Igara Studio S.A. |
3 | // |
4 | // This program is distributed under the terms of |
5 | // the End-User License Agreement for Aseprite. |
6 | |
7 | #ifdef HAVE_CONFIG_H |
8 | #include "config.h" |
9 | #endif |
10 | |
11 | #include "app/util/pal_ops.h" |
12 | |
13 | #include "doc/palette.h" |
14 | #include "doc/palette_picks.h" |
15 | #include "doc/remap.h" |
16 | |
17 | namespace app { |
18 | |
19 | void move_or_copy_palette_colors( |
20 | doc::Palette& palette, |
21 | doc::Palette& newPalette, |
22 | doc::PalettePicks& picks, |
23 | int& currentEntry, |
24 | const int beforeIndex, |
25 | const bool copy) |
26 | { |
27 | if (beforeIndex >= palette.size()) { |
28 | palette.resize(beforeIndex); // TODO is need to resize the |
29 | // palette? why not "const Palette& palette" |
30 | } |
31 | picks.resize(palette.size()); |
32 | |
33 | palette.copyColorsTo(&newPalette); |
34 | |
35 | // Copy colors |
36 | if (copy) { |
37 | int npicks = picks.picks(); |
38 | ASSERT(npicks >= 1); |
39 | |
40 | auto remap = doc::create_remap_to_expand_palette(palette.size()+npicks, |
41 | npicks, beforeIndex); |
42 | |
43 | newPalette.resize(palette.size()+npicks); |
44 | for (int i=0; i<palette.size(); ++i) |
45 | newPalette.setEntry(remap[i], palette.getEntry(i)); |
46 | |
47 | for (int i=0, j=0; i<palette.size(); ++i) { |
48 | if (picks[i]) |
49 | newPalette.setEntry(beforeIndex + (j++), palette.getEntry(i)); |
50 | } |
51 | |
52 | for (int i=0, j=0; i<palette.size(); ++i) { |
53 | if (picks[i]) { |
54 | if (currentEntry == i) { |
55 | currentEntry = beforeIndex + j; |
56 | break; |
57 | } |
58 | ++j; |
59 | } |
60 | } |
61 | |
62 | for (int i=0; i<palette.size(); ++i) |
63 | picks[i] = (i >= beforeIndex && i < beforeIndex + npicks); |
64 | } |
65 | // Move colors |
66 | else { |
67 | auto remap = doc::create_remap_to_move_picks(picks, beforeIndex); |
68 | |
69 | auto oldPicks = picks; |
70 | for (int i=0; i<palette.size(); ++i) { |
71 | newPalette.setEntry(remap[i], palette.getEntry(i)); |
72 | picks[remap[i]] = oldPicks[i]; |
73 | } |
74 | |
75 | currentEntry = remap[currentEntry]; |
76 | } |
77 | } |
78 | |
79 | } // namespace app |
80 | |