Visual Servoing Platform  version 3.6.1 under development (2024-05-04)
vpColorBlindFriendlyPalette.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See https://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  * Description:
31  * Real-time 3D point clouds plotter based on the PCL library.
32  *
33 *****************************************************************************/
34 
35 #include <visp3/gui/vpColorBlindFriendlyPalette.h>
36 #include <visp3/core/vpIoTools.h>
37 
38 #if (VISP_CXX_STANDARD > VISP_CXX_STANDARD_98)
39 
40 std::vector<std::string> vpColorBlindFriendlyPalette::s_paletteNames =
41 {
42  "black" ,
43  "orange" ,
44  "sky-blue" ,
45  "green" ,
46  "yellow" ,
47  "blue" ,
48  "vermillon" ,
49  "purple" ,
50  "unknown"
51 };
52 
53 std::vector<vpColor> vpColorBlindFriendlyPalette::s_palette = {
54  vpColor(0,0,0), // Black = 0,
55  vpColor(230,159,0), // Orange = 1,
56  vpColor(86,180,233), // SkyBlue = 2,
57  vpColor(0,158,115), // Green = 3,
58  vpColor(240,228,66), // Yellow = 4,
59  vpColor(0,114,178), // Blue = 5,
60  vpColor(213,94,0), // Vermillon = 6,
61  vpColor(204,121,167), // Purple = 7,
62  vpColor(255,255,255) // COUNT = 8
63 };
64 
66  : m_colorID(Palette::COUNT)
67 {
68 
69 }
70 
72  : m_colorID(colorID)
73 {
74 
75 }
76 
78  : m_colorID(Palette::COUNT)
79 {
80  set_fromString(nameColor);
81 }
82 
84 {
85  return m_colorID;
86 }
87 
89 {
90  return s_palette[to_uint(m_colorID)];
91 }
92 
93 std::vector<unsigned char> vpColorBlindFriendlyPalette::to_RGB() const
94 {
95  vpColor color = s_palette[to_uint(m_colorID)];
96  std::vector<unsigned char> v_rgb;
97  v_rgb.push_back(color.R);
98  v_rgb.push_back(color.G);
99  v_rgb.push_back(color.B);
100  return v_rgb;
101 }
102 
104 {
105  vpColor color = s_palette[to_uint(m_colorID)];
106  std::vector<double> v_rgb;
107  v_rgb.push_back((double)color.R / 255.0);
108  v_rgb.push_back((double)color.G / 255.0);
109  v_rgb.push_back((double)color.B / 255.0);
110  return v_rgb;
111 }
112 
113 bool vpColorBlindFriendlyPalette::set_fromString(const std::string &nameColor)
114 {
115  m_colorID = Palette::COUNT;
116  std::string nameLowerCase = nameColor; // vpIoTools::toLowerCase(nameColor);
117  bool wasFound(false);
118  for (unsigned int i = 0; i < to_uint(Palette::COUNT) && !wasFound; i++) {
120  if (to_string(candidate) == nameLowerCase) {
121  m_colorID = candidate;
122  wasFound = true;
123  }
124  }
125  return wasFound;
126 }
127 
129 {
130  std::string nameColor = to_string(m_colorID);
131  return nameColor;
132 }
133 
134 std::string vpColorBlindFriendlyPalette::getAvailableColorsNames(const std::string &prefix, const std::string &separator, const std::string &suffix)
135 {
136  std::string list(prefix);
137  const unsigned int nbAvailableColors = (unsigned int)Palette::COUNT;
138  for (unsigned int i = 0; i < nbAvailableColors - 1; i++) {
139  std::string nameCandidateID = s_paletteNames[i];
140  list += nameCandidateID + separator;
141  }
142  list += s_paletteNames[nbAvailableColors - 1] + suffix;
143  return list;
144 }
145 
146 unsigned int vpColorBlindFriendlyPalette::to_uint(const Palette &colorID)
147 {
148  const unsigned int nbAvailableColors = (unsigned int)Palette::COUNT;
149  unsigned int ID = nbAvailableColors;
150  std::string nameSearchedColor = to_string(colorID);
151  bool wasFound = false;
152  for (unsigned int i = 0; i < nbAvailableColors && !wasFound; i++) {
153  Palette candidate = (Palette)i;
154  if (to_string(candidate) == nameSearchedColor) {
155  ID = i;
156  wasFound = true;
157  }
158  }
159  return ID;
160 }
161 
163 {
164  std::string nameColor;
165  switch (colorID) {
166  case Palette::Black:
167  nameColor = s_paletteNames[0];
168  break;
169  case Palette::Orange:
170  nameColor = s_paletteNames[1];
171  break;
172  case Palette::SkyBlue:
173  nameColor = s_paletteNames[2];
174  break;
175  case Palette::Green:
176  nameColor = s_paletteNames[3];
177  break;
178  case Palette::Yellow:
179  nameColor = s_paletteNames[4];
180  break;
181  case Palette::Blue:
182  nameColor = s_paletteNames[5];
183  break;
184  case Palette::Vermillon:
185  nameColor = s_paletteNames[6];
186  break;
187  case Palette::Purple:
188  nameColor = s_paletteNames[7];
189  break;
190  default:
191  nameColor = s_paletteNames[8];
192  }
193  return nameColor;
194 }
195 
196 std::ostream &operator<<(std::ostream &os, const vpColorBlindFriendlyPalette &color)
197 {
198  os << color.to_string();
199  return os;
200 }
201 
202 std::istream &operator>>(std::istream &is, vpColorBlindFriendlyPalette &color)
203 {
204  std::string nameColor;
205  is >> nameColor;
206  color.set_fromString(nameColor);
207  return is;
208 }
209 
210 #else
211 void dummy_vpColorBlindFriendlyPalette() { }
212 #endif
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:600
Class that furnishes a set of colors that color blind people should be able to distinguish one from a...
std::vector< unsigned char > to_RGB() const
Cast a vpColorBlindFriendlyPalette in a vector {R, G, B}. A vpColorBlindFriendlyPalette::Palette::COU...
static std::vector< std::string > s_paletteNames
Palette
Enum that list the different available colors.
std::string to_string() const
Get the name of the vpColorBlindFriendlyPalette object.
vpColor to_vpColor() const
Cast a vpColorBlindFriendlyPalette in a vpColor object. A vpColorBlindFriendlyPalette::Palette::COUNT...
static std::string getAvailableColorsNames(const std::string &prefix="", const std::string &separator=" ", const std::string &suffix="")
Get the list of available colors names.
bool set_fromString(const std::string &nameColor)
Set the fromString object.
std::vector< double > to_colorRatio() const
Cast the object in a vector of doubles that belong to the range [0; 1]. The initial R,...
Palette get_colorID() const
Get the vpColorBlindFriendlyPalette::Palette the object corresponds to.
vpColorBlindFriendlyPalette()
Construct a new vp Color Blind Friendly Palette. The default value vpColorBlindFriendlyPalette::Palet...
Class to define RGB colors available for display functionalities.
Definition: vpColor.h:152
unsigned char B
Blue component.
Definition: vpRGBa.h:139
unsigned char R
Red component.
Definition: vpRGBa.h:137
unsigned char G
Green component.
Definition: vpRGBa.h:138