1 //===-- xray-graph.h - XRay Function Call Graph Renderer --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A class to get a color from a specified gradient.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef XRAY_COLOR_HELPER_H
14 #define XRAY_COLOR_HELPER_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include <tuple>
18 
19 namespace llvm {
20 namespace xray {
21 
22 /// The color helper class it a healper class which allows you to easily get a
23 /// color in a gradient. This is used to color-code edges in XRay-Graph tools.
24 ///
25 /// There are two types of color schemes in this class:
26 ///   - Sequential schemes, which are used to represent information from some
27 ///     minimum to some maximum. These take an input in the range [0,1]
28 ///   - Diverging schemes, which are used to represent information representing
29 ///     differenes, or a range that goes from negative to positive. These take
30 ///     an input in the range [-1,1].
31 /// Usage;
32 /// ColorHelper S(ColorHelper::SequentialScheme::OrRd); //Chose a color scheme.
33 /// for (double p = 0.0; p <= 1; p += 0.1){
34 ///   cout() << S.getColor(p) << " \n"; // Sample the gradient at 0.1 intervals
35 /// }
36 ///
37 /// ColorHelper D(ColorHelper::DivergingScheme::Spectral); // Choose a color
38 ///                                                        // scheme.
39 /// for (double p= -1; p <= 1 ; p += 0.1){
40 ///   cout() << D.getColor(p) << " \n"; // sample the gradient at 0.1 intervals
41 /// }
42 class ColorHelper {
43   double MinIn;
44   double MaxIn;
45 
46   ArrayRef<std::tuple<uint8_t, uint8_t, uint8_t>> ColorMap;
47   ArrayRef<std::tuple<uint8_t, uint8_t, uint8_t>> BoundMap;
48 
49 public:
50   /// Enum of the availible Sequential Color Schemes
51   enum class SequentialScheme {
52     // Schemes based on the ColorBrewer Color schemes of the same name from
53     // http://www.colorbrewer.org/ by Cynthis A Brewer Penn State University.
54     Greys,
55     OrRd,
56     PuBu
57   };
58 
59   ColorHelper(SequentialScheme S);
60 
61   /// Enum of the availible Diverging Color Schemes
62   enum class DivergingScheme {
63     // Schemes based on the ColorBrewer Color schemes of the same name from
64     // http://www.colorbrewer.org/ by Cynthis A Brewer Penn State University.
65     PiYG
66   };
67 
68   ColorHelper(DivergingScheme S);
69 
70   // Sample the gradient at the input point.
71   std::tuple<uint8_t, uint8_t, uint8_t> getColorTuple(double Point) const;
72 
73   std::string getColorString(double Point) const;
74 
75   // Get the Default color, at the moment allways black.
76   std::tuple<uint8_t, uint8_t, uint8_t> getDefaultColorTuple() const {
77     return std::make_tuple(0, 0, 0);
78   }
79 
80   std::string getDefaultColorString() const { return "black"; }
81 
82   // Convert a tuple to a string
83   static std::string getColorString(std::tuple<uint8_t, uint8_t, uint8_t> t);
84 };
85 } // namespace xray
86 } // namespace llvm
87 #endif
88