Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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 
44 namespace ogdf {
45 
47 
50 enum class StrokeType : unsigned char {
51  None,
52  Solid,
53  Dash,
54  Dot,
55  Dashdot,
56  Dashdotdot
57 };
58 
60 OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const StrokeType& st);
61 
63 
67 
69 
72 enum class StrokeLineCap : unsigned char { Butt, Round, Square };
73 
74 
76 
79 enum class StrokeLineJoin : unsigned char { Miter, Round, Bevel };
80 
81 
83 
86 enum class FillPattern {
87  None,
88  Solid,
89  Dense1,
90  Dense2,
91  Dense3,
92  Dense4,
93  Dense5,
94  Dense6,
95  Dense7,
96  Horizontal,
97  Vertical,
98  Cross,
102 };
103 
105 
107 OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const FillPattern& fp);
108 
110 
114 
115 
117 
120 enum class Shape {
121  Rect,
122  RoundedRect,
123  Ellipse,
124  Triangle,
125  Pentagon,
126  Hexagon,
127  Octagon,
128  Rhomb,
129  Trapeze,
130  Parallelogram,
131  InvTriangle,
132  InvTrapeze,
134  Image
135 };
136 
138 OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const Shape& shape);
139 
140 
142 
145 enum class EdgeArrow {
146  None,
147  Last,
148  First,
149  Both,
150  Undefined
151 };
152 
154 OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const EdgeArrow& ea);
155 
157 
165  uint8_t m_red, m_green, m_blue, m_alpha;
166 
167 public:
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 
331  Color(Color::Name name);
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 
432  Fill(Color c, FillPattern pattern)
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 
439 namespace graphics {
440 extern OGDF_EXPORT std::map<Shape, string> fromShape;
441 extern OGDF_EXPORT std::map<string, Shape> toShape;
442 
443 extern OGDF_EXPORT std::map<StrokeType, string> fromStrokeType;
444 extern OGDF_EXPORT std::map<string, StrokeType> toStrokeType;
445 
446 extern OGDF_EXPORT std::map<FillPattern, string> fromFillPattern;
447 extern OGDF_EXPORT std::map<string, FillPattern> toFillPattern;
448 
449 template<class Enum>
450 inline void init() {};
451 
452 template<class Enum>
453 inline 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 
459 template<>
460 inline 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 
471 template<>
472 inline 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 
492 template<>
493 inline 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 
514 template<class ToClass>
515 inline std::map<string, ToClass>* getMapToEnum() {
516  std::cout << "getMapToEnum was wrongly called\n";
517  OGDF_ASSERT(false);
518  return nullptr;
519 };
520 
521 template<class FromClass>
522 inline 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 
529 template<>
530 inline std::map<string, FillPattern>* getMapToEnum() {
531  return &toFillPattern;
532 };
533 
534 template<>
535 inline std::map<string, Shape>* getMapToEnum() {
536  return &toShape;
537 };
538 
539 template<>
540 inline std::map<string, StrokeType>* getMapToEnum() {
541  return &toStrokeType;
542 };
543 
544 template<>
545 inline std::map<FillPattern, string>* getMapToString() {
546  return &fromFillPattern;
547 };
548 
549 template<>
550 inline std::map<Shape, string>* getMapToString() {
551  return &fromShape;
552 };
553 
554 template<>
555 inline std::map<StrokeType, string>* getMapToString() {
556  return &fromStrokeType;
557 };
558 }
559 
560 template<class FromClass>
561 inline 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 
571 template<class ToClass>
572 inline 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 }
ogdf::Color
Colors represented as RGBA values.
Definition: graphics.h:164
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::FillPattern::Dense3
@ Dense3
ogdf::StrokeType::Dashdot
@ Dashdot
line style "dash dot dash dot ..."
ogdf::Fill
Properties of fills.
Definition: graphics.h:422
ogdf::EdgeArrow::Last
@ Last
edge arrow at target node of the edge
ogdf::graphics::fromFillPattern
std::map< FillPattern, string > fromFillPattern
ogdf::Fill::m_pattern
FillPattern m_pattern
fill pattern
Definition: graphics.h:425
ogdf::StrokeType
StrokeType
Line types of strokes.
Definition: graphics.h:50
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
ogdf::Fill::Fill
Fill()
Definition: graphics.h:427
ogdf::graphics::toStrokeType
std::map< string, StrokeType > toStrokeType
ogdf::Stroke::Stroke
Stroke(Color c)
Definition: graphics.h:410
ogdf::Color::red
uint8_t red() const
Returns the red component.
Definition: graphics.h:340
ogdf::Fill::Fill
Fill(Color c)
Definition: graphics.h:430
ogdf::FillPattern::DiagonalCross
@ DiagonalCross
ogdf::FillPattern::Dense2
@ Dense2
ogdf::FillPattern::Dense1
@ Dense1
ogdf::Color::Color
Color(int r, int g, int b, int a=255)
Creates a color from given RGBA-values.
Definition: graphics.h:327
ogdf::FillPattern::Dense4
@ Dense4
ogdf::StrokeLineCap::Round
@ Round
ogdf::graphics::toFillPattern
std::map< string, FillPattern > toFillPattern
ogdf::Color::Color
Color()
Creates an opaque black color.
Definition: graphics.h:320
ogdf::operator==
bool operator==(const Tuple2< E1, E2 > &t1, const Tuple2< E1, E2 > &t2)
Equality operator for 2-tuples.
Definition: tuples.h:77
ogdf::StrokeLineJoin::Bevel
@ Bevel
ogdf::Shape::Octagon
@ Octagon
octagon
ogdf::Fill::Fill
Fill(Color c, FillPattern pattern)
Definition: graphics.h:432
ogdf::Stroke::m_color
Color m_color
stroke color
Definition: graphics.h:397
ogdf::graphics::init< StrokeType >
void init< StrokeType >()
Definition: graphics.h:460
ogdf::EdgeArrow::None
@ None
no edge arrows
ogdf::StrokeType::Dash
@ Dash
dashed line
ogdf::Fill::Fill
Fill(Color c, Color bgColor, FillPattern pattern)
Definition: graphics.h:435
ogdf::Shape::RoundedRect
@ RoundedRect
rectangle with rounded corners
ogdf::Shape::Rhomb
@ Rhomb
rhomb (=diamond)
ogdf::graphics::init< FillPattern >
void init< FillPattern >()
Definition: graphics.h:472
ogdf::graphics::toShape
std::map< string, Shape > toShape
ogdf::EdgeArrow::Undefined
@ Undefined
ogdf::graphics::init< Shape >
void init< Shape >()
Definition: graphics.h:493
ogdf::StrokeType::Dashdotdot
@ Dashdotdot
line style "dash dot dot dash dot dot ..."
ogdf::Shape::Trapeze
@ Trapeze
trapeze (upper side shorter)
ogdf::initFillPatternHashing
void initFillPatternHashing()
ogdf::Shape::Image
@ Image
ogdf::graphics::initSecondMap
void initSecondMap(std::map< Enum, string > &fromMap, std::map< string, Enum > &toMap)
Definition: graphics.h:453
Logger.h
Contains logging functionality.
ogdf::StrokeLineCap::Butt
@ Butt
ogdf::EdgeArrow::First
@ First
edge arrow at source node of the edge
ogdf::Stroke::m_width
float m_width
stroke width
Definition: graphics.h:398
ogdf::FillPattern::Vertical
@ Vertical
ogdf::FillPattern::Dense5
@ Dense5
ogdf::FillPattern::Cross
@ Cross
ogdf::Color::operator==
bool operator==(const Color &c) const
Returns true iff c and this color are equal in every component.
Definition: graphics.h:381
ogdf::graphics::fromShape
std::map< Shape, string > fromShape
r
int r[]
Definition: hierarchical-ranking.cpp:13
ogdf::Fill::m_bgColor
Color m_bgColor
background color of fill pattern
Definition: graphics.h:424
ogdf::Color::green
uint8_t green() const
Returns the green component.
Definition: graphics.h:343
ogdf::FillPattern::ForwardDiagonal
@ ForwardDiagonal
ogdf::fromString
ToClass fromString(string key)
Definition: graphics.h:572
ogdf::Shape::Parallelogram
@ Parallelogram
parallelogram (slanted to the right)
ogdf::Stroke::m_join
StrokeLineJoin m_join
line-join of the stroke
Definition: graphics.h:401
ogdf::Stroke
Properties of strokes.
Definition: graphics.h:396
ogdf::Color::Name
Name
Named colors (same as SVG color keywords).
Definition: graphics.h:169
ogdf::StrokeLineJoin::Round
@ Round
ogdf::StrokeLineJoin
StrokeLineJoin
Line join types of strokes.
Definition: graphics.h:79
ogdf::Shape::InvTriangle
@ InvTriangle
isosceles triangle (base side up)
ogdf::FillPattern::None
@ None
ogdf::Color::blue
uint8_t blue() const
Returns the blue component.
Definition: graphics.h:346
ogdf::Color::alpha
void alpha(uint8_t a)
Sets the alpha channel to a.
Definition: graphics.h:361
ogdf::Color::m_green
uint8_t m_green
Definition: graphics.h:165
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:983
ogdf::Stroke::m_type
StrokeType m_type
stroke type (e.g.
Definition: graphics.h:399
ogdf::Color::green
void green(uint8_t g)
Sets the green component to g.
Definition: graphics.h:355
ogdf::Shape::Pentagon
@ Pentagon
pentagon
ogdf::Color::toString
string toString() const
Converts the color to a string and returns it.
ogdf::Shape::Ellipse
@ Ellipse
ellipse
ogdf::graphics::getMapToString
std::map< FromClass, string > * getMapToString()
Definition: graphics.h:522
ogdf::Color::Color
Color(const char *str)
Crates a color from string str.
Definition: graphics.h:337
ogdf::StrokeType::None
@ None
no line
ogdf::EdgeArrow
EdgeArrow
Types for edge arrows.
Definition: graphics.h:145
ogdf::Shape::InvTrapeze
@ InvTrapeze
inverted trapeze (upper side longer)
ogdf::Color::Color
Color(const string &str)
Crates a color from string str.
Definition: graphics.h:334
ogdf::Color::blue
void blue(uint8_t b)
Sets the blue component to b.
Definition: graphics.h:358
ogdf::Shape::InvParallelogram
@ InvParallelogram
inverted parallelogram (slanted to the left)
ogdf::Color::alpha
uint8_t alpha() const
Returns the alpha channel.
Definition: graphics.h:349
ogdf::graphics::init
void init()
Definition: graphics.h:450
ogdf::Color::m_blue
uint8_t m_blue
Definition: graphics.h:165
ogdf::toString
string toString(FromClass key)
Definition: graphics.h:561
basic.h
Basic declarations, included by all source files.
ogdf::EdgeArrow::Both
@ Both
edge arrow at target and source node of the edge
ogdf::StrokeLineJoin::Miter
@ Miter
ogdf::Color::operator!=
bool operator!=(const Color &c) const
Returns true iff c and this color differ in any component.
Definition: graphics.h:386
OGDF_EXPORT
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:101
ogdf::FillPattern::Solid
@ Solid
ogdf::FillPattern::BackwardDiagonal
@ BackwardDiagonal
ogdf::Fill::m_color
Color m_color
fill color
Definition: graphics.h:423
ogdf::Color::m_red
uint8_t m_red
Definition: graphics.h:165
ogdf::Shape::Rect
@ Rect
rectangle
ogdf::Shape
Shape
Types for node shapes.
Definition: graphics.h:120
ogdf::Color::Color
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
ogdf::FillPattern::Horizontal
@ Horizontal
ogdf::Color::m_alpha
uint8_t m_alpha
Definition: graphics.h:165
ogdf::graphics::fromStrokeType
std::map< StrokeType, string > fromStrokeType
ogdf::StrokeType::Solid
@ Solid
solid line
ogdf::StrokeLineCap::Square
@ Square
ogdf::StrokeLineCap
StrokeLineCap
Line cap types of strokes.
Definition: graphics.h:72
ogdf::Color::operator<<
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
ogdf::intToFillPattern
FillPattern intToFillPattern(int i)
Converts integer i to fill pattern.
ogdf::Stroke::Stroke
Stroke()
Definition: graphics.h:403
ogdf::Shape::Hexagon
@ Hexagon
hexagon
ogdf::FillPattern
FillPattern
Fill patterns.
Definition: graphics.h:86
ogdf::FillPattern::Dense7
@ Dense7
ogdf::Shape::Triangle
@ Triangle
isosceles triangle (base side down)
ogdf::FillPattern::Dense6
@ Dense6
ogdf::Color::red
void red(uint8_t r)
Sets the red component to r.
Definition: graphics.h:352
ogdf::intToStrokeType
StrokeType intToStrokeType(int i)
Converts integer i to stroke type.
ogdf::StrokeType::Dot
@ Dot
dotted line
ogdf::Logger::slout
static std::ostream & slout(Level level=Level::Default)
stream for logging-output (global)
Definition: Logger.h:193
ogdf::Stroke::m_cap
StrokeLineCap m_cap
line-cap of the stroke
Definition: graphics.h:400
ogdf::graphics::getMapToEnum
std::map< string, ToClass > * getMapToEnum()
Definition: graphics.h:515