Point Cloud Library (PCL)  1.14.0-dev
colors.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2014-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #pragma once
39 
40 #include <pcl/pcl_macros.h>
41 #include <pcl/point_types.h>
42 
43 #include <type_traits> // for is_floating_point
44 #include <array> // for std::array especially in Clang Darwin and MSVC
45 
46 namespace pcl
47 {
48 
49  PCL_EXPORTS RGB
50  getRandomColor (double min = 0.2, double max = 2.8);
51 
53  {
54  /** Color lookup table consisting of 256 colors structured in a maximally
55  * discontinuous manner. Generated using the method of Glasbey et al.
56  * (see https://github.com/taketwo/glasbey) */
58  /** A perceptually uniform colormap created by Stéfan van der Walt and
59  * Nathaniel Smith for the Python matplotlib library.
60  * (see https://youtu.be/xAoljeRJ3lU for background and overview) */
62  };
63 
64  template <ColorLUTName T>
65  class ColorLUT
66  {
67 
68  public:
69 
70  /** Get a color from the lookup table with a given id.
71  *
72  * The id should be less than the size of the LUT (see size()). */
73  static RGB at (std::size_t color_id);
74 
75  /** Get the number of colors in the lookup table.
76  *
77  * Note: the number of colors is different from the number of elements
78  * in the lookup table (each color is defined by three bytes). */
79  static std::size_t size ();
80 
81  /** Get a raw pointer to the lookup table. */
82  static const unsigned char* data ();
83 
84  };
85 
88 
89  /**
90  * @brief Returns a Look-Up Table useful in converting RGB to sRGB
91  * @tparam T floating point type with resultant value
92  * @tparam bits depth of RGB
93  * @return 1-D LUT for converting R, G or B into Rs, Gs or Bs
94  * @remarks sRGB was proposed by Stokes et al. as a uniform default color
95  * space for the internet
96  * M. Stokes, M. Anderson, S. Chandrasekar, and R. Motta: A standard default colorspace for the internet - sRGB (Nov 1996)
97  * IEC 61966-2.1 Default RGB Colour Space - sRGB (International Electrotechnical Commission, Geneva, Switzerland, 1999)
98  * www.srgb.com, www.color.org/srgb.html
99  */
100  template <typename T, std::uint8_t bits = 8>
101  PCL_EXPORTS inline std::array<T, 1 << bits>
102  RGB2sRGB_LUT() noexcept
103  {
104  static_assert(std::is_floating_point<T>::value, "LUT value must be a floating point");
105 
106  constexpr const std::size_t size = 1 << bits;
107 
108  static const auto sRGB_LUT = [&]() {
109  // MSVC wouldn't take `size` here instead of the expression
110  std::array<T, 1 << bits> LUT;
111  for (std::size_t i = 0; i < size; ++i) {
112  T f = static_cast<T>(i) / static_cast<T>(size - 1);
113  if (f > 0.04045) {
114  // ((f + 0.055)/1.055)^2.4
115  LUT[i] = static_cast<T>(
116  std::pow((f + static_cast<T>(0.055)) / static_cast<T>(1.055),
117  static_cast<T>(2.4)));
118  }
119  else {
120  // f / 12.92
121  LUT[i] = f / static_cast<T>(12.92);
122  }
123  }
124  return LUT;
125  }();
126  return sRGB_LUT;
127  }
128 
129  /**
130  * @brief Returns a Look-Up Table useful in converting scaled CIE XYZ into CIE L*a*b*
131  * @details The function assumes that the XYZ values are
132  * * not normalized using reference illuminant
133  * * scaled such that reference illuminant has Xn = Yn = Zn = discretizations
134  * @tparam T floating point type with resultant value
135  * @tparam discretizations number of levels for the LUT
136  * @return 1-D LUT with results of f(X/Xn)
137  * @note This function doesn't convert from CIE XYZ to CIE L*a*b*. The actual conversion
138  * is as follows:
139  * L* = 116 * [f(Y/Yn) - 16/116]
140  * a* = 500 * [f(X/Xn) - f(Y/Yn)]
141  * b* = 200 * [f(Y/Yn) - f(Z/Zn)]
142  * Where, Xn, Yn and Zn are values of the reference illuminant (at prescribed angle)
143  * f is appropriate function such that L* = 100, a* = b* = 0 for white color
144  * Reference: Billmeyer and Saltzman’s Principles of Color Technology
145  */
146  template <typename T, std::size_t discretizations = 4000>
147  PCL_EXPORTS inline const std::array<T, discretizations>&
148  XYZ2LAB_LUT() noexcept
149  {
150  static_assert(std::is_floating_point<T>::value, "LUT value must be a floating point");
151 
152  static const auto f_LUT = [&]() {
153  std::array<T, discretizations> LUT;
154  for (std::size_t i = 0; i < discretizations; ++i) {
155  T f = static_cast<T>(i) / static_cast<T>(discretizations);
156  if (f > static_cast<T>(0.008856)) {
157  // f^(1/3)
158  LUT[i] = static_cast<T>(std::pow(f, (static_cast<T>(1) / static_cast<T>(3))));
159  }
160  else {
161  // 7.87 * f + 16/116
162  LUT[i] =
163  static_cast<T>(7.87) * f + (static_cast<T>(16) / static_cast<T>(116));
164  }
165  }
166  return LUT;
167  }();
168  return f_LUT;
169  }
170 } // namespace pcl
static std::size_t size()
Get the number of colors in the lookup table.
static RGB at(std::size_t color_id)
Get a color from the lookup table with a given id.
static const unsigned char * data()
Get a raw pointer to the lookup table.
Defines all the PCL implemented PointT point type structures.
PCL_EXPORTS std::array< T, 1<< bits > RGB2sRGB_LUT() noexcept
Returns a Look-Up Table useful in converting RGB to sRGB.
Definition: colors.h:102
PCL_EXPORTS RGB getRandomColor(double min=0.2, double max=2.8)
ColorLUTName
Definition: colors.h:53
@ LUT_GLASBEY
Color lookup table consisting of 256 colors structured in a maximally discontinuous manner.
Definition: colors.h:57
@ LUT_VIRIDIS
A perceptually uniform colormap created by Stéfan van der Walt and Nathaniel Smith for the Python mat...
Definition: colors.h:61
PCL_EXPORTS const std::array< T, discretizations > & XYZ2LAB_LUT() noexcept
Returns a Look-Up Table useful in converting scaled CIE XYZ into CIE L*a*b*.
Definition: colors.h:148
Defines all the PCL and non-PCL macros used.
#define PCL_EXPORTS
Definition: pcl_macros.h:323
A structure representing RGB color information.