fw4spl
ools/src/fwDataTools/Color.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2014-2015.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwDataTools/Color.hpp"
8 
9 #include <fwCore/exceptionmacros.hpp>
10 #include "fwCore/spyLog.hpp"
11 
12 #include <sstream>
13 
14 namespace fwDataTools
15 {
16 
17 //------------------------------------------------------------------------------
18 
19 void Color::hexaStringToRGBA( const std::string& _hexaColor, std::uint8_t _rgba[4] )
20 {
21  FW_RAISE_IF(
22  "Color string should start with '#' and followed by 6 ou 8 "
23  "hexadecimal digits. Given color : " + _hexaColor,
24  _hexaColor[0] != '#'
25  || ( _hexaColor.length() != 7 && _hexaColor.length() != 9)
26  );
27 
28  std::string redString = _hexaColor.substr(1, 2);
29  std::string greenString = _hexaColor.substr(3, 2);
30  std::string blueString = _hexaColor.substr(5, 2);
31  int r,g,b,a = 255;
32 
33  std::istringstream iss;
34  iss.str (redString);
35  iss >> std::hex >> r;
36  iss.clear();
37  iss.str (greenString);
38  iss >> std::hex >> g;
39  iss.clear();
40  iss.str (blueString);
41  iss >> std::hex >> b;
42 
43  _rgba[3] = 255;
44  if (_hexaColor.length() == 9)
45  {
46  std::string alphaString = _hexaColor.substr(7, 2);
47  iss.clear();
48  iss.str (alphaString);
49  iss >> std::hex >> a;
50  }
51 
52  _rgba[0] = static_cast< std::uint8_t >(r);
53  _rgba[1] = static_cast< std::uint8_t >(g);
54  _rgba[2] = static_cast< std::uint8_t >(b);
55  _rgba[3] = static_cast< std::uint8_t >(a);
56 }
57 
58 //------------------------------------------------------------------------------
59 
60 } // end namespace fwDataTools
static FWDATATOOLS_API void hexaStringToRGBA(const std::string &_hexaColor, std::uint8_t _rgba[4])
Convert a color coded as an hexadecimal string into an array of four bytes (RGBA) ...
The namespace fwDataTools contains classes which provide helpers to manipulate fwData::Object. *.
This file defines SpyLog macros. These macros are used to log messages to a file or to the console du...