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 <map>
38 #include <typeinfo>
39 
40 namespace ogdf {
41 
43 
46 enum class StrokeType : unsigned char {
47  None,
48  Solid,
49  Dash,
50  Dot,
51  Dashdot,
52  Dashdotdot
53 };
54 
56 OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const StrokeType& st);
57 
59 
63 
65 
68 enum class StrokeLineCap : unsigned char { Butt, Round, Square };
69 
70 
72 
75 enum class StrokeLineJoin : unsigned char { Miter, Round, Bevel };
76 
77 
79 
82 enum class FillPattern {
83  None,
84  Solid,
85  Dense1,
86  Dense2,
87  Dense3,
88  Dense4,
89  Dense5,
90  Dense6,
91  Dense7,
92  Horizontal,
93  Vertical,
94  Cross,
98 };
99 
101 
103 OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const FillPattern& fp);
104 
106 
110 
111 
113 
116 enum class Shape {
117  Rect,
118  RoundedRect,
119  Ellipse,
120  Triangle,
121  Pentagon,
122  Hexagon,
123  Octagon,
124  Rhomb,
125  Trapeze,
126  Parallelogram,
127  InvTriangle,
128  InvTrapeze,
130  Image
131 };
132 
134 OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const Shape& shape);
135 
136 
138 
141 enum class EdgeArrow {
142  None,
143  Last,
144  First,
145  Both,
146  Undefined
147 };
148 
150 OGDF_EXPORT std::ostream& operator<<(std::ostream& os, const EdgeArrow& ea);
151 
153 
161  uint8_t m_red, m_green, m_blue, m_alpha;
162 
163 public:
165  enum class Name {
166  Aliceblue,
167  Antiquewhite,
168  Aqua,
169  Aquamarine,
170  Azure,
171  Beige,
172  Bisque,
173  Black,
174  Blanchedalmond,
175  Blue,
176  Blueviolet,
177  Brown,
178  Burlywood,
179  Cadetblue,
180  Chartreuse,
181  Chocolate,
182  Coral,
183  Cornflowerblue,
184  Cornsilk,
185  Crimson,
186  Cyan,
187  Darkblue,
188  Darkcyan,
189  Darkgoldenrod,
190  Darkgray,
191  Darkgreen,
192  Darkgrey,
193  Darkkhaki,
194  Darkmagenta,
195  Darkolivegreen,
196  Darkorange,
197  Darkorchid,
198  Darkred,
199  Darksalmon,
200  Darkseagreen,
201  Darkslateblue,
202  Darkslategray,
203  Darkslategrey,
204  Darkturquoise,
205  Darkviolet,
206  Deeppink,
207  Deepskyblue,
208  Dimgray,
209  Dimgrey,
210  Dodgerblue,
211  Firebrick,
212  Floralwhite,
213  Forestgreen,
214  Fuchsia,
215  Gainsboro,
216  Ghostwhite,
217  Gold,
218  Goldenrod,
219  Gray,
220  Green,
221  Greenyellow,
222  Grey,
223  Honeydew,
224  Hotpink,
225  Indianred,
226  Indigo,
227  Ivory,
228  Khaki,
229  Lavender,
230  Lavenderblush,
231  Lawngreen,
232  Lemonchiffon,
233  Lightblue,
234  Lightcoral,
235  Lightcyan,
236  Lightgoldenrodyellow,
237  Lightgray,
238  Lightgreen,
239  Lightgrey,
240  Lightpink,
241  Lightsalmon,
242  Lightseagreen,
243  Lightskyblue,
244  Lightslategray,
245  Lightslategrey,
246  Lightsteelblue,
247  Lightyellow,
248  Lime,
249  Limegreen,
250  Linen,
251  Magenta,
252  Maroon,
253  Mediumaquamarine,
254  Mediumblue,
255  Mediumorchid,
256  Mediumpurple,
257  Mediumseagreen,
258  Mediumslateblue,
259  Mediumspringgreen,
260  Mediumturquoise,
261  Mediumvioletred,
262  Midnightblue,
263  Mintcream,
264  Mistyrose,
265  Moccasin,
266  Navajowhite,
267  Navy,
268  Oldlace,
269  Olive,
270  Olivedrab,
271  Orange,
272  Orangered,
273  Orchid,
274  Palegoldenrod,
275  Palegreen,
276  Paleturquoise,
277  Palevioletred,
278  Papayawhip,
279  Peachpuff,
280  Peru,
281  Pink,
282  Plum,
283  Powderblue,
284  Purple,
285  Red,
286  Rosybrown,
287  Royalblue,
288  Saddlebrown,
289  Salmon,
290  Sandybrown,
291  Seagreen,
292  Seashell,
293  Sienna,
294  Silver,
295  Skyblue,
296  Slateblue,
297  Slategray,
298  Slategrey,
299  Snow,
300  Springgreen,
301  Steelblue,
302  Tan,
303  Teal,
304  Thistle,
305  Tomato,
306  Turquoise,
307  Violet,
308  Wheat,
309  White,
310  Whitesmoke,
311  Yellow,
312  Yellowgreen
313  };
314 
316  Color() : m_red(0), m_green(0), m_blue(0), m_alpha(255) { }
317 
319  Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255)
320  : m_red(r), m_green(g), m_blue(b), m_alpha(a) { }
321 
323  Color(int r, int g, int b, int a = 255)
324  : m_red((uint8_t)r), m_green((uint8_t)g), m_blue((uint8_t)b), m_alpha((uint8_t)a) { }
325 
327  Color(Color::Name name);
328 
330  Color(const string& str) { fromString(str); }
331 
333  Color(const char* str) { fromString(string(str)); }
334 
336  uint8_t red() const { return m_red; }
337 
339  uint8_t green() const { return m_green; }
340 
342  uint8_t blue() const { return m_blue; }
343 
345  uint8_t alpha() const { return m_alpha; }
346 
348  void red(uint8_t r) { m_red = r; }
349 
351  void green(uint8_t g) { m_green = g; }
352 
354  void blue(uint8_t b) { m_blue = b; }
355 
357  void alpha(uint8_t a) { m_alpha = a; }
358 
360 
365  string toString() const;
366 
368 
374  bool fromString(const string& str);
375 
377  bool operator==(const Color& c) const {
378  return m_red == c.m_red && m_green == c.m_green && m_blue == c.m_blue && m_alpha == c.m_alpha;
379  }
380 
382  bool operator!=(const Color& c) const { return !operator==(c); }
383 
385  friend std::ostream& operator<<(std::ostream& os, const Color& c) { return os << c.toString(); }
386 };
387 
389 
394  float m_width;
398 
400  : m_color(Color::Name::Black)
401  , m_width(1.0f)
402  , m_type(StrokeType::Solid)
403  , m_cap(StrokeLineCap::Butt)
404  , m_join(StrokeLineJoin::Miter) { }
405 
407  : m_color(c)
408  , m_width(1.0f)
409  , m_type(StrokeType::Solid)
410  , m_cap(StrokeLineCap::Butt)
411  , m_join(StrokeLineJoin::Miter) { }
412 };
413 
415 
422 
424  : m_color(Color::Name::White), m_bgColor(Color::Name::Black), m_pattern(FillPattern::Solid) { }
425 
426  Fill(Color c) : m_color(c), m_bgColor(Color::Name::Black), m_pattern(FillPattern::Solid) { }
427 
428  Fill(Color c, FillPattern pattern)
429  : m_color(c), m_bgColor(Color::Name::Black), m_pattern(pattern) { }
430 
431  Fill(Color c, Color bgColor, FillPattern pattern)
432  : m_color(c), m_bgColor(bgColor), m_pattern(pattern) { }
433 };
434 
435 namespace graphics {
436 extern OGDF_EXPORT std::map<Shape, string> fromShape;
437 extern OGDF_EXPORT std::map<string, Shape> toShape;
438 
439 extern OGDF_EXPORT std::map<StrokeType, string> fromStrokeType;
440 extern OGDF_EXPORT std::map<string, StrokeType> toStrokeType;
441 
442 extern OGDF_EXPORT std::map<FillPattern, string> fromFillPattern;
443 extern OGDF_EXPORT std::map<string, FillPattern> toFillPattern;
444 
445 template<class Enum>
446 inline void init() {};
447 
448 template<class Enum>
449 inline void initSecondMap(std::map<Enum, string>& fromMap, std::map<string, Enum>& toMap) {
450  for (auto it : fromMap) {
451  toMap.emplace(it.second, it.first);
452  }
453 }
454 
455 template<>
456 inline void init<StrokeType>() {
457  fromStrokeType.emplace(StrokeType::None, "None");
458  fromStrokeType.emplace(StrokeType::Solid, "Solid");
459  fromStrokeType.emplace(StrokeType::Dash, "Dash");
460  fromStrokeType.emplace(StrokeType::Dot, "Dot");
461  fromStrokeType.emplace(StrokeType::Dashdot, "Dashdot");
462  fromStrokeType.emplace(StrokeType::Dashdotdot, "Dashdotdot");
463 
464  initSecondMap<StrokeType>(fromStrokeType, toStrokeType);
465 }
466 
467 template<>
468 inline void init<FillPattern>() {
469  fromFillPattern.emplace(FillPattern::None, "None");
470  fromFillPattern.emplace(FillPattern::Solid, "Solid");
471  fromFillPattern.emplace(FillPattern::Dense1, "Dense1");
472  fromFillPattern.emplace(FillPattern::Dense2, "Dense2");
473  fromFillPattern.emplace(FillPattern::Dense3, "Dense3");
474  fromFillPattern.emplace(FillPattern::Dense4, "Dense4");
475  fromFillPattern.emplace(FillPattern::Dense5, "Dense5");
476  fromFillPattern.emplace(FillPattern::Dense6, "Dense6");
477  fromFillPattern.emplace(FillPattern::Dense7, "Dense7");
478  fromFillPattern.emplace(FillPattern::Horizontal, "Horizontal");
479  fromFillPattern.emplace(FillPattern::Vertical, "Vertical");
480  fromFillPattern.emplace(FillPattern::Cross, "Cross");
481  fromFillPattern.emplace(FillPattern::BackwardDiagonal, "BackwardDiagonal");
482  fromFillPattern.emplace(FillPattern::ForwardDiagonal, "ForwardDiagonal");
483  fromFillPattern.emplace(FillPattern::DiagonalCross, "DiagonalCross");
484 
485  initSecondMap<FillPattern>(fromFillPattern, toFillPattern);
486 }
487 
488 template<>
489 inline void init<Shape>() {
490  fromShape.emplace(Shape::Rect, "Rect");
491  fromShape.emplace(Shape::RoundedRect, "RoundedRect");
492  fromShape.emplace(Shape::Ellipse, "Ellipse");
493  fromShape.emplace(Shape::Triangle, "Triangle");
494  fromShape.emplace(Shape::Pentagon, "Pentagon");
495  fromShape.emplace(Shape::Hexagon, "Hexagon");
496  fromShape.emplace(Shape::Octagon, "Octagon");
497  fromShape.emplace(Shape::Rhomb, "Rhomb");
498  fromShape.emplace(Shape::Trapeze, "Trapeze");
499  fromShape.emplace(Shape::Parallelogram, "Parallelogram");
500  fromShape.emplace(Shape::InvTriangle, "InvTriangle");
501  fromShape.emplace(Shape::InvTrapeze, "InvTrapeze");
502  fromShape.emplace(Shape::InvParallelogram, "InvParallelogram");
503  fromShape.emplace(Shape::Image, "Image");
504 
505  initSecondMap<Shape>(fromShape, toShape);
506  toShape.emplace("rectangle", Shape::Rect);
507  toShape.emplace("box", Shape::Image);
508 }
509 
510 template<class ToClass>
511 inline std::map<string, ToClass>* getMapToEnum() {
512  std::cout << "getMapToEnum was wrongly called\n";
513  OGDF_ASSERT(false);
514  return nullptr;
515 };
516 
517 template<class FromClass>
518 inline std::map<FromClass, string>* getMapToString() {
519  FromClass fc;
520  std::cout << "getMapToString was wrongly called " << typeid(fc).name() << "\n";
521  OGDF_ASSERT(false);
522  return nullptr;
523 };
524 
525 template<>
526 inline std::map<string, FillPattern>* getMapToEnum() {
527  return &toFillPattern;
528 };
529 
530 template<>
531 inline std::map<string, Shape>* getMapToEnum() {
532  return &toShape;
533 };
534 
535 template<>
536 inline std::map<string, StrokeType>* getMapToEnum() {
537  return &toStrokeType;
538 };
539 
540 template<>
541 inline std::map<FillPattern, string>* getMapToString() {
542  return &fromFillPattern;
543 };
544 
545 template<>
546 inline std::map<Shape, string>* getMapToString() {
547  return &fromShape;
548 };
549 
550 template<>
551 inline std::map<StrokeType, string>* getMapToString() {
552  return &fromStrokeType;
553 };
554 }
555 
556 template<class FromClass>
557 inline string toString(FromClass key) {
558  auto* map = graphics::getMapToString<FromClass>();
559  if (map->empty()) {
560  graphics::init<FromClass>();
561  }
562  auto it = map->find(key);
563  OGDF_ASSERT(it != map->end());
564  return (*it).second;
565 };
566 
567 template<class ToClass>
568 inline ToClass fromString(string key) {
569  auto map = graphics::getMapToEnum<ToClass>();
570  if (map->empty()) {
571  graphics::init<ToClass>();
572  }
573  auto it = map->find(key);
574  if (it != map->end()) {
575  return (*it).second;
576  } else {
577  Logger::slout() << "Encountered invalid " << typeid((*map->begin()).second).name() << ": "
578  << key << " " << map->size() << " " << map->empty() << std::endl;
579  return static_cast<ToClass>(std::numeric_limits<int>::min());
580  }
581 };
582 }
ogdf::Color
Colors represented as RGBA values.
Definition: graphics.h:160
ogdf
The namespace for all OGDF objects.
Definition: AugmentationModule.h:36
ogdf::FillPattern::Dense3
@ Dense3
ogdf::StrokeType::Dashdot
@ Dashdot
line style "dash dot dash dot ..."
ogdf::Fill
Properties of fills.
Definition: graphics.h:418
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:421
ogdf::StrokeType
StrokeType
Line types of strokes.
Definition: graphics.h:46
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:54
ogdf::Fill::Fill
Fill()
Definition: graphics.h:423
ogdf::graphics::toStrokeType
std::map< string, StrokeType > toStrokeType
ogdf::Stroke::Stroke
Stroke(Color c)
Definition: graphics.h:406
ogdf::Color::red
uint8_t red() const
Returns the red component.
Definition: graphics.h:336
ogdf::Fill::Fill
Fill(Color c)
Definition: graphics.h:426
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:323
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:316
ogdf::operator==
bool operator==(const Tuple2< E1, E2 > &t1, const Tuple2< E1, E2 > &t2)
Equality operator for 2-tuples.
Definition: tuples.h:74
ogdf::StrokeLineJoin::Bevel
@ Bevel
ogdf::Shape::Octagon
@ Octagon
octagon
ogdf::Fill::Fill
Fill(Color c, FillPattern pattern)
Definition: graphics.h:428
ogdf::Stroke::m_color
Color m_color
stroke color
Definition: graphics.h:393
ogdf::graphics::init< StrokeType >
void init< StrokeType >()
Definition: graphics.h:456
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:431
ogdf::Shape::RoundedRect
@ RoundedRect
rectangle with rounded corners
ogdf::Shape::Rhomb
@ Rhomb
rhomb (=diamond)
ogdf::graphics::init< FillPattern >
void init< FillPattern >()
Definition: graphics.h:468
ogdf::graphics::toShape
std::map< string, Shape > toShape
ogdf::EdgeArrow::Undefined
@ Undefined
ogdf::graphics::init< Shape >
void init< Shape >()
Definition: graphics.h:489
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:449
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:394
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:377
ogdf::graphics::fromShape
std::map< Shape, string > fromShape
r
int r[]
Definition: hierarchical-ranking.cpp:8
ogdf::Fill::m_bgColor
Color m_bgColor
background color of fill pattern
Definition: graphics.h:420
ogdf::Color::green
uint8_t green() const
Returns the green component.
Definition: graphics.h:339
ogdf::FillPattern::ForwardDiagonal
@ ForwardDiagonal
ogdf::fromString
ToClass fromString(string key)
Definition: graphics.h:568
ogdf::Shape::Parallelogram
@ Parallelogram
parallelogram (slanted to the right)
ogdf::Stroke::m_join
StrokeLineJoin m_join
line-join of the stroke
Definition: graphics.h:397
ogdf::Stroke
Properties of strokes.
Definition: graphics.h:392
ogdf::Color::Name
Name
Named colors (same as SVG color keywords).
Definition: graphics.h:165
ogdf::StrokeLineJoin::Round
@ Round
ogdf::StrokeLineJoin
StrokeLineJoin
Line join types of strokes.
Definition: graphics.h:75
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:342
ogdf::Color::alpha
void alpha(uint8_t a)
Sets the alpha channel to a.
Definition: graphics.h:357
ogdf::Color::m_green
uint8_t m_green
Definition: graphics.h:161
ogdf::operator<<
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:978
ogdf::Stroke::m_type
StrokeType m_type
stroke type (e.g.
Definition: graphics.h:395
ogdf::Color::green
void green(uint8_t g)
Sets the green component to g.
Definition: graphics.h:351
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:518
ogdf::Color::Color
Color(const char *str)
Crates a color from string str.
Definition: graphics.h:333
ogdf::StrokeType::None
@ None
no line
ogdf::EdgeArrow
EdgeArrow
Types for edge arrows.
Definition: graphics.h:141
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:330
ogdf::Color::blue
void blue(uint8_t b)
Sets the blue component to b.
Definition: graphics.h:354
ogdf::Shape::InvParallelogram
@ InvParallelogram
inverted parallelogram (slanted to the left)
ogdf::Color::alpha
uint8_t alpha() const
Returns the alpha channel.
Definition: graphics.h:345
ogdf::graphics::init
void init()
Definition: graphics.h:446
ogdf::Color::m_blue
uint8_t m_blue
Definition: graphics.h:161
ogdf::toString
string toString(FromClass key)
Definition: graphics.h:557
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:382
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:419
ogdf::Color::m_red
uint8_t m_red
Definition: graphics.h:161
ogdf::Shape::Rect
@ Rect
rectangle
ogdf::Shape
Shape
Types for node shapes.
Definition: graphics.h:116
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:319
ogdf::FillPattern::Horizontal
@ Horizontal
ogdf::Color::m_alpha
uint8_t m_alpha
Definition: graphics.h:161
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:68
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:385
ogdf::intToFillPattern
FillPattern intToFillPattern(int i)
Converts integer i to fill pattern.
ogdf::Stroke::Stroke
Stroke()
Definition: graphics.h:399
ogdf::Shape::Hexagon
@ Hexagon
hexagon
ogdf::FillPattern
FillPattern
Fill patterns.
Definition: graphics.h:82
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:348
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:191
ogdf::Stroke::m_cap
StrokeLineCap m_cap
line-cap of the stroke
Definition: graphics.h:396
ogdf::graphics::getMapToEnum
std::map< string, ToClass > * getMapToEnum()
Definition: graphics.h:511