1//===-- xray-graph.cpp: XRay Function Call Graph Renderer -----------------===//
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#include "xray-color-helper.h"
14#include "llvm/Support/FormatVariadic.h"
15#include "llvm/Support/raw_ostream.h"
16
17using namespace llvm;
18using namespace xray;
19
20//  Sequential ColorMaps, which are used to represent information
21//  from some minimum to some maximum.
22
23static const std::tuple<uint8_t, uint8_t, uint8_t> SequentialMaps[][9] = {
24    {// The greys color scheme from http://colorbrewer2.org/
25     std::make_tuple(255, 255, 255), std::make_tuple(240, 240, 240),
26     std::make_tuple(217, 217, 217), std::make_tuple(189, 189, 189),
27     std::make_tuple(150, 150, 150), std::make_tuple(115, 115, 115),
28     std::make_tuple(82, 82, 82), std::make_tuple(37, 37, 37),
29     std::make_tuple(0, 0, 0)},
30    {// The OrRd color scheme from http://colorbrewer2.org/
31     std::make_tuple(255, 247, 236), std::make_tuple(254, 232, 200),
32     std::make_tuple(253, 212, 158), std::make_tuple(253, 187, 132),
33     std::make_tuple(252, 141, 89), std::make_tuple(239, 101, 72),
34     std::make_tuple(215, 48, 31), std::make_tuple(179, 0, 0),
35     std::make_tuple(127, 0, 0)},
36    {// The PuBu color scheme from http://colorbrewer2.org/
37     std::make_tuple(255, 247, 251), std::make_tuple(236, 231, 242),
38     std::make_tuple(208, 209, 230), std::make_tuple(166, 189, 219),
39     std::make_tuple(116, 169, 207), std::make_tuple(54, 144, 192),
40     std::make_tuple(5, 112, 176), std::make_tuple(4, 90, 141),
41     std::make_tuple(2, 56, 88)}};
42
43// Sequential Maps extend the last colors given out of range inputs.
44static const std::tuple<uint8_t, uint8_t, uint8_t> SequentialBounds[][2] = {
45    {// The Bounds for the greys color scheme
46     std::make_tuple(255, 255, 255), std::make_tuple(0, 0, 0)},
47    {// The Bounds for the OrRd color Scheme
48     std::make_tuple(255, 247, 236), std::make_tuple(127, 0, 0)},
49    {// The Bounds for the PuBu color Scheme
50     std::make_tuple(255, 247, 251), std::make_tuple(2, 56, 88)}};
51
52ColorHelper::ColorHelper(ColorHelper::SequentialScheme S)
53    : MinIn(0.0), MaxIn(1.0), ColorMap(SequentialMaps[static_cast<int>(S)]),
54      BoundMap(SequentialBounds[static_cast<int>(S)]) {}
55
56// Diverging ColorMaps, which are used to represent information
57// representing differenes, or a range that goes from negative to positive.
58// These take an input in the range [-1,1].
59
60static const std::tuple<uint8_t, uint8_t, uint8_t> DivergingCoeffs[][11] = {
61    {// The PiYG color scheme from http://colorbrewer2.org/
62     std::make_tuple(142, 1, 82), std::make_tuple(197, 27, 125),
63     std::make_tuple(222, 119, 174), std::make_tuple(241, 182, 218),
64     std::make_tuple(253, 224, 239), std::make_tuple(247, 247, 247),
65     std::make_tuple(230, 245, 208), std::make_tuple(184, 225, 134),
66     std::make_tuple(127, 188, 65), std::make_tuple(77, 146, 33),
67     std::make_tuple(39, 100, 25)}};
68
69// Diverging maps use out of bounds ranges to show missing data. Missing Right
70// Being below min, and missing left being above max.
71static const std::tuple<uint8_t, uint8_t, uint8_t> DivergingBounds[][2] = {
72    {// The PiYG color scheme has green and red for missing right and left
73     // respectively.
74     std::make_tuple(255, 0, 0), std::make_tuple(0, 255, 0)}};
75
76ColorHelper::ColorHelper(ColorHelper::DivergingScheme S)
77    : MinIn(-1.0), MaxIn(1.0), ColorMap(DivergingCoeffs[static_cast<int>(S)]),
78      BoundMap(DivergingBounds[static_cast<int>(S)]) {}
79
80// Takes a tuple of uint8_ts representing a color in RGB and converts them to
81// HSV represented by a tuple of doubles
82static std::tuple<double, double, double>
83convertToHSV(const std::tuple<uint8_t, uint8_t, uint8_t> &Color) {
84  double Scaled[3] = {std::get<0>(Color) / 255.0, std::get<1>(Color) / 255.0,
85                      std::get<2>(Color) / 255.0};
86  int Min = 0;
87  int Max = 0;
88  for (int i = 1; i < 3; ++i) {
89    if (Scaled[i] < Scaled[Min])
90      Min = i;
91    if (Scaled[i] > Scaled[Max])
92      Max = i;
93  }
94
95  double C = Scaled[Max] - Scaled[Min];
96
97  double HPrime =
98      (C == 0) ? 0 : (Scaled[(Max + 1) % 3] - Scaled[(Max + 2) % 3]) / C;
99  HPrime = HPrime + 2.0 * Max;
100
101  double H = (HPrime < 0) ? (HPrime + 6.0) * 60
102                          : HPrime * 60; // Scale to between 0 and 360
103  double V = Scaled[Max];
104
105  double S = (V == 0.0) ? 0.0 : C / V;
106
107  return std::make_tuple(H, S, V);
108}
109
110// Takes a double precision number, clips it between 0 and 1 and then converts
111// that to an integer between 0x00 and 0xFF with proxpper rounding.
112static uint8_t unitIntervalTo8BitChar(double B) {
113  double n = std::max(std::min(B, 1.0), 0.0);
114  return static_cast<uint8_t>(255 * n + 0.5);
115}
116
117// Takes a typle of doubles representing a color in HSV and converts them to
118// RGB represented as a tuple of uint8_ts
119static std::tuple<uint8_t, uint8_t, uint8_t>
120convertToRGB(const std::tuple<double, double, double> &Color) {
121  const double &H = std::get<0>(Color);
122  const double &S = std::get<1>(Color);
123  const double &V = std::get<2>(Color);
124
125  double C = V * S;
126
127  double HPrime = H / 60;
128  double X = C * (1 - std::abs(std::fmod(HPrime, 2.0) - 1));
129
130  double RGB1[3];
131  int HPrimeInt = static_cast<int>(HPrime);
132  if (HPrimeInt % 2 == 0) {
133    RGB1[(HPrimeInt / 2) % 3] = C;
134    RGB1[(HPrimeInt / 2 + 1) % 3] = X;
135    RGB1[(HPrimeInt / 2 + 2) % 3] = 0.0;
136  } else {
137    RGB1[(HPrimeInt / 2) % 3] = X;
138    RGB1[(HPrimeInt / 2 + 1) % 3] = C;
139    RGB1[(HPrimeInt / 2 + 2) % 3] = 0.0;
140  }
141
142  double Min = V - C;
143  double RGB2[3] = {RGB1[0] + Min, RGB1[1] + Min, RGB1[2] + Min};
144
145  return std::make_tuple(unitIntervalTo8BitChar(RGB2[0]),
146                         unitIntervalTo8BitChar(RGB2[1]),
147                         unitIntervalTo8BitChar(RGB2[2]));
148}
149
150// The Hue component of the HSV interpolation Routine
151static double interpolateHue(double H0, double H1, double T) {
152  double D = H1 - H0;
153  if (H0 > H1) {
154    std::swap(H0, H1);
155
156    D = -D;
157    T = 1 - T;
158  }
159
160  if (D <= 180) {
161    return H0 + T * (H1 - H0);
162  } else {
163    H0 = H0 + 360;
164    return std::fmod(H0 + T * (H1 - H0) + 720, 360);
165  }
166}
167
168// Interpolates between two HSV Colors both represented as a tuple of doubles
169// Returns an HSV Color represented as a tuple of doubles
170static std::tuple<double, double, double>
171interpolateHSV(const std::tuple<double, double, double> &C0,
172               const std::tuple<double, double, double> &C1, double T) {
173  double H = interpolateHue(std::get<0>(C0), std::get<0>(C1), T);
174  double S = std::get<1>(C0) + T * (std::get<1>(C1) - std::get<1>(C0));
175  double V = std::get<2>(C0) + T * (std::get<2>(C1) - std::get<2>(C0));
176  return std::make_tuple(H, S, V);
177}
178
179// Get the Color as a tuple of uint8_ts
180std::tuple<uint8_t, uint8_t, uint8_t>
181ColorHelper::getColorTuple(double Point) const {
182  assert(!ColorMap.empty() && "ColorMap must not be empty!");
183  assert(!BoundMap.empty() && "BoundMap must not be empty!");
184
185  if (Point < MinIn)
186    return BoundMap[0];
187  if (Point > MaxIn)
188    return BoundMap[1];
189
190  size_t MaxIndex = ColorMap.size() - 1;
191  double IntervalWidth = MaxIn - MinIn;
192  double OffsetP = Point - MinIn;
193  double SectionWidth = IntervalWidth / static_cast<double>(MaxIndex);
194  size_t SectionNo = std::floor(OffsetP / SectionWidth);
195  double T = (OffsetP - SectionNo * SectionWidth) / SectionWidth;
196
197  auto &RGBColor0 = ColorMap[SectionNo];
198  auto &RGBColor1 = ColorMap[std::min(SectionNo + 1, MaxIndex)];
199
200  auto HSVColor0 = convertToHSV(RGBColor0);
201  auto HSVColor1 = convertToHSV(RGBColor1);
202
203  auto InterpolatedHSVColor = interpolateHSV(HSVColor0, HSVColor1, T);
204  return convertToRGB(InterpolatedHSVColor);
205}
206
207// A helper method to convert a color represented as tuple of uint8s to a hex
208// string.
209std::string
210ColorHelper::getColorString(std::tuple<uint8_t, uint8_t, uint8_t> t) {
211  return llvm::formatv("#{0:X-2}{1:X-2}{2:X-2}", std::get<0>(t), std::get<1>(t),
212                       std::get<2>(t));
213}
214
215// Gets a color in a gradient given a number in the interval [0,1], it does this
216// by evaluating a polynomial which maps [0, 1] -> [0, 1] for each of the R G
217// and B values in the color. It then converts this [0,1] colors to a 24 bit
218// color as a hex string.
219std::string ColorHelper::getColorString(double Point) const {
220  return getColorString(getColorTuple(Point));
221}
222