color.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2017 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "cartographer/io/color.h"
18 
19 #include <cmath>
20 
22 #include "glog/logging.h"
23 
24 namespace cartographer {
25 namespace io {
26 
27 namespace {
28 
29 constexpr float kInitialHue = 0.69f;
30 constexpr float kSaturation = 0.85f;
31 constexpr float kValue = 0.77f;
32 
33 FloatColor HsvToRgb(const float h, const float s, const float v) {
34  const float h_6 = (h == 1.f) ? 0.f : 6 * h;
35  const int h_i = std::floor(h_6);
36  const float f = h_6 - h_i;
37 
38  const float p = v * (1.f - s);
39  const float q = v * (1.f - f * s);
40  const float t = v * (1.f - (1.f - f) * s);
41 
42  if (h_i == 0) {
43  return {{v, t, p}};
44  } else if (h_i == 1) {
45  return {{q, v, p}};
46  } else if (h_i == 2) {
47  return {{p, v, t}};
48  } else if (h_i == 3) {
49  return {{p, q, v}};
50  } else if (h_i == 4) {
51  return {{t, p, v}};
52  } else if (h_i == 5) {
53  return {{v, p, q}};
54  } else {
55  return {{0.f, 0.f, 0.f}};
56  }
57 }
58 
59 } // namespace
60 
62  CHECK_GE(id, 0);
63  // Uniform color sampling using the golden ratio from
64  // http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
65  constexpr float kGoldenRatioConjugate = 0.6180339887498949f;
66  const float hue = std::fmod(kInitialHue + kGoldenRatioConjugate * id, 1.f);
67  return HsvToRgb(hue, kSaturation, kValue);
68 }
69 
70 } // namespace io
71 } // namespace cartographer
FloatColor GetColor(int id)
Definition: color.cc:61
std::array< float, 3 > FloatColor
Definition: color.h:29


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58