Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

graphics.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/basic.h>
35 #include <map>
36 #include <typeinfo>
37 #include <ogdf/basic/Logger.h>
38 
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 
69 
71 
75 
76 
78 
81 enum class StrokeLineCap : unsigned char{
82  Butt,
83  Round,
84  Square
85 };
86 
87 
89 
92 enum class StrokeLineJoin : unsigned char {
93  Miter,
94  Round,
95  Bevel
96 };
97 
98 
100 
103 enum class FillPattern {
104  None,
105  Solid,
106  Dense1,
107  Dense2,
108  Dense3,
109  Dense4,
110  Dense5,
111  Dense6,
112  Dense7,
113  Horizontal,
114  Vertical,
115  Cross,
119 };
120 
122 
124 
128 
130 
134 
136 OGDF_EXPORT std::ostream &operator<<(std::ostream &os, const FillPattern &fp);
137 
139 
143 
144 
146 
149 enum class Shape {
150  Rect,
151  RoundedRect,
152  Ellipse,
153  Triangle,
154  Pentagon,
155  Hexagon,
156  Octagon,
157  Rhomb,
158  Trapeze,
159  Parallelogram,
160  InvTriangle,
161  InvTrapeze,
163  Image
164 };
165 
167 
171 
173 
177 
179 OGDF_EXPORT std::ostream &operator<<(std::ostream &os, const FillPattern &fp);
180 
181 
183 
186 enum class EdgeArrow {
187  None,
188  Last,
189  First,
190  Both,
191  Undefined
192 };
193 
195 OGDF_EXPORT std::ostream &operator<<(std::ostream &os, const EdgeArrow &ea);
196 
198 
206  uint8_t m_red, m_green, m_blue, m_alpha;
207 
208 public:
210  enum class Name {
211  Aliceblue,
212  Antiquewhite,
213  Aqua,
214  Aquamarine,
215  Azure,
216  Beige,
217  Bisque,
218  Black,
219  Blanchedalmond,
220  Blue,
221  Blueviolet,
222  Brown,
223  Burlywood,
224  Cadetblue,
225  Chartreuse,
226  Chocolate,
227  Coral,
228  Cornflowerblue,
229  Cornsilk,
230  Crimson,
231  Cyan,
232  Darkblue,
233  Darkcyan,
234  Darkgoldenrod,
235  Darkgray,
236  Darkgreen,
237  Darkgrey,
238  Darkkhaki,
239  Darkmagenta,
240  Darkolivegreen,
241  Darkorange,
242  Darkorchid,
243  Darkred,
244  Darksalmon,
245  Darkseagreen,
246  Darkslateblue,
247  Darkslategray,
248  Darkslategrey,
249  Darkturquoise,
250  Darkviolet,
251  Deeppink,
252  Deepskyblue,
253  Dimgray,
254  Dimgrey,
255  Dodgerblue,
256  Firebrick,
257  Floralwhite,
258  Forestgreen,
259  Fuchsia,
260  Gainsboro,
261  Ghostwhite,
262  Gold,
263  Goldenrod,
264  Gray,
265  Green,
266  Greenyellow,
267  Grey,
268  Honeydew,
269  Hotpink,
270  Indianred,
271  Indigo,
272  Ivory,
273  Khaki,
274  Lavender,
275  Lavenderblush,
276  Lawngreen,
277  Lemonchiffon,
278  Lightblue,
279  Lightcoral,
280  Lightcyan,
281  Lightgoldenrodyellow,
282  Lightgray,
283  Lightgreen,
284  Lightgrey,
285  Lightpink,
286  Lightsalmon,
287  Lightseagreen,
288  Lightskyblue,
289  Lightslategray,
290  Lightslategrey,
291  Lightsteelblue,
292  Lightyellow,
293  Lime,
294  Limegreen,
295  Linen,
296  Magenta,
297  Maroon,
298  Mediumaquamarine,
299  Mediumblue,
300  Mediumorchid,
301  Mediumpurple,
302  Mediumseagreen,
303  Mediumslateblue,
304  Mediumspringgreen,
305  Mediumturquoise,
306  Mediumvioletred,
307  Midnightblue,
308  Mintcream,
309  Mistyrose,
310  Moccasin,
311  Navajowhite,
312  Navy,
313  Oldlace,
314  Olive,
315  Olivedrab,
316  Orange,
317  Orangered,
318  Orchid,
319  Palegoldenrod,
320  Palegreen,
321  Paleturquoise,
322  Palevioletred,
323  Papayawhip,
324  Peachpuff,
325  Peru,
326  Pink,
327  Plum,
328  Powderblue,
329  Purple,
330  Red,
331  Rosybrown,
332  Royalblue,
333  Saddlebrown,
334  Salmon,
335  Sandybrown,
336  Seagreen,
337  Seashell,
338  Sienna,
339  Silver,
340  Skyblue,
341  Slateblue,
342  Slategray,
343  Slategrey,
344  Snow,
345  Springgreen,
346  Steelblue,
347  Tan,
348  Teal,
349  Thistle,
350  Tomato,
351  Turquoise,
352  Violet,
353  Wheat,
354  White,
355  Whitesmoke,
356  Yellow,
357  Yellowgreen
358  };
359 
361  Color() : m_red(0), m_green(0), m_blue(0), m_alpha(255) { }
362 
364  Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 255) : m_red(r), m_green(g), m_blue(b), m_alpha(a) { }
365 
367  Color(int r, int g, int b, int a = 255) : m_red((uint8_t)r), m_green((uint8_t)g), m_blue((uint8_t)b), m_alpha((uint8_t)a) { }
368 
370  Color(Color::Name name);
371 
373  Color(const string &str) { fromString(str); }
374 
376  Color(const char *str) { fromString(string(str)); }
377 
379  uint8_t red() const { return m_red; }
380 
382  uint8_t green() const { return m_green; }
383 
385  uint8_t blue() const { return m_blue; }
386 
388  uint8_t alpha() const { return m_alpha; }
389 
391  void red(uint8_t r) { m_red = r; }
392 
394  void green(uint8_t g) { m_green = g; }
395 
397  void blue(uint8_t b) { m_blue = b; }
398 
400  void alpha(uint8_t a) { m_alpha = a; }
401 
403 
408  string toString() const;
409 
411  bool fromString(const string &str);
412 
414  bool operator==(const Color &c) const {
415  return m_red == c.m_red && m_green == c.m_green && m_blue == c.m_blue && m_alpha == c.m_alpha;
416  }
417 
419  bool operator!=(const Color &c) const {
420  return !operator==(c);
421  }
422 
424  friend std::ostream &operator<<(std::ostream &os, const Color &c) {
425  return os << c.toString();
426  }
427 };
428 
429 
431 
436  float m_width;
440 
441  Stroke() : m_color(Color::Name::Black), m_width(1.0f), m_type(StrokeType::Solid), m_cap(StrokeLineCap::Butt), m_join(StrokeLineJoin::Miter) { }
442  Stroke(Color c) : m_color(c), m_width(1.0f), m_type(StrokeType::Solid), m_cap(StrokeLineCap::Butt), m_join(StrokeLineJoin::Miter) { }
443 };
444 
445 
447 
454 
455  Fill() : m_color(Color::Name::White), m_bgColor(Color::Name::Black), m_pattern(FillPattern::Solid) { }
456  Fill(Color c) : m_color(c), m_bgColor(Color::Name::Black), m_pattern(FillPattern::Solid) { }
457  Fill(Color c, FillPattern pattern) : m_color(c), m_bgColor(Color::Name::Black), m_pattern(pattern) { }
458  Fill(Color c, Color bgColor, FillPattern pattern) : m_color(c), m_bgColor(bgColor), m_pattern(pattern) { }
459 };
460 
461 namespace graphics {
462  extern OGDF_EXPORT std::map<Shape, string> fromShape;
463  extern OGDF_EXPORT std::map<string, Shape> toShape;
464 
465  extern OGDF_EXPORT std::map<StrokeType, string> fromStrokeType;
466  extern OGDF_EXPORT std::map<string, StrokeType> toStrokeType;
467 
468  extern OGDF_EXPORT std::map<FillPattern, string> fromFillPattern;
469  extern OGDF_EXPORT std::map<string, FillPattern> toFillPattern;
470 
471  template<class Enum>
472  inline void init() {};
473 
474  template<class Enum>
475  inline void initSecondMap(std::map<Enum, string> &fromMap, std::map<string, Enum> &toMap) {
476  for(auto it : fromMap) {
477  toMap.emplace(it.second, it.first);
478  }
479  }
480 
481 
482  template<>
483  inline void init<StrokeType>() {
484  fromStrokeType.emplace(StrokeType::None, "None");
485  fromStrokeType.emplace(StrokeType::Solid, "Solid");
486  fromStrokeType.emplace(StrokeType::Dash, "Dash");
487  fromStrokeType.emplace(StrokeType::Dot, "Dot");
488  fromStrokeType.emplace(StrokeType::Dashdot, "Dashdot");
489  fromStrokeType.emplace(StrokeType::Dashdotdot, "Dashdotdot");
490 
491  initSecondMap<StrokeType>(fromStrokeType, toStrokeType);
492  }
493 
494  template<>
495  inline void init<FillPattern>() {
496  fromFillPattern.emplace(FillPattern::None, "None");
497  fromFillPattern.emplace(FillPattern::Solid, "Solid");
498  fromFillPattern.emplace(FillPattern::Dense1, "Dense1");
499  fromFillPattern.emplace(FillPattern::Dense2, "Dense2");
500  fromFillPattern.emplace(FillPattern::Dense3, "Dense3");
501  fromFillPattern.emplace(FillPattern::Dense4, "Dense4");
502  fromFillPattern.emplace(FillPattern::Dense5, "Dense5");
503  fromFillPattern.emplace(FillPattern::Dense6, "Dense6");
504  fromFillPattern.emplace(FillPattern::Dense7, "Dense7");
505  fromFillPattern.emplace(FillPattern::Horizontal, "Horizontal");
506  fromFillPattern.emplace(FillPattern::Vertical, "Vertical");
507  fromFillPattern.emplace(FillPattern::Cross, "Cross");
508  fromFillPattern.emplace(FillPattern::BackwardDiagonal, "BackwardDiagonal");
509  fromFillPattern.emplace(FillPattern::ForwardDiagonal, "ForwardDiagonal");
510  fromFillPattern.emplace(FillPattern::DiagonalCross, "DiagonalCross");
511 
512  initSecondMap<FillPattern>(fromFillPattern, toFillPattern);
513  }
514 
515  template<>
516  inline void init<Shape>() {
517  fromShape.emplace(Shape::Rect, "Rect");
518  fromShape.emplace(Shape::RoundedRect, "RoundedRect");
519  fromShape.emplace(Shape::Ellipse, "Ellipse");
520  fromShape.emplace(Shape::Triangle, "Triangle");
521  fromShape.emplace(Shape::Pentagon, "Pentagon");
522  fromShape.emplace(Shape::Hexagon, "Hexagon");
523  fromShape.emplace(Shape::Octagon, "Octagon");
524  fromShape.emplace(Shape::Rhomb, "Rhomb");
525  fromShape.emplace(Shape::Trapeze, "Trapeze");
526  fromShape.emplace(Shape::Parallelogram, "Parallelogram");
527  fromShape.emplace(Shape::InvTriangle, "InvTriangle");
528  fromShape.emplace(Shape::InvTrapeze, "InvTrapeze");
529  fromShape.emplace(Shape::InvParallelogram, "InvParallelogram");
530  fromShape.emplace(Shape::Image, "Image");
531 
532  initSecondMap<Shape>(fromShape, toShape);
533  toShape.emplace("rectangle", Shape::Rect);
534  toShape.emplace("box", Shape::Image);
535  }
536 
537  template<class ToClass>
538  inline std::map<string, ToClass>* getMapToEnum() {
539  std::cout << "getMapToEnum was wrongly called\n";
540  OGDF_ASSERT(false);
541  return nullptr;
542  };
543 
544  template<class FromClass>
545  inline std::map<FromClass, string>* getMapToString() {
546  FromClass fc;
547  std::cout << "getMapToString was wrongly called " << typeid(fc).name() << "\n";
548  OGDF_ASSERT(false);
549  return nullptr;
550  };
551 
552  template<>
553  inline std::map<string, FillPattern> *getMapToEnum() {
554  return &toFillPattern;
555  };
556 
557  template<>
558  inline std::map<string, Shape> *getMapToEnum() {
559  return &toShape;
560  };
561 
562  template<>
563  inline std::map<string, StrokeType> *getMapToEnum() {
564  return &toStrokeType;
565  };
566 
567  template<>
568  inline std::map<FillPattern, string> *getMapToString() {
569  return &fromFillPattern;
570  };
571 
572  template<>
573  inline std::map<Shape, string> *getMapToString() {
574  return &fromShape;
575  };
576 
577  template<>
578  inline std::map<StrokeType, string> *getMapToString() {
579  return &fromStrokeType;
580  };
581 }
582 
583 template<class FromClass>
584 inline string toString(FromClass key) {
585  auto *map = graphics::getMapToString<FromClass>();
586  if(map->empty()) {
587  graphics::init<FromClass>();
588  }
589  auto it = map->find(key);
590  OGDF_ASSERT(it != map->end());
591  return (*it).second;
592 };
593 
594 template<class ToClass>
595 inline ToClass fromString(string key) {
596  auto map = graphics::getMapToEnum<ToClass>();
597  if(map->empty()) {
598  graphics::init<ToClass>();
599  }
600  auto it = map->find(key);
601  if(it != map->end()) {
602  return (*it).second;
603  } else {
604  Logger::slout() << "Encountered invalid " << typeid((*map->begin()).second).name()
605  << ": " << key << " " << map->size() << " " << map->empty() << std::endl;
606  return static_cast<ToClass>(std::numeric_limits<int>::min());
607  }
608 };
609 }
inverted parallelogram (slanted to the left)
uint8_t red() const
Returns the red component.
Definition: graphics.h:379
string toString(FromClass key)
Definition: graphics.h:584
Color(int r, int g, int b, int a=255)
Creates a color from given RGBA-values.
Definition: graphics.h:367
Stroke(Color c)
Definition: graphics.h:442
std::map< Shape, string > fromShape
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
ToClass fromString(string key)
Definition: graphics.h:595
std::map< StrokeType, string > * getMapToString()
Definition: graphics.h:578
parallelogram (slanted to the right)
trapeze (upper side shorter)
Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255)
Creates a color from given RGBA-values.
Definition: graphics.h:364
StrokeType stringToStrokeType(string s)
Converts string s to stroke type.
Fill(Color c)
Definition: graphics.h:456
Color m_color
fill color
Definition: graphics.h:451
StrokeLineCap
Line cap types of strokes.
Definition: graphics.h:81
line style "dash dot dot dash dot dot ..."
StrokeLineJoin
Line join types of strokes.
Definition: graphics.h:92
static std::ostream & slout(Level level=Level::Default)
stream for logging-output (global)
Definition: Logger.h:168
uint8_t m_red
Definition: graphics.h:206
void init()
Definition: graphics.h:472
FillPattern
Fill patterns.
Definition: graphics.h:103
EdgeArrow
Types for edge arrows.
Definition: graphics.h:186
void initSecondMap(std::map< Enum, string > &fromMap, std::map< string, Enum > &toMap)
Definition: graphics.h:475
isosceles triangle (base side down)
uint8_t green() const
Returns the green component.
Definition: graphics.h:382
edge arrow at target and source node of the edge
rectangle with rounded corners
float m_width
stroke width
Definition: graphics.h:436
Properties of strokes.
Definition: graphics.h:434
string toString() const
Converts the color to a string and returns it.
FillPattern m_pattern
fill pattern
Definition: graphics.h:453
std::map< FillPattern, string > fromFillPattern
Shape
Types for node shapes.
Definition: graphics.h:149
string strokeTypeToString(StrokeType st)
Converts string s to stroke type.
Fill(Color c, FillPattern pattern)
Definition: graphics.h:457
StrokeLineJoin m_join
line-join of the stroke
Definition: graphics.h:439
uint8_t alpha() const
Returns the alpha channel.
Definition: graphics.h:388
Contains logging functionality.
isosceles triangle (base side up)
void init< FillPattern >()
Definition: graphics.h:495
bool operator!=(const Color &c) const
Returns true iff c and this color differ in any component.
Definition: graphics.h:419
string fillPatternToString(FillPattern fp)
Converts fillpattern fp to string.
Colors represented as RGBA values.
Definition: graphics.h:205
std::map< string, StrokeType > * getMapToEnum()
Definition: graphics.h:563
bool operator==(const Color &c) const
Returns true iff c and this color are equal in every component.
Definition: graphics.h:414
FillPattern intToFillPattern(int i)
Converts integer i to fill pattern.
edge arrow at target node of the edge
uint8_t m_blue
Definition: graphics.h:206
Color(const string &str)
Crates a color from string str.
Definition: graphics.h:373
line style "dash dot dash dot ..."
uint8_t blue() const
Returns the blue component.
Definition: graphics.h:385
Color(const char *str)
Crates a color from string str.
Definition: graphics.h:376
void blue(uint8_t b)
Sets the blue component to b.
Definition: graphics.h:397
uint8_t m_alpha
Definition: graphics.h:206
Shape stringToShape(string s)
Converts string s to shape.
StrokeType intToStrokeType(int i)
Converts integer i to stroke type.
Basic declarations, included by all source files.
void initFillPatternHashing()
std::map< StrokeType, string > fromStrokeType
Name
Named colors (same as SVG color keywords).
Definition: graphics.h:210
StrokeType m_type
stroke type (e.g.
Definition: graphics.h:437
Fill(Color c, Color bgColor, FillPattern pattern)
Definition: graphics.h:458
FillPattern stringToFillPattern(string s)
Converts string s to fill pattern.
void green(uint8_t g)
Sets the green component to g.
Definition: graphics.h:394
uint8_t m_green
Definition: graphics.h:206
StrokeType
Line types of strokes.
Definition: graphics.h:46
std::map< string, StrokeType > toStrokeType
edge arrow at source node of the edge
The namespace for all OGDF objects.
friend std::ostream & operator<<(std::ostream &os, const Color &c)
Writes the string representation of color c to output stream os.
Definition: graphics.h:424
void init< Shape >()
Definition: graphics.h:516
Color m_color
stroke color
Definition: graphics.h:435
Properties of fills.
Definition: graphics.h:450
void alpha(uint8_t a)
Sets the alpha channel to a.
Definition: graphics.h:400
Color()
Creates an opaque black color.
Definition: graphics.h:361
void init< StrokeType >()
Definition: graphics.h:483
Color m_bgColor
background color of fill pattern
Definition: graphics.h:452
StrokeLineCap m_cap
line-cap of the stroke
Definition: graphics.h:438
bool operator==(const Tuple2< E1, E2 > &t1, const Tuple2< E1, E2 > &t2)
Equality operator for 2-tuples.
Definition: tuples.h:76
rhomb (=diamond)
string shapeToString(Shape s)
Converts shape s to string.
void red(uint8_t r)
Sets the red component to r.
Definition: graphics.h:391
std::ostream & operator<<(std::ostream &os, const ogdf::Array< E, INDEX > &a)
Prints array a to output stream os.
Definition: Array.h:942
std::map< string, FillPattern > toFillPattern
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
std::map< string, Shape > toShape
inverted trapeze (upper side longer)