1 #include "stdafx.h"
2 #include "../main/Helper.h"
3 #include "hardwaretypes.h"
4 #include "ColorSwitch.h"
5 #include "../main/json_helper.h"
6 
_tColor()7 _tColor::_tColor()
8 {
9 	//level = 0;
10 	t = r = g = b = cw = ww = 0;
11 	mode = ColorModeNone;
12 }
13 
_tColor(const Json::Value & json)14 _tColor::_tColor(const Json::Value &json)
15 {
16 	fromJSON(json);
17 }
18 
_tColor(const std::string & sRaw)19 _tColor::_tColor(const std::string &sRaw) //explicit to avoid unintentional conversion of string to _tColor
20 {
21 	fromString(sRaw);
22 }
23 
_tColor(const uint8_t ir,const uint8_t ig,const uint8_t ib,const uint8_t icw,const uint8_t iww,ColorMode imode)24 _tColor::_tColor(const uint8_t ir, const uint8_t ig, const uint8_t ib, const uint8_t icw, const uint8_t iww, ColorMode imode)
25 {
26 	mode = imode;
27 	//level=ilevel;
28 	r=ir;
29 	g=ig;
30 	b=ib;
31 	cw=icw;
32 	ww=iww;
33 	t = 0;
34 }
35 
_tColor(uint8_t x,ColorMode imode)36 _tColor::_tColor(uint8_t x, ColorMode imode)
37 {
38 	t = r = g = b = cw = ww = 0;
39 	mode = imode;
40 	if (mode == ColorModeWhite)
41 	{
42 		ww = 0xff;
43 		cw = 0xff;
44 	}
45 	else if (mode == ColorModeTemp)
46 	{
47 		ww = x;
48 		cw = 255-x;
49 		t = x;
50 	}
51 }
52 
getrgbwwhex() const53 std::string _tColor::getrgbwwhex() const
54 {
55 	char tmp[13];
56 	snprintf(tmp, sizeof(tmp), "%02x%02x%02x%02x%02x", r, g, b, cw, ww);
57 	return std::string(tmp);
58 }
59 
fromJSON(const Json::Value & root)60 void _tColor::fromJSON(const Json::Value &root)
61 {
62 	mode = ColorModeNone;
63 	try {
64 		int tmp = root.get("m", 0).asInt();
65 		if (tmp == ColorModeNone || tmp > ColorModeLast) return;
66 		mode = ColorMode(tmp);
67 		t = (uint8_t)root.get("t", 0).asInt();
68 		r = (uint8_t)root.get("r", 0).asInt();
69 		g = (uint8_t)root.get("g", 0).asInt();
70 		b = (uint8_t)root.get("b", 0).asInt();
71 		cw = (uint8_t)root.get("cw", 0).asInt();
72 		ww = (uint8_t)root.get("ww", 0).asInt();
73 		//level = root.get("l", 0).asInt();
74 	}
75 	catch (...) {
76 	}
77 }
78 
fromString(const std::string & s)79 void _tColor::fromString(const std::string &s)
80 {
81 	Json::Value root;
82 	mode = ColorModeNone;
83 	try {
84 		bool parsingSuccessful = ParseJSonStrict(s.c_str(), root);
85 		if ( !parsingSuccessful )
86 		{
87 			mode = ColorModeNone;
88 			return;
89 		}
90 		fromJSON(root);
91 	}
92 	catch (...) {
93 	}
94 }
95 
toJSONValue() const96 Json::Value _tColor::toJSONValue() const
97 {
98 	Json::Value root;
99 
100 	// Return empty value if the color is not valid
101 	if (mode == ColorModeNone || mode > ColorModeLast) return root;
102 
103 	root["m"] = mode;
104 	//root["l"] = level;
105 	root["t"] = t;
106 	root["r"] = r;
107 	root["g"] = g;
108 	root["b"] = b;
109 	root["cw"] = cw;
110 	root["ww"] = ww;
111 
112 	return root;
113 }
114 
toJSONString() const115 std::string _tColor::toJSONString() const
116 {
117 	// Return the empty string if the color is not valid
118 	if (mode == ColorModeNone || mode > ColorModeLast) return "";
119 
120 	Json::Value root = toJSONValue();
121 
122 	return JSonToRawString(root);
123 }
124 
toString() const125 std::string _tColor::toString() const
126 {
127 	char tmp[1024];
128 	// Return the empty string if the color is not valid
129 	if (mode == ColorModeNone || mode > ColorModeLast) return "{INVALID}";
130 
131 	snprintf(tmp, sizeof(tmp), "{m: %u, RGB: %02x%02x%02x, CWWW: %02x%02x, CT: %u}", (unsigned int)mode, r, g, b, cw, ww, t);
132 
133 	return std::string(tmp);
134 }
135