* Map a given value to a RGB value in the given colormap. */
| 1186 | * Map a given value <x> to a RGB value in the given colormap. |
| 1187 | */ |
| 1188 | std::array<double, 3> mapToColor(double x, double min, double max, const std::string& colormap_name) { |
| 1189 | |
| 1190 | // Normalize value to [0,1] range. |
| 1191 | x = std::max(std::min(x, max), min); |
| 1192 | x = (x - min) / (max - min); |
| 1193 | |
| 1194 | // Determine the colormap. |
| 1195 | std::vector<std::pair<double, Vector3>> colormap; |
| 1196 | if (colormap_name == "coolwarm") { |
| 1197 | colormap = COOLWARM; |
| 1198 | } else if (colormap_name == "seismic") { |
| 1199 | colormap = SEISMIC; |
| 1200 | } else if (colormap_name == "hot") { |
| 1201 | colormap = HOT; |
| 1202 | } |
| 1203 | |
| 1204 | // Find the two colors in the colormap that x falls in between. |
| 1205 | int v1 = 1; |
| 1206 | while (colormap[v1].first < x) { |
| 1207 | v1 += 1; |
| 1208 | } |
| 1209 | v1 -= 1; |
| 1210 | int v2 = v1 + 1; |
| 1211 | |
| 1212 | Vector3 c1 = colormap[v1].second; |
| 1213 | Vector3 c2 = colormap[v2].second; |
| 1214 | |
| 1215 | // Interpolate between the two colors. |
| 1216 | Vector3 c = c1 + (c2 - c1) * (x - colormap[v1].first) / (colormap[v2].first - colormap[v1].first); |
| 1217 | std::array<double, 3> color = {c[0], c[1], c[2]}; |
| 1218 | return color; |
| 1219 | } |
| 1220 | |
| 1221 | /* |
| 1222 | * Computes the hue, saturation, and value of the RGB color model. |
no outgoing calls
no test coverage detected