Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Loading...
Searching...
No Matches
graphics.h
Go to the documentation of this file.
1
32#pragma once
33
34#include <ogdf/basic/Logger.h>
35#include <ogdf/basic/basic.h>
36
37#include <cstdint>
38#include <iostream>
39#include <limits>
40#include <map>
41#include <string>
42#include <typeinfo>
43
44namespace ogdf {
45
47
50enum class StrokeType : unsigned char {
51 None,
52 Solid,
53 Dash,
54 Dot,
55 Dashdot,
57};
58
60OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const StrokeType& st);
61
63
67
69
72enum class StrokeLineCap : unsigned char { Butt, Round, Square };
73
74
76
79enum class StrokeLineJoin : unsigned char { Miter, Round, Bevel };
80
81
83
86enum class FillPattern {
87 None,
88 Solid,
89 Dense1,
90 Dense2,
91 Dense3,
92 Dense4,
93 Dense5,
94 Dense6,
95 Dense7,
98 Cross,
102};
103
105
107OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const FillPattern& fp);
108
110
114
115
117
120enum class Shape {
121 Rect,
123 Ellipse,
124 Triangle,
125 Pentagon,
126 Hexagon,
127 Octagon,
128 Rhomb,
129 Trapeze,
132 InvTrapeze,
134 Image
135};
136
138OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const Shape& shape);
139
140
142
145enum class EdgeArrow {
146 None,
147 Last,
148 First,
149 Both,
151};
152
154OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const EdgeArrow& ea);
155
157
165 uint8_t m_red, m_green, m_blue, m_alpha;
166
167public:
169 enum class Name {
170 Aliceblue,
171 Antiquewhite,
172 Aqua,
173 Aquamarine,
174 Azure,
175 Beige,
176 Bisque,
177 Black,
178 Blanchedalmond,
179 Blue,
180 Blueviolet,
181 Brown,
182 Burlywood,
183 Cadetblue,
184 Chartreuse,
185 Chocolate,
186 Coral,
187 Cornflowerblue,
188 Cornsilk,
189 Crimson,
190 Cyan,
191 Darkblue,
192 Darkcyan,
193 Darkgoldenrod,
194 Darkgray,
195 Darkgreen,
196 Darkgrey,
197 Darkkhaki,
198 Darkmagenta,
199 Darkolivegreen,
200 Darkorange,
201 Darkorchid,
202 Darkred,
203 Darksalmon,
204 Darkseagreen,
205 Darkslateblue,
206 Darkslategray,
207 Darkslategrey,
208 Darkturquoise,
209 Darkviolet,
210 Deeppink,
211 Deepskyblue,
212 Dimgray,
213 Dimgrey,
214 Dodgerblue,
215 Firebrick,
216 Floralwhite,
217 Forestgreen,
218 Fuchsia,
219 Gainsboro,
220 Ghostwhite,
221 Gold,
222 Goldenrod,
223 Gray,
224 Green,
225 Greenyellow,
226 Grey,
227 Honeydew,
228 Hotpink,
229 Indianred,
230 Indigo,
231 Ivory,
232 Khaki,
233 Lavender,
234 Lavenderblush,
235 Lawngreen,
236 Lemonchiffon,
237 Lightblue,
238 Lightcoral,
239 Lightcyan,
240 Lightgoldenrodyellow,
241 Lightgray,
242 Lightgreen,
243 Lightgrey,
244 Lightpink,
245 Lightsalmon,
246 Lightseagreen,
247 Lightskyblue,
248 Lightslategray,
249 Lightslategrey,
250 Lightsteelblue,
251 Lightyellow,
252 Lime,
253 Limegreen,
254 Linen,
255 Magenta,
256 Maroon,
257 Mediumaquamarine,
258 Mediumblue,
259 Mediumorchid,
260 Mediumpurple,
261 Mediumseagreen,
262 Mediumslateblue,
263 Mediumspringgreen,
264 Mediumturquoise,
265 Mediumvioletred,
266 Midnightblue,
267 Mintcream,
268 Mistyrose,
269 Moccasin,
270 Navajowhite,
271 Navy,
272 Oldlace,
273 Olive,
274 Olivedrab,
275 Orange,
276 Orangered,
277 Orchid,
278 Palegoldenrod,
279 Palegreen,
280 Paleturquoise,
281 Palevioletred,
282 Papayawhip,
283 Peachpuff,
284 Peru,
285 Pink,
286 Plum,
287 Powderblue,
288 Purple,
289 Red,
290 Rosybrown,
291 Royalblue,
292 Saddlebrown,
293 Salmon,
294 Sandybrown,
295 Seagreen,
296 Seashell,
297 Sienna,
298 Silver,
299 Skyblue,
300 Slateblue,
301 Slategray,
302 Slategrey,
303 Snow,
304 Springgreen,
305 Steelblue,
306 Tan,
307 Teal,
308 Thistle,
309 Tomato,
310 Turquoise,
311 Violet,
312 Wheat,
313 White,
314 Whitesmoke,
315 Yellow,
316 Yellowgreen
317 };
318
320 Color() : m_red(0), m_green(0), m_blue(0), m_alpha(255) { }
321
323 Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
324 : m_red(r), m_green(g), m_blue(b), m_alpha(a) { }
325
327 Color(int r, int g, int b, int a = 255)
328 : m_red((uint8_t)r), m_green((uint8_t)g), m_blue((uint8_t)b), m_alpha((uint8_t)a) { }
329
332
334 Color(const string& str) { fromString(str); }
335
337 Color(const char* str) { fromString(string(str)); }
338
340 uint8_t red() const { return m_red; }
341
343 uint8_t green() const { return m_green; }
344
346 uint8_t blue() const { return m_blue; }
347
349 uint8_t alpha() const { return m_alpha; }
350
352 void red(uint8_t r) { m_red = r; }
353
355 void green(uint8_t g) { m_green = g; }
356
358 void blue(uint8_t b) { m_blue = b; }
359
361 void alpha(uint8_t a) { m_alpha = a; }
362
364
369 string toString() const;
370
372
378 bool fromString(const string& str);
379
381 bool operator==(const Color& c) const {
382 return m_red == c.m_red && m_green == c.m_green && m_blue == c.m_blue && m_alpha == c.m_alpha;
383 }
384
386 bool operator!=(const Color& c) const { return !operator==(c); }
387
389 friend std::ostream& operator<<(std::ostream& os, const Color& c) { return os << c.toString(); }
390};
391
393
398 float m_width;
402
404 : m_color(Color::Name::Black)
405 , m_width(1.0f)
406 , m_type(StrokeType::Solid)
407 , m_cap(StrokeLineCap::Butt)
408 , m_join(StrokeLineJoin::Miter) { }
409
411 : m_color(c)
412 , m_width(1.0f)
413 , m_type(StrokeType::Solid)
414 , m_cap(StrokeLineCap::Butt)
415 , m_join(StrokeLineJoin::Miter) { }
416};
417
419
426
428 : m_color(Color::Name::White), m_bgColor(Color::Name::Black), m_pattern(FillPattern::Solid) { }
429
430 Fill(Color c) : m_color(c), m_bgColor(Color::Name::Black), m_pattern(FillPattern::Solid) { }
431
433 : m_color(c), m_bgColor(Color::Name::Black), m_pattern(pattern) { }
434
435 Fill(Color c, Color bgColor, FillPattern pattern)
436 : m_color(c), m_bgColor(bgColor), m_pattern(pattern) { }
437};
438
439namespace graphics {
440extern OGDF_EXPORT std::map<Shape, string> fromShape;
441extern OGDF_EXPORT std::map<string, Shape> toShape;
442
443extern OGDF_EXPORT std::map<StrokeType, string> fromStrokeType;
444extern OGDF_EXPORT std::map<string, StrokeType> toStrokeType;
445
446extern OGDF_EXPORT std::map<FillPattern, string> fromFillPattern;
447extern OGDF_EXPORT std::map<string, FillPattern> toFillPattern;
448
449template<class Enum>
450inline void init() {};
451
452template<class Enum>
453inline void initSecondMap(std::map<Enum, string>& fromMap, std::map<string, Enum>& toMap) {
454 for (auto it : fromMap) {
455 toMap.emplace(it.second, it.first);
456 }
457}
458
459template<>
460inline void init<StrokeType>() {
461 fromStrokeType.emplace(StrokeType::None, "None");
462 fromStrokeType.emplace(StrokeType::Solid, "Solid");
463 fromStrokeType.emplace(StrokeType::Dash, "Dash");
464 fromStrokeType.emplace(StrokeType::Dot, "Dot");
465 fromStrokeType.emplace(StrokeType::Dashdot, "Dashdot");
466 fromStrokeType.emplace(StrokeType::Dashdotdot, "Dashdotdot");
467
468 initSecondMap<StrokeType>(fromStrokeType, toStrokeType);
469}
470
471template<>
472inline void init<FillPattern>() {
473 fromFillPattern.emplace(FillPattern::None, "None");
474 fromFillPattern.emplace(FillPattern::Solid, "Solid");
475 fromFillPattern.emplace(FillPattern::Dense1, "Dense1");
476 fromFillPattern.emplace(FillPattern::Dense2, "Dense2");
477 fromFillPattern.emplace(FillPattern::Dense3, "Dense3");
478 fromFillPattern.emplace(FillPattern::Dense4, "Dense4");
479 fromFillPattern.emplace(FillPattern::Dense5, "Dense5");
480 fromFillPattern.emplace(FillPattern::Dense6, "Dense6");
481 fromFillPattern.emplace(FillPattern::Dense7, "Dense7");
482 fromFillPattern.emplace(FillPattern::Horizontal, "Horizontal");
483 fromFillPattern.emplace(FillPattern::Vertical, "Vertical");
484 fromFillPattern.emplace(FillPattern::Cross, "Cross");
485 fromFillPattern.emplace(FillPattern::BackwardDiagonal, "BackwardDiagonal");
486 fromFillPattern.emplace(FillPattern::ForwardDiagonal, "ForwardDiagonal");
487 fromFillPattern.emplace(FillPattern::DiagonalCross, "DiagonalCross");
488
489 initSecondMap<FillPattern>(fromFillPattern, toFillPattern);
490}
491
492template<>
493inline void init<Shape>() {
494 fromShape.emplace(Shape::Rect, "Rect");
495 fromShape.emplace(Shape::RoundedRect, "RoundedRect");
496 fromShape.emplace(Shape::Ellipse, "Ellipse");
497 fromShape.emplace(Shape::Triangle, "Triangle");
498 fromShape.emplace(Shape::Pentagon, "Pentagon");
499 fromShape.emplace(Shape::Hexagon, "Hexagon");
500 fromShape.emplace(Shape::Octagon, "Octagon");
501 fromShape.emplace(Shape::Rhomb, "Rhomb");
502 fromShape.emplace(Shape::Trapeze, "Trapeze");
503 fromShape.emplace(Shape::Parallelogram, "Parallelogram");
504 fromShape.emplace(Shape::InvTriangle, "InvTriangle");
505 fromShape.emplace(Shape::InvTrapeze, "InvTrapeze");
506 fromShape.emplace(Shape::InvParallelogram, "InvParallelogram");
507 fromShape.emplace(Shape::Image, "Image");
508
509 initSecondMap<Shape>(fromShape, toShape);
510 toShape.emplace("rectangle", Shape::Rect);
511 toShape.emplace("box", Shape::Image);
512}
513
514template<class ToClass>
515inline std::map<string, ToClass>* getMapToEnum() {
516 std::cout << "getMapToEnum was wrongly called\n";
517 OGDF_ASSERT(false);
518 return nullptr;
519};
520
521template<class FromClass>
522inline std::map<FromClass, string>* getMapToString() {
523 FromClass fc;
524 std::cout << "getMapToString was wrongly called " << typeid(fc).name() << "\n";
525 OGDF_ASSERT(false);
526 return nullptr;
527};
528
529template<>
530inline std::map<string, FillPattern>* getMapToEnum() {
531 return &toFillPattern;
532};
533
534template<>
535inline std::map<string, Shape>* getMapToEnum() {
536 return &toShape;
537};
538
539template<>
540inline std::map<string, StrokeType>* getMapToEnum() {
541 return &toStrokeType;
542};
543
544template<>
545inline std::map<FillPattern, string>* getMapToString() {
546 return &fromFillPattern;
547};
548
549template<>
550inline std::map<Shape, string>* getMapToString() {
551 return &fromShape;
552};
553
554template<>
555inline std::map<StrokeType, string>* getMapToString() {
556 return &fromStrokeType;
557};
558}
559
560template<class FromClass>
561inline string toString(FromClass key) {
562 auto* map = graphics::getMapToString<FromClass>();
563 if (map->empty()) {
564 graphics::init<FromClass>();
565 }
566 auto it = map->find(key);
567 OGDF_ASSERT(it != map->end());
568 return (*it).second;
569};
570
571template<class ToClass>
572inline ToClass fromString(string key) {
573 auto map = graphics::getMapToEnum<ToClass>();
574 if (map->empty()) {
575 graphics::init<ToClass>();
576 }
577 auto it = map->find(key);
578 if (it != map->end()) {
579 return (*it).second;
580 } else {
581 Logger::slout() << "Encountered invalid " << typeid((*map->begin()).second).name() << ": "
582 << key << " " << map->size() << " " << map->empty() << std::endl;
583 return static_cast<ToClass>(std::numeric_limits<int>::min());
584 }
585};
586}
Contains logging functionality.
Basic declarations, included by all source files.
Colors represented as RGBA values.
Definition graphics.h:164
Color(Color::Name name)
Creates a color from given color name name.
bool operator==(const Color &c) const
Returns true iff c and this color are equal in every component.
Definition graphics.h:381
uint8_t m_alpha
Definition graphics.h:165
Color(const char *str)
Crates a color from string str.
Definition graphics.h:337
uint8_t m_red
Definition graphics.h:165
uint8_t blue() const
Returns the blue component.
Definition graphics.h:346
void alpha(uint8_t a)
Sets the alpha channel to a.
Definition graphics.h:361
Name
Named colors (same as SVG color keywords).
Definition graphics.h:169
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Creates a color from given RGBA-values.
Definition graphics.h:323
uint8_t red() const
Returns the red component.
Definition graphics.h:340
uint8_t green() const
Returns the green component.
Definition graphics.h:343
bool operator!=(const Color &c) const
Returns true iff c and this color differ in any component.
Definition graphics.h:386
uint8_t m_blue
Definition graphics.h:165
Color(const string &str)
Crates a color from string str.
Definition graphics.h:334
void blue(uint8_t b)
Sets the blue component to b.
Definition graphics.h:358
void red(uint8_t r)
Sets the red component to r.
Definition graphics.h:352
uint8_t alpha() const
Returns the alpha channel.
Definition graphics.h:349
Color(int r, int g, int b, int a=255)
Creates a color from given RGBA-values.
Definition graphics.h:327
bool fromString(const string &str)
Sets this color to the color defined by str.
friend std::ostream & operator<<(std::ostream &os, const Color &c)
Writes the string representation of color c to output stream os.
Definition graphics.h:389
uint8_t m_green
Definition graphics.h:165
Color()
Creates an opaque black color.
Definition graphics.h:320
void green(uint8_t g)
Sets the green component to g.
Definition graphics.h:355
string toString() const
Converts the color to a string and returns it.
static std::ostream & slout(Level level=Level::Default)
stream for logging-output (global)
Definition Logger.h:193
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF dynamic library (shared object / DLL),...
Definition config.h:117
StrokeType intToStrokeType(int i)
Converts integer i to stroke type.
StrokeLineJoin
Line join types of strokes.
Definition graphics.h:79
FillPattern
Fill patterns.
Definition graphics.h:86
FillPattern intToFillPattern(int i)
Converts integer i to fill pattern.
StrokeLineCap
Line cap types of strokes.
Definition graphics.h:72
StrokeType
Line types of strokes.
Definition graphics.h:50
Shape
Types for node shapes.
Definition graphics.h:120
EdgeArrow
Types for edge arrows.
Definition graphics.h:145
@ Dash
dashed line
@ Dashdotdot
line style "dash dot dot dash dot dot ..."
@ Dashdot
line style "dash dot dash dot ..."
@ Dot
dotted line
@ Solid
solid line
@ InvTrapeze
inverted trapeze (upper side longer)
@ Pentagon
pentagon
@ Ellipse
ellipse
@ Hexagon
hexagon
@ RoundedRect
rectangle with rounded corners
@ Octagon
octagon
@ Rhomb
rhomb (=diamond)
@ InvParallelogram
inverted parallelogram (slanted to the left)
@ Triangle
isosceles triangle (base side down)
@ Rect
rectangle
@ Trapeze
trapeze (upper side shorter)
@ Parallelogram
parallelogram (slanted to the right)
@ InvTriangle
isosceles triangle (base side up)
@ Both
edge arrow at target and source node of the edge
@ First
edge arrow at source node of the edge
@ Last
edge arrow at target node of the edge
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition basic.h:52
std::map< string, FillPattern > toFillPattern
void init< StrokeType >()
Definition graphics.h:460
void initSecondMap(std::map< Enum, string > &fromMap, std::map< string, Enum > &toMap)
Definition graphics.h:453
void init< FillPattern >()
Definition graphics.h:472
std::map< string, ToClass > * getMapToEnum()
Definition graphics.h:515
std::map< Shape, string > fromShape
std::map< FromClass, string > * getMapToString()
Definition graphics.h:522
std::map< FillPattern, string > fromFillPattern
void init< Shape >()
Definition graphics.h:493
std::map< StrokeType, string > fromStrokeType
std::map< string, Shape > toShape
std::map< string, StrokeType > toStrokeType
The namespace for all OGDF objects.
@ None
Two geometric objects do not intersect.
void initFillPatternHashing()
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition Array.h:983
bool operator==(const Tuple2< E1, E2 > &t1, const Tuple2< E1, E2 > &t2)
Equality operator for 2-tuples.
Definition tuples.h:77
string toString(FromClass key)
Definition graphics.h:561
ToClass fromString(string key)
Definition graphics.h:572
Properties of fills.
Definition graphics.h:422
FillPattern m_pattern
fill pattern
Definition graphics.h:425
Fill(Color c, FillPattern pattern)
Definition graphics.h:432
Fill(Color c, Color bgColor, FillPattern pattern)
Definition graphics.h:435
Color m_color
fill color
Definition graphics.h:423
Fill(Color c)
Definition graphics.h:430
Color m_bgColor
background color of fill pattern
Definition graphics.h:424
Properties of strokes.
Definition graphics.h:396
float m_width
stroke width
Definition graphics.h:398
Color m_color
stroke color
Definition graphics.h:397
StrokeLineJoin m_join
line-join of the stroke
Definition graphics.h:401
StrokeType m_type
stroke type (e.g.
Definition graphics.h:399
StrokeLineCap m_cap
line-cap of the stroke
Definition graphics.h:400
Stroke(Color c)
Definition graphics.h:410