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_ros/color.h"
18 
19 #include <cmath>
20 
21 #include "glog/logging.h"
22 
23 namespace cartographer_ros {
24 
25 namespace {
26 
27 constexpr float kInitialHue = 0.69f;
28 constexpr float kSaturation = 0.85f;
29 constexpr float kValue = 0.77f;
30 constexpr float kGoldenRatioConjugate = (std::sqrt(5.f) - 1.f) / 2.f;
31 constexpr float kAlpha = 1.f;
32 
33 ::std_msgs::ColorRGBA CreateRgba(const float r, const float g, const float b) {
34  ::std_msgs::ColorRGBA result;
35  result.r = r;
36  result.g = g;
37  result.b = b;
38  result.a = kAlpha;
39  return result;
40 }
41 
42 ::std_msgs::ColorRGBA HsvToRgb(const float h, const float s, const float v) {
43  const float h_6 = (h == 1.f) ? 0.f : 6 * h;
44  const int h_i = std::floor(h_6);
45  const float f = h_6 - h_i;
46 
47  const float p = v * (1.f - s);
48  const float q = v * (1.f - f * s);
49  const float t = v * (1.f - (1.f - f) * s);
50 
51  if (h_i == 0) {
52  return CreateRgba(v, t, p);
53  } else if (h_i == 1) {
54  return CreateRgba(q, v, p);
55  } else if (h_i == 2) {
56  return CreateRgba(p, v, t);
57  } else if (h_i == 3) {
58  return CreateRgba(p, q, v);
59  } else if (h_i == 4) {
60  return CreateRgba(t, p, v);
61  } else if (h_i == 5) {
62  return CreateRgba(v, p, q);
63  } else {
64  return CreateRgba(0.f, 0.f, 0.f);
65  }
66 }
67 
68 } // namespace
69 
70 ::std_msgs::ColorRGBA GetColor(int id) {
71  CHECK_GE(id, 0);
72  // Uniform color sampling using the golden ratio from
73  // http://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/
74  const float hue = std::fmod(kInitialHue + kGoldenRatioConjugate * id, 1.f);
75  return HsvToRgb(hue, kSaturation, kValue);
76 }
77 
78 } // namespace cartographer_ros
f
::std_msgs::ColorRGBA GetColor(int id)
Definition: color.cc:70


cartographer_ros
Author(s):
autogenerated on Wed Jun 5 2019 22:35:56