PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PColor Class Reference

#include <PColor.h>

Inheritance diagram for PColor:
PEBLObjectBase

Public Member Functions

 PColor ()
 Standard constructor of PColor.
 
 PColor (unsigned int color)
 Constructor.
 
 PColor (int red, int green, int blue, int alpha)
 Constructor.
 
 PColor (const std::string &colorname)
 Constructor.
 
 PColor (const PColor &pcolor)
 Constructor using RGBColorNames.
 
virtual ~PColor ()
 Copy constructor.
 
virtual bool SetProperty (std::string, Variant v)
 Standard Destructor.
 
virtual ObjectValidationError ValidateProperty (std::string name, Variant v) const
 
virtual ObjectValidationError ValidateProperty (std::string name) const
 
int GetRed () const
 
int GetGreen () const
 
int GetBlue () const
 
int GetAlpha () const
 
void SetRed (int color)
 
void SetGreen (int color)
 
void SetBlue (int color)
 
void SetAlpha (int color)
 
void SetColorByRGBA (unsigned int color)
 Sets color as a 32-bit unsigned int.
 
void SetColorByRGBA (int red, int green, int blue, int alpha)
 Sets color as a 4 8-bit unsigned int.
 
void SetColorByName (const std::string &colorname)
 Sets color as by its name.
 
unsigned int GetColor () const
 Gets 32-bit unsigned int color.
 
bool HasChanged () const
 Change detection for nested property modifications.
 
void ClearChanged ()
 
- Public Member Functions inherited from PEBLObjectBase
 PEBLObjectBase ()
 Standard Constructor.
 
 PEBLObjectBase (ComplexDataType cdt)
 Standard Constructor.
 
 PEBLObjectBase (const PEBLObjectBase &pob)
 
virtual ~PEBLObjectBase ()
 Standard Destructor.
 
virtual bool InitializeProperty (std::string name, Variant v)
 
Variant GetProperty (std::string) const
 
ComplexDataType GetType ()
 
virtual std::ostream & PrintProperties (std::ostream &out)
 
virtual Variant GetPropertyList ()
 

Protected Member Functions

virtual std::string ObjectName () const
 
virtual std::ostream & SendToStream (std::ostream &out) const
 This sends the color descriptions to the specified stream.
 

Additional Inherited Members

- Protected Attributes inherited from PEBLObjectBase
ComplexDataType mCDT
 
std::map< std::string, VariantmProperties
 

Detailed Description

This class represents color. Currently, only RGBA color is accessible. Access through other color spaces could be added.

Definition at line 36 of file PColor.h.

Constructor & Destructor Documentation

◆ PColor() [1/5]

PColor::PColor ( )

Standard constructor of PColor.

Definition at line 44 of file PColor.cpp.

44 :
46 mRed(0),
47 mGreen(0),
48 mBlue(0),
49 mAlpha(255),
50 mChanged(false)
51
52{
53
54 InitializeProperty("NAME", Variant("<COLOR>"));
59
60}
@ CDT_COLOR
Definition PEBLObject.h:49
int GetRed() const
Definition PColor.cpp:226
int GetAlpha() const
Definition PColor.cpp:229
int GetBlue() const
Definition PColor.cpp:228
int GetGreen() const
Definition PColor.cpp:227
virtual bool InitializeProperty(std::string name, Variant v)
PEBLObjectBase()
Standard Constructor.

References GetAlpha(), GetBlue(), GetGreen(), GetRed(), and PEBLObjectBase::InitializeProperty().

◆ PColor() [2/5]

PColor::PColor ( unsigned int  color)

Constructor.

Definition at line 62 of file PColor.cpp.

62 :
63 mRed(0),mGreen(0),mBlue(0),mAlpha(0),mChanged(false)
64 {
65 SetColorByRGBA(color);
66 };
void SetColorByRGBA(unsigned int color)
Sets color as a 32-bit unsigned int.
Definition PColor.cpp:235

References SetColorByRGBA().

◆ PColor() [3/5]

PColor::PColor ( int  red,
int  green,
int  blue,
int  alpha 
)

Constructor.

Convenience constructor of PColor:

Definition at line 70 of file PColor.cpp.

70 :
72 mRed(To8BitColor(red)),
73 mGreen(To8BitColor(green)),
74 mBlue(To8BitColor(blue)),
75 mAlpha(To8BitColor(alpha)),
76 mChanged(false)
77
78{
79 InitializeProperty("NAME", Variant("<COLOR>"));
84
85}

References GetAlpha(), GetBlue(), GetGreen(), GetRed(), and PEBLObjectBase::InitializeProperty().

◆ PColor() [4/5]

PColor::PColor ( const std::string &  colorname)

Constructor.

◆ PColor() [5/5]

PColor::PColor ( const PColor pcolor)

