-
Notifications
You must be signed in to change notification settings - Fork 772
/
ColorPalette.cpp
188 lines (156 loc) · 6.08 KB
/
ColorPalette.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "ColorPalette.h"
#include <fstream>
#include <limits>
#include <sstream>
#include <gtk/gtk.h>
#include "util/StringUtils.h"
#include "util/i18n.h"
#include "util/serdesstream.h"
Palette::Palette(fs::path path): filepath{std::move(path)}, namedColors{}, header{} {}
void Palette::load() {
if (!fs::exists(this->filepath))
throw std::invalid_argument(
FS(FORMAT_STR("The palette file {1} does not exist.") % this->filepath.filename().u8string()));
int lineNumber{};
header.clear();
namedColors.clear();
auto gplFile = serdes_stream<std::ifstream>(filepath);
std::string line;
if (gplFile.is_open()) {
getline(gplFile, line);
lineNumber++;
// parse standard header line
parseFirstGimpPaletteLine(line);
/*
* attempt parsing line by line as
* either header, color, or fallback
*/
while (getline(gplFile, line)) {
lineNumber++;
parseCommentLine(line) || parseHeaderLine(line) || parseColorLine(line) || parseLineFallback(lineNumber);
}
if (namedColors.size() < 1)
throw std::invalid_argument("Your Palettefile has no parsable color. It needs at least one!");
}
}
auto Palette::load_default() -> void {
std::stringstream defaultFile{default_palette()};
std::string line;
getline(defaultFile, line);
if (!parseFirstGimpPaletteLine(line))
g_error("The default file was mallformed. This should never happen!");
while (getline(defaultFile, line)) { parseHeaderLine(line) || parseColorLine(line); }
}
auto Palette::parseFirstGimpPaletteLine(const std::string& line) const -> bool {
if (StringUtils::trim(line) != "GIMP Palette") {
throw std::invalid_argument(".gpl file needs to start with \"GIMP Palette\" in the "
"first line");
}
return true;
}
auto Palette::parseHeaderLine(const std::string& line) -> bool {
Header headerEntry;
std::istringstream iss{line};
if (iss >> headerEntry) {
header[headerEntry.getAttribute()] = headerEntry.getValue();
return true;
}
return false;
}
auto Palette::parseColorLine(const std::string& line) -> bool {
NamedColor color{namedColors.size()};
std::istringstream iss{line};
if (iss >> color) {
namedColors.emplace_back(std::move(color));
return true;
}
return false;
}
auto Palette::parseCommentLine(const std::string& line) const -> bool { return line.front() == '#'; }
auto Palette::parseLineFallback(int lineNumber) const -> const bool {
throw std::invalid_argument(FS(FORMAT_STR("The line {1} is malformed.") % lineNumber));
}
auto Palette::getColorAt(size_t i) const -> NamedColor const& {
if (i >= namedColors.size()) {
i = i % namedColors.size();
g_warning("There are more Coloritems in the Toolbar than your Palette defines.\n"
"Hence, cycling through palette from the beginning.");
}
return namedColors.at(i);
}
auto Palette::size() const -> size_t { return namedColors.size(); }
auto Palette::default_palette() -> const std::string {
auto d = serdes_stream<std::stringstream>();
d << "GIMP Palette\n"
<< "Name: Xournal Default Palette\n"
<< "#\n"
<< 0 << " " << 0 << " " << 0 << " "
<< "Black\n"
<< 0 << " " << 128 << " " << 0 << " "
<< "Green\n"
<< 0 << " " << 192 << " " << 255 << " "
<< "Light Blue\n"
<< 0 << " " << 255 << " " << 0 << " "
<< "Light Green\n"
<< 51 << " " << 51 << " " << 204 << " "
<< "Blue\n"
<< 128 << " " << 128 << " " << 128 << " "
<< "Gray\n"
<< 255 << " " << 0 << " " << 0 << " "
<< "Red\n"
<< 255 << " " << 0 << " " << 255 << " "
<< "Magenta\n"
<< 255 << " " << 128 << " " << 0 << " "
<< "Orange\n"
<< 255 << " " << 255 << " " << 0 << " "
<< "Yellow\n"
<< 255 << " " << 255 << " " << 255 << " "
<< "White" << std::endl;
return d.str();
}
void Palette::create_default(fs::path filepath) {
auto myfile = serdes_stream<std::ofstream>(filepath);
myfile << default_palette();
}
auto Header::getAttribute() const -> std::string { return this->attribute; };
auto Header::getValue() const -> std::string { return this->value; };
auto operator>>(std::istream& str, Header& header) -> std::istream& {
/*
* inspired by
* https://codereview.stackexchange.com/questions/38879/parsing-text-file-in-c
*/
std::string line;
Header tmp;
if (std::getline(str, line)) {
std::istringstream iss{line};
if (getline(iss, tmp.attribute, ':') && getline(iss, tmp.value, '\n')) {
tmp.attribute = StringUtils::trim(tmp.attribute);
tmp.value = StringUtils::trim(tmp.value);
// parsing was successful hence results can be stored permanently
header = std::move(tmp);
} else {
/*
* One operation failed.
* So set the state on the main stream
* to indicate failure.
*/
str.setstate(std::ios::failbit);
}
}
return str;
}
auto Palette::parseErrorDialog(const std::exception& e) const -> void {
std::stringstream msg_stream{};
msg_stream << "There has been a problem parsing the color palette file at " << filepath.c_str() << "\n\n";
msg_stream << "What happened:\n" << e.what() << std::endl;
msg_stream << "What to do:\n";
msg_stream << "Please fix your palette file, or rename it so xournalpp creates a new default palette file "
"for you. This file can then be used as a template.\n";
msg_stream << "What will happen now:\nThe application will start with the default color palette.";
GtkWidget* dialog = gtk_message_dialog_new(nullptr, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s",
msg_stream.str().c_str());
gtk_dialog_add_button(GTK_DIALOG(dialog), _("OK"), 1);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
g_warning("%s", msg_stream.str().c_str());
}