Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
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 BEGIN_VISP_NAMESPACE
41 std::vector<std::string> vpColorBlindFriendlyPalette::s_paletteNames =
42 {
43  "black" ,
44  "orange" ,
45  "sky-blue" ,
46  "green" ,
47  "yellow" ,
48  "blue" ,
49  "vermillon" ,
50  "purple" ,
51  "unknown"
52 };
53 
54 std::vector<vpColor> vpColorBlindFriendlyPalette::s_palette = {
55  vpColor(0,0,0), // Black = 0,
56  vpColor(230,159,0), // Orange = 1,
57  vpColor(86,180,233), // SkyBlue = 2,
58  vpColor(0,158,115), // Green = 3,
59  vpColor(240,228,66), // Yellow = 4,
60  vpColor(0,114,178), // Blue = 5,
61  vpColor(213,94,0), // Vermillon = 6,
62  vpColor(204,121,167), // Purple = 7,
63  vpColor(255,255,255) // COUNT = 8
64 };
65 
67  : m_colorID(Palette::COUNT)
68 {
69 
70 }
71 
73  : m_colorID(colorID)
74 {
75 
76 }
77 
79  : m_colorID(Palette::COUNT)
80 {
81  set_fromString(nameColor);
82 }
83 
85 {
86  return m_colorID;
87 }
88 
90 {
91  return s_palette[to_uint(m_colorID)];
92 }
93 
94 std::vector<unsigned char> vpColorBlindFriendlyPalette::to_RGB() const
95 {
96  vpColor color = s_palette[to_uint(m_colorID)];
97  std::vector<unsigned char> v_rgb;
98  v_rgb.push_back(color.R);
99  v_rgb.push_back(color.G);
100  v_rgb.push_back(color.B);
101  return v_rgb;
102 }
103 
105 {
106  vpColor color = s_palette[to_uint(m_colorID)];
107  std::vector<double> v_rgb;
108  v_rgb.push_back((double)color.R / 255.0);
109  v_rgb.push_back((double)color.G / 255.0);
110  v_rgb.push_back((double)color.B / 255.0);
111  return v_rgb;
112 }
113 
114 bool vpColorBlindFriendlyPalette::set_fromString(const std::string &nameColor)
115 {
116  m_colorID = Palette::COUNT;
117  std::string nameLowerCase = nameColor; // vpIoTools::toLowerCase(nameColor);
118  bool wasFound(false);
119  for (unsigned int i = 0; i < to_uint(Palette::COUNT) && !wasFound; i++) {
121  if (to_string(candidate) == nameLowerCase) {
122  m_colorID = candidate;
123  wasFound = true;
124  }
125  }
126  return wasFound;
127 }
128 
130 {
131  std::string nameColor = to_string(m_colorID);
132  return nameColor;
133 }
134 
135 std::string vpColorBlindFriendlyPalette::getAvailableColorsNames(const std::string &prefix, const std::string &separator, const std::string &suffix)
136 {
137  std::string list(prefix);
138  const unsigned int nbAvailableColors = (unsigned int)Palette::COUNT;
139  for (unsigned int i = 0; i < nbAvailableColors - 1; i++) {
140  std::string nameCandidateID = s_paletteNames[i];
141  list += nameCandidateID + separator;
142  }
143  list += s_paletteNames[nbAvailableColors - 1] + suffix;
144  return list;
145 }
146 
147 unsigned int vpColorBlindFriendlyPalette::to_uint(const Palette &colorID)
148 {
149  const unsigned int nbAvailableColors = (unsigned int)Palette::COUNT;
150  unsigned int ID = nbAvailableColors;
151  std::string nameSearchedColor = to_string(colorID);
152  bool wasFound = false;
153  for (unsigned int i = 0; i < nbAvailableColors && !wasFound; i++) {
154  Palette candidate = (Palette)i;
155  if (to_string(candidate) == nameSearchedColor) {
156  ID = i;
157  wasFound = true;
158  }
159  }
160  return ID;
161 }
162 
164 {
165  std::string nameColor;
166  switch (colorID) {
167  case Palette::Black:
168  nameColor = s_paletteNames[0];
169  break;
170  case Palette::Orange:
171  nameColor = s_paletteNames[1];
172  break;
173  case Palette::SkyBlue:
174  nameColor = s_paletteNames[2];
175  break;
176  case Palette::Green:
177  nameColor = s_paletteNames[3];
178  break;
179  case Palette::Yellow:
180  nameColor = s_paletteNames[4];
181  break;
182  case Palette::Blue:
183  nameColor = s_paletteNames[5];
184  break;
185  case Palette::Vermillon:
186  nameColor = s_paletteNames[6];
187  break;
188  case Palette::Purple:
189  nameColor = s_paletteNames[7];
190  break;
191  default:
192  nameColor = s_paletteNames[8];
193  }
194  return nameColor;
195 }
196 
197 END_VISP_NAMESPACE
198 
199 std::ostream &operator<<(std::ostream &os, const VISP_NAMESPACE_ADDRESSING vpColorBlindFriendlyPalette &color)
200 {
201  os << color.to_string();
202  return os;
203 }
204 
205 std::istream &operator>>(std::istream &is, VISP_NAMESPACE_ADDRESSING vpColorBlindFriendlyPalette &color)
206 {
207  std::string nameColor;
208  is >> nameColor;
209  color.set_fromString(nameColor);
210  return is;
211 }
212 #else
213 void dummy_vpColorBlindFriendlyPalette() { }
214 #endif
friend std::ostream & operator<<(std::ostream &s, const vpArray2D< Type > &A)
Definition: vpArray2D.h:614
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...
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.
static std::vector< std::string > s_paletteNames
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:157
unsigned char B
Blue component.
Definition: vpRGBa.h:169
unsigned char R
Red component.
Definition: vpRGBa.h:167
unsigned char G
Green component.
Definition: vpRGBa.h:168