Lumiverse  2.5
A framework for creating lighting control applications
LumiverseColor.h
Go to the documentation of this file.
1 
5 #ifndef _LUMIVERSECOLOR_H_
6 #define _LUMIVERSECOLOR_H_
7 #pragma once
8 
9 #include <map>
10 #include <unordered_map>
11 #include <cmath>
12 #include <mutex>
13 #include <memory>
14 #include <float.h>
15 #include "lib/Eigen/Dense"
16 #include "../LumiverseType.h"
17 #include "LumiverseColorLib.h"
18 
19 using namespace std;
20 
21 namespace Lumiverse {
23  enum ColorMode {
28  };
29 
30 #ifdef USE_C11_MAPS
31 
32  static unordered_map<int, string> ColorModeToString =
33  {
34  { ADDITIVE, "ADDITIVE" }, { SUBTRACTIVE, "SUBTRACTIVE" },
35  { BASIC_RGB, "BASIC_RGB" }, { BASIC_CMY, "BASIC_CMY" }
36  };
37 
39  static unordered_map<string, ColorMode> StringToColorMode =
40  {
41  { "ADDITIVE", ADDITIVE }, { "SUBTRACTIVE", SUBTRACTIVE },
42  { "BASIC_RGB", BASIC_RGB }, { "BASIC_CMY", BASIC_CMY }
43  };
44 #else
45  static string ColorModeToString(ColorMode m) {
46  unordered_map<int, string> ColorModeToString;
47  ColorModeToString[ADDITIVE] = "ADDITIVE";
48  ColorModeToString[SUBTRACTIVE] = "SUBTRACTIVE";
49  ColorModeToString[BASIC_RGB] = "BASIC_RGB";
50  ColorModeToString[BASIC_CMY] = "BASIC_CMY";
51 
52  return ColorModeToString[m];
53  }
54 
55  static ColorMode StringToColorMode(string s) {
56  unordered_map<string, ColorMode> StringToColorMode;
57  StringToColorMode["ADDITIVE"] = ADDITIVE;
58  StringToColorMode["SUBTRACTIVE"] = SUBTRACTIVE;
59  StringToColorMode["BASIC_RGB"] = BASIC_RGB;
60  StringToColorMode["BASIC_CMY"] = BASIC_CMY;
61 
62  return StringToColorMode[s];
63  }
64 #endif
65 
83  class LumiverseColor : public LumiverseType {
84  public:
92 
100  LumiverseColor(map<string, Eigen::Vector3d> basis, ColorMode mode = ADDITIVE);
101 
103  LumiverseColor(unordered_map<string, double> params, map<string, Eigen::Vector3d> basis, ColorMode mode, double weight);
104 
107 
110 
112  LumiverseColor(const LumiverseColor& other);
113 
115  virtual ~LumiverseColor();
116 
120  virtual string getTypeName() { return "color"; }
121 
125  virtual void reset();
126 
128  virtual JSONNode toJSON(string name);
129 
135  virtual string asString();
136 
143  double getX();
144 
151  double getY();
152 
159  double getZ();
160 
166  Eigen::Vector3d getXYZ() { return Eigen::Vector3d(getX(), getY(), getZ()); }
167 
172  double getx();
173 
178  double gety();
179 
184  double getz();
185 
196  void setRGB(double r, double g, double b, double weight = 1.0, RGBColorSpace cs = sRGB);
197 
204  Eigen::Vector3d getRGB(RGBColorSpace cs = sRGB);
205 
206 
214  void setxy(double x, double y, double weight = 1.0);
215 
221  Eigen::Vector3d getxyY();
222 
228  Eigen::Vector3d getLab(ReferenceWhite refWhite);
229 
237  Eigen::Vector3d getLab(Eigen::Vector3d refWhite);
238 
244  Eigen::Vector3d getLCHab(ReferenceWhite refWhite);
245 
251  Eigen::Vector3d getLCHab(Eigen::Vector3d refWhite);
252 
258  Eigen::Vector2d getupvp();
259 
265  Eigen::Vector2d getuv();
266 
272  Eigen::Vector3d getHSV(RGBColorSpace cs = sRGB);
273 
277  Eigen::Vector2d getCCT();
278 
283  bool addColorChannel(string name);
284 
290  bool deleteColorChannel(string name);
291 
302  bool setColorChannel(string name, double val);
303 
309  double getColorChannel(string name) { return m_deviceChannels[name] * m_weight; }
310 
317  double& operator[](string name);
318 
327  void setWeight(double weight);
328 
338  bool setRGBRaw(double r, double g, double b, double weight = 1.0);
339 
347  bool setHSV(double H, double S, double V, double weight = 1.0);
348 
352  unordered_map<string, double> getColorParams() { return m_deviceChannels; }
353 
355  double getWeight() { return m_weight; }
356 
357  // Arithmetic overrides
358  void operator=(LumiverseColor& other);
359 
361  LumiverseColor& operator+=(double val);
362 
364  LumiverseColor& operator-=(double val);
365 
367  LumiverseColor& operator*=(double val);
368 
370  LumiverseColor& operator/=(double val);
371 
383  shared_ptr<LumiverseType> lerp(LumiverseColor* rhs, float t);
384 
392  bool isEqual(LumiverseColor& other);
393 
401  int cmpHue(LumiverseColor& other, ReferenceWhite refWhite = D65);
402 
403  virtual bool isDefault();
404 
411  void changeMode(ColorMode newMode);
412 
416  ColorMode getMode() { return m_mode; }
417 
423  void setBasisVector(string channel, double x, double y, double z);
424 
431  void removeBasisVector(string channel);
432 
438  Eigen::Vector3d getBasisVector(string channel);
439 
443  const map<string, Eigen::Vector3d>& getBasisVectors() { return m_basisVectors; }
444 
445  size_t numBasisVectors() { return m_basisVectors.size(); }
446 
447  private:
453  double m_weight;
454 
457 
459  mutex m_mapMutex;
460 
467  unordered_map<string, double> m_deviceChannels;
468 
470  map<string, Eigen::Vector3d> m_basisVectors;
471 
474 
476  Eigen::Vector3d m_XYZ;
477 
479  void initMode();
480 
482  double sumComponent(int i);
483 
485  Eigen::Vector3d RGBtoXYZ(double r, double g, double b, RGBColorSpace cs);
486 
500  void matchChroma(double x, double y, double weight = 1.0);
501 
503  void updateXYZ();
504 
505  inline bool doubleEq(double a, double b) {
506  return (abs(a - b) < DBL_EPSILON);
507  }
508  };
509 
510  // Operators time!
511  inline bool operator==(LumiverseColor& a, LumiverseColor& b) {
512  if (a.getTypeName() != "color" || b.getTypeName() != "color")
513  return false;
514 
515  return a.isEqual(b);
516  }
517 }
518 
519 #endif
Definition: LumiverseColor.h:27
This class is a wapper around a variety of different possible data types that might be needed by a De...
Definition: LumiverseType.h:33
Eigen::Vector3d getXYZ()
Returns a vector representing the color in XYZ coordinates.
Definition: LumiverseColor.h:166
This class describes a color.
Definition: LumiverseColor.h:83
static unordered_map< int, string > ColorModeToString
Converts ColorMode to a string.
Definition: LumiverseColor.h:32
Eigen::Vector3d m_XYZ
Cached XYZ value to avoid recomputation if color has not changed.
Definition: LumiverseColor.h:476
Definition: Layer.h:437
mutex m_mapMutex
Protects access to the deviceChannels map.
Definition: LumiverseColor.h:459
Contains information for calculating various colors from incandescent sources.
unordered_map< string, double > getColorParams()
Gets the current values for the color parameters.
Definition: LumiverseColor.h:352
Definition: LumiverseColorLib.h:30
RGBColorSpace
Selects a RGB color space to use in color conversion functions.
Definition: LumiverseColorLib.h:29
Definition: LumiverseColor.h:24
const map< string, Eigen::Vector3d > & getBasisVectors()
Returns the map of channel name to basis vector.
Definition: LumiverseColor.h:443
shared_ptr< LumiverseType > lerp(LumiverseType *lhs, LumiverseType *rhs, float t)
Lerps the values of a LumiverseType and returns the value.
Definition: LumiverseTypeUtils.cpp:98
map< string, Eigen::Vector3d > m_basisVectors
Basis vectors for each LED source in the light. Represented in XYZ.
Definition: LumiverseColor.h:470
double getColorChannel(string name)
Gets the weighted value for the color channel.
Definition: LumiverseColor.h:309
virtual string getTypeName()
Returns the name of the type.
Definition: LumiverseColor.h:120
Definition: LumiverseColor.h:25
double getWeight()
Gets the weight.
Definition: LumiverseColor.h:355
unordered_map< string, double > m_deviceChannels
Contains a map from device channel to current value.
Definition: LumiverseColor.h:467
ColorMode getMode()
Gets the mode of the color.
Definition: LumiverseColor.h:416
Contains all core Lumiverse functions and variables.
Definition: Device.cpp:2
double m_weight
Parameter that controls the overall values of the device channels.
Definition: LumiverseColor.h:453
ColorMode
Selects the color mode for a LumiverseColor.
Definition: LumiverseColor.h:23
bool m_XYZupdated
Is true if the XYZ cache has been updated.
Definition: LumiverseColor.h:473
ColorMode m_mode
Color mode for this color.
Definition: LumiverseColor.h:456
static unordered_map< string, ColorMode > StringToColorMode
Converts a string to ColorMode.
Definition: LumiverseColor.h:39
Definition: LumiverseColor.h:26