Constructor using RGBColorNames.

Copy constructor of PColor:

Definition at line 120 of file PColor.cpp.

120 :
122{
123
124 mRed = pcolor.GetRed();
125 mGreen = pcolor.GetGreen();
126 mBlue = pcolor.GetBlue();
127 mAlpha = pcolor.GetAlpha();
128 mChanged = false;
129
130 InitializeProperty("NAME", Variant("<COLOR>"));
131 InitializeProperty("RED",Variant((int)mRed));
132 InitializeProperty("GREEN",Variant((int)mGreen));
133 InitializeProperty("BLUE",Variant((int)mBlue));
134 InitializeProperty("ALPHA",Variant((int)mAlpha));
135
136}

References GetAlpha(), GetBlue(), GetGreen(), GetRed(), and PEBLObjectBase::InitializeProperty().

◆ ~PColor()

PColor::~PColor ( )
virtual

Copy constructor.

Definition at line 138 of file PColor.cpp.

139{
140}

Member Function Documentation

◆ ClearChanged()

void PColor::ClearChanged ( )
inline

Definition at line 79 of file PColor.h.

79{ mChanged = false; }

Referenced by PlatformFont::ClearChanged().

◆ GetAlpha()

◆ GetBlue()

◆ GetColor()

unsigned int PColor::GetColor ( ) const

Gets 32-bit unsigned int color.

this is a bit clearer and easier to deal with cross-platform.

Definition at line 395 of file PColor.cpp.

396{
397 unsigned int color=0;
398 color = mAlpha + (255 * mBlue) + (255 * 255 * mGreen) + (255 * 255 * 255 * mRed);
399 return color;
400}

Referenced by main(), and ObjectName().

◆ GetGreen()

◆ GetRed()

◆ HasChanged()

bool PColor::HasChanged ( ) const
inline

Change detection for nested property modifications.

Definition at line 78 of file PColor.h.

78{ return mChanged; }

Referenced by PlatformFont::HasChanged().

◆ ObjectName()

std::string PColor::ObjectName ( ) const
protectedvirtual

Reimplemented from PEBLObjectBase.

Definition at line 412 of file PColor.cpp.

413{
414 Variant tmp = "<PColor: Red: [";
415
416 tmp = tmp + Variant((int)mRed) ;
417 tmp = tmp + Variant("] Green: [");
418 tmp = tmp + Variant((int)mGreen);
419 tmp = tmp + Variant("] Blue: [");
420 tmp = tmp + Variant((int)mBlue );
421 tmp = tmp + Variant("] Alpha: [");
422 tmp = tmp + Variant((int)mAlpha );
423 tmp = tmp + Variant("] Color Code: [");
424 tmp = tmp + Variant((long unsigned int)GetColor());
425 tmp = tmp + Variant("]>");
426
427 return tmp.GetString();
428}
unsigned int GetColor() const
Gets 32-bit unsigned int color.
Definition PColor.cpp:395
std::string GetString() const
Definition Variant.cpp:1056

References GetColor(), and Variant::GetString().

Referenced by SendToStream().

◆ SendToStream()

std::ostream & PColor::SendToStream ( std::ostream &  out) const
protectedvirtual

This sends the color descriptions to the specified stream.

Reimplemented from PEBLObjectBase.

Definition at line 406 of file PColor.cpp.

407{
408 out << ObjectName() << std::flush;
409 return out;
410}
virtual std::string ObjectName() const
Definition PColor.cpp:412

References ObjectName().

◆ SetAlpha()

void PColor::SetAlpha ( int  color)

Definition at line 218 of file PColor.cpp.

219{
220 mAlpha = To8BitColor(color);
222 mChanged = true;
223}
virtual bool SetProperty(std::string name, Variant v)

References GetAlpha(), and PEBLObjectBase::SetProperty().

Referenced by main(), PWidget::SetBackgroundColor(), PFont::SetBackgroundColor(), PFont::SetFontColor(), and SetProperty().

◆ SetBlue()

void PColor::SetBlue ( int  color)

Definition at line 211 of file PColor.cpp.

212{
213 mBlue = To8BitColor(color);
215 mChanged = true;
216}

References GetBlue(), and PEBLObjectBase::SetProperty().

Referenced by main(), PWidget::SetBackgroundColor(), PFont::SetBackgroundColor(), PFont::SetFontColor(), and SetProperty().

◆ SetColorByName()

void PColor::SetColorByName ( const std::string &  colorname)

Sets color as by its name.

the RGBNames::Colornames is uppercase and sorted, so use a simple search to find the right one really fast.

Definition at line 257 of file PColor.cpp.

