00001 /* 00002 * Copyright 2017 The Cartographer Authors 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef CARTOGRAPHER_IO_COLOR_H_ 00018 #define CARTOGRAPHER_IO_COLOR_H_ 00019 00020 #include <array> 00021 00022 #include "cartographer/common/math.h" 00023 #include "cartographer/common/port.h" 00024 00025 namespace cartographer { 00026 namespace io { 00027 00028 using Uint8Color = std::array<uint8, 3>; 00029 using FloatColor = std::array<float, 3>; 00030 00031 // A function for on-demand generation of a color palette, with every two 00032 // direct successors having large contrast. 00033 FloatColor GetColor(int id); 00034 00035 inline uint8 FloatComponentToUint8(float c) { 00036 return static_cast<uint8>(cartographer::common::RoundToInt( 00037 cartographer::common::Clamp(c, 0.f, 1.f) * 255)); 00038 } 00039 00040 inline float Uint8ComponentToFloat(uint8 c) { return c / 255.f; } 00041 00042 inline Uint8Color ToUint8Color(const FloatColor& color) { 00043 return {{FloatComponentToUint8(color[0]), FloatComponentToUint8(color[1]), 00044 FloatComponentToUint8(color[2])}}; 00045 } 00046 00047 inline FloatColor ToFloatColor(const Uint8Color& color) { 00048 return {{Uint8ComponentToFloat(color[0]), Uint8ComponentToFloat(color[1]), 00049 Uint8ComponentToFloat(color[2])}}; 00050 } 00051 00052 } // namespace io 00053 } // namespace cartographer 00054 00055 #endif // CARTOGRAPHER_IO_COLOR_H_