fw4spl
src/fwData/Color.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-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 "fwData/registry/macros.hpp"
8 #include "fwData/Exception.hpp"
9 #include "fwData/Color.hpp"
10 
11 #include <fwCore/base.hpp>
12 
13 fwDataRegisterMacro( ::fwData::Color );
14 
15 namespace fwData
16 {
17 
18 //------------------------------------------------------------------------------
19 
21 {
22  m_vRGBA.fill(1.0);
23 }
24 
25 //------------------------------------------------------------------------------
26 
27 Color::sptr Color::ColorFactory(Color::ColorType red, Color::ColorType green, Color::ColorType blue,
28  Color::ColorType alpha)
29 {
30  Color::sptr color = ::fwData::Color::New();
31  color->m_vRGBA = {red, green, blue, alpha};
32  return color;
33 }
34 
35 //------------------------------------------------------------------------------
36 
38 {
39 }
40 
41 //------------------------------------------------------------------------------
42 
43 void Color::shallowCopy(const Object::csptr &_source )
44 {
45  Color::csptr other = Color::dynamicConstCast(_source);
46  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
47  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
48  + " to " + this->getClassname()), !bool(other) );
49  this->fieldShallowCopy( _source );
50  m_vRGBA = other->m_vRGBA;
51 }
52 
53 //------------------------------------------------------------------------------
54 
55 void Color::cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache)
56 {
57  Color::csptr other = Color::dynamicConstCast(_source);
58  FW_RAISE_EXCEPTION_IF( ::fwData::Exception(
59  "Unable to copy" + (_source ? _source->getClassname() : std::string("<NULL>"))
60  + " to " + this->getClassname()), !bool(other) );
61  this->fieldDeepCopy( _source, cache );
62  m_vRGBA = other->m_vRGBA;
63 }
64 
65 //------------------------------------------------------------------------------
66 
67 void Color::setRGBA( const ColorType red, const ColorType green, const ColorType blue, const ColorType alpha )
68 {
69  m_vRGBA = {red, green, blue, alpha};
70 }
71 
72 //------------------------------------------------------------------------------
73 
74 void Color::setRGBA( std::string hexaColor )
75 {
77  "Color string should start with '#' and followed by 6 ou 8 "
78  "hexadecimal digits. Given color : " << hexaColor,
79  hexaColor[0] == '#'
80  && ( hexaColor.length() == 7 || hexaColor.length() == 9)
81  );
82 
83  std::string redString = hexaColor.substr(1, 2);
84  std::string greenString = hexaColor.substr(3, 2);
85  std::string blueString = hexaColor.substr(5, 2);
86  int r,g,b, a = 255;
87 
88  std::istringstream iss;
89  iss.str (redString);
90  iss >> std::hex >> r;
91  iss.clear();
92  iss.str (greenString);
93  iss >> std::hex >> g;
94  iss.clear();
95  iss.str (blueString);
96  iss >> std::hex >> b;
97 
98  if (hexaColor.length() == 9)
99  {
100  std::string alphaString = hexaColor.substr(7, 2);
101  iss.clear();
102  iss.str (alphaString);
103  iss >> std::hex >> a;
104  }
105 
106  this->setRGBA(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
107 }
108 
109 //------------------------------------------------------------------------------
110 
111 Color::ColorType &Color::red()
112 {
113  return m_vRGBA[0];
114 }
115 
116 Color::ColorType &Color::green()
117 {
118  return m_vRGBA[1];
119 }
120 
121 Color::ColorType &Color::blue()
122 {
123  return m_vRGBA[2];
124 }
125 
126 Color::ColorType &Color::alpha()
127 {
128  return m_vRGBA[3];
129 }
130 
131 const Color::ColorType &Color::red() const
132 {
133  return m_vRGBA[0];
134 }
135 
136 const Color::ColorType &Color::green() const
137 {
138  return m_vRGBA[1];
139 }
140 
141 const Color::ColorType &Color::blue() const
142 {
143  return m_vRGBA[2];
144 }
145 
146 const Color::ColorType &Color::alpha() const
147 {
148  return m_vRGBA[3];
149 }
150 
151 //------------------------------------------------------------------------------
152 
153 } // namespace fwData
154 
155 
virtual FWDATA_API ~Color()
Destructor.
#define OSLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:310
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Implements data exception class.
ColorArray m_vRGBA
RGBA of the image (in terms of points)
virtual const std::string & getClassname() const override
return full object&#39;s classname with its namespace, i.e. fwCore::BaseObject
This class defines color object.
FWDATA_API void fieldDeepCopy(const ::fwData::Object::csptr &source)
A deep copy of fields (objects in m_children)
FWDATA_API Color(::fwData::Object::Key key)
Constructor.
void setRGBA(const ColorArray &_vRGBA)
FWDATA_API void cachedDeepCopy(const Object::csptr &_source, DeepCopyCacheType &cache) override
Defines deep copy.
FWDATA_API void shallowCopy(const Object::csptr &_source) override
Defines shallow copy.
Contains the representation of the data objects used in the framework.
FWDATA_API void fieldShallowCopy(const ::fwData::Object::csptr &source)
A shallow copy of fields (objects in m_children)