258{
259
260 // Check if this is a hex color code (starts with #)
261 if (!colorname.empty() && colorname[0] == '#')
262 {
263 std::string hexcode = colorname.substr(1); // Remove the '#'
264
265 // Validate that hexcode contains only hex digits (0-9, A-F, a-f)
266 bool valid = true;
267 for (size_t i = 0; i < hexcode.length(); i++) {
268 char c = hexcode[i];
269 if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'))) {
270 valid = false;
271 break;
272 }
273 }
274
275 if (valid && (hexcode.length() == 3 || hexcode.length() == 6 || hexcode.length() == 8))
276 {
277 int r = 0, g = 0, b = 0, a = 255;
278
279 if (hexcode.length() == 3) {
280 // #RGB format - expand to #RRGGBB
281 r = std::stoi(hexcode.substr(0, 1), nullptr, 16) * 17; // 0xF becomes 0xFF
282 g = std::stoi(hexcode.substr(1, 1), nullptr, 16) * 17;
283 b = std::stoi(hexcode.substr(2, 1), nullptr, 16) * 17;
284 }
285 else if (hexcode.length() == 6) {
286 // #RRGGBB format
287 r = std::stoi(hexcode.substr(0, 2), nullptr, 16);
288 g = std::stoi(hexcode.substr(2, 2), nullptr, 16);
289 b = std::stoi(hexcode.substr(4, 2), nullptr, 16);
290 }
291 else if (hexcode.length() == 8) {
292 // #RRGGBBAA format
293 r = std::stoi(hexcode.substr(0, 2), nullptr, 16);
294 g = std::stoi(hexcode.substr(2, 2), nullptr, 16);
295 b = std::stoi(hexcode.substr(4, 2), nullptr, 16);
296 a = std::stoi(hexcode.substr(6, 2), nullptr, 16);
297 }
298
299 mRed = To8BitColor(r);
300 mGreen = To8BitColor(g);
301 mBlue = To8BitColor(b);
302 mAlpha = To8BitColor(a);
303 __SetProps__();
304 return; // Successfully parsed hex color
305 }
306 }
307
308 std::string ucasename = PEBLUtility::ToUpper(colorname);
309
310
313 int bottom = 0;
314 int top = RGBNames::NumRGBColorNames-1;
315 int mid = (top + bottom)/2;
316 int foundindex = -1;
317 int test;
318 bool sep1=false; //This flags if the separation between top and bottom is 1.
319
320
321 //Check the top-most name first. If this is a hit, we could get stuck
322 if(ucasename == RGBNames::ColorNames[top].name)
323 {
324 foundindex = top; //It is a hit.
325 }
326
327 //Check the bottom-most name. If this is a hit, we could get stuck
328 if(ucasename == RGBNames::ColorNames[bottom].name)
329 {
330 foundindex = bottom; //It is a hit.
331 }
332
333
334 //repeat until you find it or there is nothing to find.
335 while(((top - bottom) > 1) && (foundindex == -1) && !sep1)
336 {
337
338
339 mid = (top + bottom)/2;
340
341 //set the '1-item separation' flag to true.
342 if((top - bottom) == 1) sep1 = true;
343
344
345 //Compare the library color and the current color. 0 means they are the same.
346 test =strcmp(ucasename.c_str(), RGBNames::ColorNames[mid].name.c_str());
347
348
349 if(test < 0) //If test < 0, the chosen word comes before the point in the dictionary. Move top down.
350 {
351 top = mid;
352 }
353 else if(test > 0) //If test > 0, the chosen word comes after the point in the dictionary. Move bottom up.
354 {
355 bottom = mid;
356 }
357 else
358 {
359 foundindex = mid;
360 }
361
362
363 }
364
365 //Check top & bottom to see if there is a match.
366 //if(strcmp(ucasename, RGBNames::ColorNames[top].name) == 0) foundindex = top;
367 //if(strcmp(ucasename, RGBNames::ColorNames[bottom].name) == 0) foundindex = bottom;
368
369
370 //If foundindex is still -1, we did not find the color name in the library.
371 if(foundindex == -1)
372 {
373 PError::SignalWarning("Color name [" + string(ucasename) + "] not found. Using Black.");
374 foundindex = 25; //25 is the index of black.
375
376 //Better error detection should be done here: searching with levenshtien distance,
377 //ignoring _ or - or #s, etc.
378 }
379
380
381
382 mRed = RGBNames::ColorNames[foundindex].r;
383 mGreen = RGBNames::ColorNames[foundindex].g;
384 mBlue = RGBNames::ColorNames[foundindex].b;
385 mAlpha = 255;
386 __SetProps__();
387
388 // cout << mRed << "-" << mGreen << "-" << mBlue << "-" <<endl;
389}
std::string ToUpper(const std::string &text)
void SignalWarning(const std::string &message)
Definition PError.cpp:119
const int NumRGBColorNames
PEBL_RGB_Color_Names ColorNames[]

