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