-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyle.hpp
More file actions
374 lines (323 loc) · 10.9 KB
/
style.hpp
File metadata and controls
374 lines (323 loc) · 10.9 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* @file style.hpp
* @brief Line, marker, and text styles for CppPlot
*/
#ifndef CPPPLOT_CORE_STYLE_HPP
#define CPPPLOT_CORE_STYLE_HPP
#include "color.hpp"
#include "types.hpp"
#include <string>
#include <regex>
namespace cppplot {
/**
* @brief Line style definition
*/
struct LineStyle {
std::string style = "-"; // "-", "--", "-.", ":", "none"
double width = 1.5;
Color color = Color::tab10(0);
double alpha = 1.0;
LineStyle() = default;
LineStyle(const std::string& s, double w = 1.5, const Color& c = Color::tab10(0))
: style(s), width(w), color(c) {}
bool isVisible() const {
return style != "none" && style != "" && width > 0 && alpha > 0;
}
// Convert style to SVG dash array
std::string toDashArray() const {
if (style == "-" || style == "solid") return "";
if (style == "--" || style == "dashed") return "6,4";
if (style == "-." || style == "dashdot") return "6,2,2,2";
if (style == ":" || style == "dotted") return "2,2";
return "";
}
};
/**
* @brief Marker style definition
*/
struct MarkerStyle {
std::string marker = "none"; // "o", "s", "^", "v", "<", ">", "x", "+", "*", ".", "none"
double size = 6.0;
Color faceColor = Color::tab10(0);
Color edgeColor = Color::tab10(0);
double edgeWidth = 1.0;
double alpha = 1.0;
MarkerStyle() = default;
MarkerStyle(const std::string& m, double s = 6.0, const Color& fc = Color::tab10(0))
: marker(m), size(s), faceColor(fc), edgeColor(fc) {}
bool isVisible() const {
return marker != "none" && marker != "" && size > 0 && alpha > 0;
}
// Check if marker is filled
bool isFilled() const {
return marker == "o" || marker == "s" || marker == "^" ||
marker == "v" || marker == "<" || marker == ">" ||
marker == "d" || marker == "D" || marker == "h" ||
marker == "H" || marker == "p" || marker == "8";
}
};
/**
* @brief Text style definition
*/
struct TextStyle {
std::string fontFamily = "Arial, sans-serif";
double fontSize = 12.0;
std::string fontWeight = "normal"; // "normal", "bold"
std::string fontStyle = "normal"; // "normal", "italic"
Color color = Color::black();
TextAnchor anchor = TextAnchor::Start;
TextBaseline baseline = TextBaseline::Alphabetic;
double rotation = 0.0;
TextStyle() = default;
TextStyle& setSize(double size) { fontSize = size; return *this; }
TextStyle& setBold(bool bold = true) {
fontWeight = bold ? "bold" : "normal";
return *this;
}
TextStyle& setItalic(bool italic = true) {
fontStyle = italic ? "italic" : "normal";
return *this;
}
TextStyle& setColor(const Color& c) { color = c; return *this; }
TextStyle& setAnchor(TextAnchor a) { anchor = a; return *this; }
TextStyle& setRotation(double r) { rotation = r; return *this; }
std::string anchorString() const {
switch (anchor) {
case TextAnchor::Start: return "start";
case TextAnchor::Middle: return "middle";
case TextAnchor::End: return "end";
default: return "start";
}
}
std::string baselineString() const {
switch (baseline) {
case TextBaseline::Top: return "text-before-edge";
case TextBaseline::Middle: return "middle";
case TextBaseline::Bottom: return "text-after-edge";
case TextBaseline::Alphabetic: return "alphabetic";
default: return "alphabetic";
}
}
};
/**
* @brief Combined plot style (line + marker)
*/
struct PlotStyle {
LineStyle line;
MarkerStyle marker;
std::string label;
int zorder = 0;
PlotStyle() = default;
/**
* @brief Parse matplotlib-style format string
*
* Examples: "b-o", "r--", "g^", "k:", "m-."
*/
static PlotStyle parse(const std::string& fmt, int colorIndex = 0) {
PlotStyle style;
// Default color from cycle
Color defaultColor = Color::tab10(colorIndex);
style.line.color = defaultColor;
style.marker.faceColor = defaultColor;
style.marker.edgeColor = defaultColor;
if (fmt.empty()) {
return style;
}
std::string remaining = fmt;
// Parse color (single char at start)
if (!remaining.empty()) {
char c = remaining[0];
if (c == 'b' || c == 'g' || c == 'r' || c == 'c' ||
c == 'm' || c == 'y' || c == 'k' || c == 'w') {
Color color = Color::fromChar(c);
style.line.color = color;
style.marker.faceColor = color;
style.marker.edgeColor = color;
remaining = remaining.substr(1);
}
}
// Parse line style
if (remaining.find("-.") != std::string::npos) {
style.line.style = "-.";
remaining = std::regex_replace(remaining, std::regex("-\\."), "");
} else if (remaining.find("--") != std::string::npos) {
style.line.style = "--";
remaining = std::regex_replace(remaining, std::regex("--"), "");
} else if (remaining.find(":") != std::string::npos) {
style.line.style = ":";
remaining = std::regex_replace(remaining, std::regex(":"), "");
} else if (remaining.find("-") != std::string::npos) {
style.line.style = "-";
remaining = std::regex_replace(remaining, std::regex("-"), "");
} else {
style.line.style = "none"; // No line by default if marker specified
}
// Parse marker
if (!remaining.empty()) {
style.marker.marker = remaining;
if (style.line.style == "none" && remaining.length() > 0) {
// If only marker specified, we might want a line too
// Check original format
if (fmt.find('-') != std::string::npos ||
fmt.find(':') != std::string::npos) {
// Line was explicitly specified
} else {
// Marker only
style.line.style = "none";
}
} else if (style.line.style != "none") {
// Both line and marker
}
}
// If line style was found but marker style not explicitly none
if (style.line.style != "none" && style.marker.marker == "none") {
// Just line, no marker - this is correct
}
return style;
}
PlotStyle& setColor(const Color& c) {
line.color = c;
marker.faceColor = c;
marker.edgeColor = c;
return *this;
}
PlotStyle& setLineWidth(double w) {
line.width = w;
return *this;
}
PlotStyle& setMarkerSize(double s) {
marker.size = s;
return *this;
}
PlotStyle& setLabel(const std::string& l) {
label = l;
return *this;
}
PlotStyle& setAlpha(double a) {
line.alpha = a;
marker.alpha = a;
return *this;
}
};
/**
* @brief Grid style
*/
struct GridStyle {
bool show = false;
bool showMajor = true;
bool showMinor = false;
LineStyle majorStyle = LineStyle("-", 0.5, Color::gray(200));
LineStyle minorStyle = LineStyle(":", 0.3, Color::gray(220));
GridStyle() = default;
GridStyle& setVisible(bool v) { show = v; return *this; }
GridStyle& setColor(const Color& c) {
majorStyle.color = c;
minorStyle.color = c.lighter();
return *this;
}
};
/**
* @brief Axis style
*/
struct AxisStyle {
bool visible = true;
LineStyle lineStyle = LineStyle("-", 1.0, Color::black());
bool showTicks = true;
bool showTickLabels = true;
double tickLength = 5.0;
double tickWidth = 1.0;
TextStyle labelStyle;
TextStyle tickLabelStyle;
AxisStyle() {
labelStyle.fontSize = 12;
tickLabelStyle.fontSize = 10;
}
};
/**
* @brief Legend style
*/
struct LegendStyle {
bool visible = false;
LegendPosition position = LegendPosition::Best;
Color backgroundColor = Color::white().withAlpha(0.8);
Color borderColor = Color::gray(200);
double borderWidth = 1.0;
TextStyle textStyle;
double padding = 8.0;
double spacing = 4.0;
LegendStyle() {
textStyle.fontSize = 10;
}
};
/**
* @brief Complete figure/axes style theme
*/
struct Theme {
std::string name = "default";
Color backgroundColor = Color::white();
Color plotAreaColor = Color::white();
AxisStyle xAxisStyle;
AxisStyle yAxisStyle;
GridStyle gridStyle;
LegendStyle legendStyle;
TextStyle titleStyle;
std::vector<Color> colorCycle;
Theme() {
titleStyle.fontSize = 14;
titleStyle.setBold();
titleStyle.anchor = TextAnchor::Middle;
// Default color cycle (tab10)
for (int i = 0; i < 10; ++i) {
colorCycle.push_back(Color::tab10(i));
}
}
// Predefined themes
static Theme defaultTheme() {
return Theme();
}
static Theme dark() {
Theme t;
t.name = "dark";
t.backgroundColor = Color(30, 30, 30);
t.plotAreaColor = Color(40, 40, 40);
t.xAxisStyle.lineStyle.color = Color::gray(150);
t.yAxisStyle.lineStyle.color = Color::gray(150);
t.xAxisStyle.labelStyle.color = Color::gray(200);
t.yAxisStyle.labelStyle.color = Color::gray(200);
t.xAxisStyle.tickLabelStyle.color = Color::gray(180);
t.yAxisStyle.tickLabelStyle.color = Color::gray(180);
t.gridStyle.majorStyle.color = Color::gray(60);
t.titleStyle.color = Color::white();
t.legendStyle.backgroundColor = Color(40, 40, 40, 230);
t.legendStyle.borderColor = Color::gray(80);
t.legendStyle.textStyle.color = Color::gray(200);
return t;
}
static Theme seaborn() {
Theme t;
t.name = "seaborn";
t.plotAreaColor = Color(234, 234, 242);
t.gridStyle.show = true;
t.gridStyle.majorStyle = LineStyle("-", 1.0, Color::white());
t.xAxisStyle.lineStyle.style = "none";
t.yAxisStyle.lineStyle.style = "none";
return t;
}
static Theme ggplot() {
Theme t;
t.name = "ggplot";
t.plotAreaColor = Color(235, 235, 235);
t.gridStyle.show = true;
t.gridStyle.majorStyle = LineStyle("-", 1.0, Color::white());
return t;
}
// Get theme by name
static Theme get(const std::string& name) {
if (name == "dark") return dark();
if (name == "seaborn") return seaborn();
if (name == "ggplot") return ggplot();
return defaultTheme();
}
};
} // namespace cppplot
#endif // CPPPLOT_CORE_STYLE_HPP