References RGBNames::PEBL_RGB_Color_Names::b, RGBNames::ColorNames, RGBNames::PEBL_RGB_Color_Names::g, RGBNames::PEBL_RGB_Color_Names::name, RGBNames::NumRGBColorNames, RGBNames::PEBL_RGB_Color_Names::r, PError::SignalWarning(), and PEBLUtility::ToUpper().

Referenced by main().

◆ SetColorByRGBA() [1/2]

void PColor::SetColorByRGBA ( int  red,
int  green,
int  blue,
int  alpha 
)

Sets color as a 4 8-bit unsigned int.

Convenience constructor of PColor:

Definition at line 247 of file PColor.cpp.

248{
249 mRed = To8BitColor(red);
250 mGreen = To8BitColor(green);
251 mBlue = To8BitColor(blue);
252 mAlpha = To8BitColor(alpha);
253 __SetProps__();
254}

◆ SetColorByRGBA() [2/2]

void PColor::SetColorByRGBA ( unsigned int  color)

Sets color as a 32-bit unsigned int.

This uses modular arithmetic to extract colors from unsigned int. There could be more efficient ways to do it, but this is clear, and More likely to work cross-platform.

Definition at line 235 of file PColor.cpp.

236{
237 mAlpha = color % 255;
238 mBlue = color / 255 % 255;
239 mGreen = color / (255 * 255) % 255;
240 mRed = color / ( 255 * 255 * 255) % 255;
241 __SetProps__();
242}

Referenced by main(), and PColor().

◆ SetGreen()

void PColor::SetGreen ( int  color)

Definition at line 204 of file PColor.cpp.

205{
206 mGreen = To8BitColor(color);
208 mChanged = true;
209}

References GetGreen(), and PEBLObjectBase::SetProperty().

Referenced by main(), PWidget::SetBackgroundColor(), PFont::SetBackgroundColor(), PFont::SetFontColor(), and SetProperty().

◆ SetProperty()

bool PColor::SetProperty ( std::string  name,
Variant  v 
)
virtual

Standard Destructor.

Reimplemented from PEBLObjectBase.

Definition at line 153 of file PColor.cpp.

154{
155
156 if(name == "RED") SetRed(v);
157 else if(name == "GREEN") SetGreen(v);
158 else if(name == "BLUE") SetBlue(v);
159 else if(name == "ALPHA") SetAlpha(v);
160 else return false;
161
162 return true;
163}
void SetRed(int color)
Definition PColor.cpp:197
void SetGreen(int color)
Definition PColor.cpp:204
void SetBlue(int color)
Definition PColor.cpp:211
void SetAlpha(int color)
Definition PColor.cpp:218

References SetAlpha(), SetBlue(), SetGreen(), and SetRed().

◆ SetRed()

void PColor::SetRed ( int  color)

Definition at line 197 of file PColor.cpp.

198{
199 mRed = To8BitColor(color);
201 mChanged = true;
202}

References GetRed(), and PEBLObjectBase::SetProperty().

Referenced by main(), PWidget::SetBackgroundColor(), PFont::SetBackgroundColor(), PFont::SetFontColor(), and SetProperty().

◆ ValidateProperty() [1/2]

ObjectValidationError PColor::ValidateProperty ( std::string  name) const
virtual

Reimplemented from PEBLObjectBase.

Definition at line 177 of file PColor.cpp.

178 {
180 }
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const

References PEBLObjectBase::ValidateProperty().

◆ ValidateProperty() [2/2]

ObjectValidationError PColor::ValidateProperty ( std::string  name,
Variant  v 
) const
virtual

Reimplemented from PEBLObjectBase.

Definition at line 167 of file PColor.cpp.

168{
170
171 if(!v.IsNumber()) return OVE_INVALID_PROPERTY_TYPE;
172 if( v < Variant(0) || v > Variant(255)) return OVE_INVALID_PROPERTY_VALUE;
173 return OVE_SUCCESS;
174}
@ OVE_SUCCESS
Definition PEBLObject.h:38
@ OVE_INVALID_PROPERTY_VALUE
Definition PEBLObject.h:42
@ OVE_INVALID_PROPERTY_NAME
Definition PEBLObject.h:40
@ OVE_INVALID_PROPERTY_TYPE
Definition PEBLObject.h:41
virtual ObjectValidationError ValidateProperty(std::string name, Variant v) const
Definition PColor.cpp:167
bool IsNumber() const
This tests whether the Variant is a number (i.e., a float or an integer.)
Definition Variant.cpp:930

References Variant::IsNumber(), OVE_INVALID_PROPERTY_NAME, OVE_INVALID_PROPERTY_TYPE, OVE_INVALID_PROPERTY_VALUE, OVE_SUCCESS, and ValidateProperty().

Referenced by ValidateProperty().


The documentation for this class was generated from the following files: