1 // Aseprite Document Library
2 // Copyright (c) 2001-2018 David Capello
3 //
4 // This file is released under the terms of the MIT license.
5 // Read LICENSE.txt for more information.
6 
7 #ifndef DOC_PALETTE_H_INCLUDED
8 #define DOC_PALETTE_H_INCLUDED
9 #pragma once
10 
11 #include "base/debug.h"
12 #include "doc/color.h"
13 #include "doc/frame.h"
14 #include "doc/object.h"
15 
16 #include <vector>
17 #include <string>
18 
19 namespace doc {
20 
21   class Remap;
22 
23   class Palette : public Object {
24   public:
25     Palette(frame_t frame, int ncolors);
26     Palette(const Palette& palette);
27     Palette(const Palette& palette, const Remap& remap);
28     ~Palette();
29 
30     static Palette* createGrayscale();
31 
size()32     int size() const { return (int)m_colors.size(); }
33     void resize(int ncolors);
34 
filename()35     const std::string& filename() const { return m_filename; }
comment()36     const std::string& comment() const { return m_comment; }
37 
setFilename(const std::string & filename)38     void setFilename(const std::string& filename) {
39       m_filename = filename;
40     }
41 
setComment(const std::string & comment)42     void setComment(const std::string& comment) {
43       m_comment = comment;
44     }
45 
getModifications()46     int getModifications() const { return m_modifications; }
47 
48     // Return true if the palette has alpha != 255 in some entry
49     bool hasAlpha() const;
50 
frame()51     frame_t frame() const { return m_frame; }
52     void setFrame(frame_t frame);
53 
entry(int i)54     color_t entry(int i) const {
55       // TODO At this moment we cannot enable this assert because
56       //      there are situations that are not handled quite well yet.
57       //      E.g. A palette with lesser colors is loaded
58       //
59       //ASSERT(i < size());
60       ASSERT(i >= 0);
61       if (i >= 0 && i < size())
62         return m_colors[i];
63       else
64         return 0;
65     }
getEntry(int i)66     color_t getEntry(int i) const {
67       return entry(i);
68     }
69     void setEntry(int i, color_t color);
70     void addEntry(color_t color);
71 
72     void copyColorsTo(Palette* dst) const;
73 
74     int countDiff(const Palette* other, int* from, int* to) const;
75 
76     bool operator==(const Palette& other) const {
77       return (countDiff(&other, nullptr, nullptr) == 0);
78     }
79 
80     bool operator!=(const Palette& other) const {
81       return !operator==(other);
82     }
83 
84     // Returns true if the palette is completely black.
85     bool isBlack() const;
86     void makeBlack();
87 
88     void makeGradient(int from, int to);
89 
90     int findExactMatch(int r, int g, int b, int a, int mask_index) const;
91     int findBestfit(int r, int g, int b, int a, int mask_index) const;
92 
93     void applyRemap(const Remap& remap);
94 
95     // TODO add undo/redo support of entry names
96     void setEntryName(const int i, const std::string& name);
97     const std::string& getEntryName(const int i) const;
98 
99   private:
100     frame_t m_frame;
101     std::vector<color_t> m_colors;
102     std::vector<std::string> m_names;
103     int m_modifications;
104     std::string m_filename; // If the palette is associated with a file.
105     std::string m_comment; // Some extra comment from the .gpl file (author, website, etc.).
106   };
107 
108 } // namespace doc
109 
110 #endif
111