| 67 | constexpr char kUnicodeEllipsis[] = " \u2026 "; |
| 68 | |
| 69 | class Color { |
| 70 | public: |
| 71 | Color() {} |
| 72 | Color(uint8 r, uint8 g, uint8 b) : r_(r), g_(g), b_(b) {} |
| 73 | explicit Color(uint32 word) |
| 74 | : r_((word & 0x00FF0000) >> 16), |
| 75 | g_((word & 0x0000FF00) >> 8), |
| 76 | b_((word & 0x000000FF) >> 0) {} |
| 77 | |
| 78 | // Returns the string serialization of this color in graphviz format, |
| 79 | // for use as 'fillcolor' in boxes. |
| 80 | string AsHexString() const { return StringF("#%.2X%.2X%.2X", r_, g_, b_); } |
| 81 | // The color to use for this node; will be used as 'fillcolor' |
| 82 | // for its box. See Color::AsHexString. A suitable, different |
| 83 | // color will be chosen for the 'fontcolor' for the inside text |
| 84 | // label, see Color::TextColorString. |
| 85 | // Returns the serialization in graphviz format of a suitable color to use |
| 86 | // 'fontcolor' in the same boxes. It should black or white, whichever offers |
| 87 | // the better contrast from AsHexString(). |
| 88 | string TextColorString() const { |
| 89 | // https://en.wikipedia.org/wiki/Relative_luminance |
| 90 | const float luminance = 0.2126f * r_ + 0.7152f * g_ + 0.0722f * b_; |
| 91 | const uint8 l = luminance > 128.f ? 0 : 255; |
| 92 | return StringF("#%.2X%.2X%.2X", l, l, l); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | uint8 r_ = 0, g_ = 0, b_ = 0; |
| 97 | }; |
| 98 | |
| 99 | Color HashStringToColor(string s) { |
| 100 | // Return a unique color for a name. |
no outgoing calls