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