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 <cmath>
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
23const std::tuple<uint8_t, uint8_t, uint8_t> SequentialMaps[][9] = {
24 {// The greys color scheme from http://colorbrewer2.org/
25 std::make_tuple(args: 255, args: 255, args: 255), std::make_tuple(args: 240, args: 240, args: 240),
26 std::make_tuple(args: 217, args: 217, args: 217), std::make_tuple(args: 189, args: 189, args: 189),
27 std::make_tuple(args: 150, args: 150, args: 150), std::make_tuple(args: 115, args: 115, args: 115),
28 std::make_tuple(args: 82, args: 82, args: 82), std::make_tuple(args: 37, args: 37, args: 37),
29 std::make_tuple(args: 0, args: 0, args: 0)},
30 {// The OrRd color scheme from http://colorbrewer2.org/
31 std::make_tuple(args: 255, args: 247, args: 236), std::make_tuple(args: 254, args: 232, args: 200),
32 std::make_tuple(args: 253, args: 212, args: 158), std::make_tuple(args: 253, args: 187, args: 132),
33 std::make_tuple(args: 252, args: 141, args: 89), std::make_tuple(args: 239, args: 101, args: 72),
34 std::make_tuple(args: 215, args: 48, args: 31), std::make_tuple(args: 179, args: 0, args: 0),
35 std::make_tuple(args: 127, args: 0, args: 0)},
36 {// The PuBu color scheme from http://colorbrewer2.org/
37 std::make_tuple(args: 255, args: 247, args: 251), std::make_tuple(args: 236, args: 231, args: 242),
38 std::make_tuple(args: 208, args: 209, args: 230), std::make_tuple(args: 166, args: 189, args: 219),
39 std::make_tuple(args: 116, args: 169, args: 207), std::make_tuple(args: 54, args: 144, args: 192),
40 std::make_tuple(args: 5, args: 112, args: 176), std::make_tuple(args: 4, args: 90, args: 141),
41 std::make_tuple(args: 2, args: 56, args: 88)}};
42
43// Sequential Maps extend the last colors given out of range inputs.
44const std::tuple<uint8_t, uint8_t, uint8_t> SequentialBounds[][2] = {
45 {// The Bounds for the greys color scheme
46 std::make_tuple(args: 255, args: 255, args: 255), std::make_tuple(args: 0, args: 0, args: 0)},
47 {// The Bounds for the OrRd color Scheme
48 std::make_tuple(args: 255, args: 247, args: 236), std::make_tuple(args: 127, args: 0, args: 0)},
49 {// The Bounds for the PuBu color Scheme
50 std::make_tuple(args: 255, args: 247, args: 251), std::make_tuple(args: 2, args: 56, args: 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
60const std::tuple<uint8_t, uint8_t, uint8_t> DivergingCoeffs[][11] = {
61 {// The PiYG color scheme from http://colorbrewer2.org/
62 std::make_tuple(args: 142, args: 1, args: 82), std::make_tuple(args: 197, args: 27, args: 125),
63 std::make_tuple(args: 222, args: 119, args: 174), std::make_tuple(args: 241, args: 182, args: 218),
64 std::make_tuple(args: 253, args: 224, args: 239), std::make_tuple(args: 247, args: 247, args: 247),
65 std::make_tuple(args: 230, args: 245, args: 208), std::make_tuple(args: 184, args: 225, args: 134),
66 std::make_tuple(args: 127, args: 188, args: 65), std::make_tuple(args: 77, args: 146, args: 33),
67 std::make_tuple(args: 39, args: 100, args: 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.
71const 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(args: 255, args: 0, args: 0), std::make_tuple(args: 0, args: 255, args: 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>(t: Color) / 255.0, std::get<1>(t: Color) / 255.0,
85 std::get<2>(t: 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(args&: H, args&: S, args&: 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::clamp(val: B, lo: 0.0, hi: 1.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>(t: Color);
122 const double &S = std::get<1>(t: Color);
123 const double &V = std::get<2>(t: Color);
124
125 double C = V * S;
126
127 double HPrime = H / 60;
128 double X = C * (1 - std::abs(x: std::fmod(x: HPrime, y: 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(args: unitIntervalTo8BitChar(B: RGB2[0]),
146 args: unitIntervalTo8BitChar(B: RGB2[1]),
147 args: unitIntervalTo8BitChar(B: 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(a&: H0, b&: 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(x: H0 + T * (H1 - H0) + 720, y: 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(H0: std::get<0>(t: C0), H1: std::get<0>(t: C1), T);
174 double S = std::get<1>(t: C0) + T * (std::get<1>(t: C1) - std::get<1>(t: C0));
175 double V = std::get<2>(t: C0) + T * (std::get<2>(t: C1) - std::get<2>(t: C0));
176 return std::make_tuple(args&: H, args&: S, args&: 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(x: OffsetP / SectionWidth);
195 double T = (OffsetP - SectionNo * SectionWidth) / SectionWidth;
196
197 auto &RGBColor0 = ColorMap[SectionNo];
198 auto &RGBColor1 = ColorMap[std::min(a: SectionNo + 1, b: MaxIndex)];
199
200 auto HSVColor0 = convertToHSV(Color: RGBColor0);
201 auto HSVColor1 = convertToHSV(Color: RGBColor1);
202
203 auto InterpolatedHSVColor = interpolateHSV(C0: HSVColor0, C1: HSVColor1, T);
204 return convertToRGB(Color: 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 std::string(llvm::formatv(Fmt: "#{0:X-2}{1:X-2}{2:X-2}", Vals&: std::get<0>(t&: t),
212 Vals&: std::get<1>(t&: t), Vals&: std::get<2>(t&: 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(t: getColorTuple(Point));
221}
222