wxr
2023-06-06 592974441a4df95fffd9167c90192da1a390b1c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
 
namespace HDL_ON.Entity
{
    public class Light 
    {
        #region RGB
        /// <summary>
        /// 获取rgb颜色
        /// </summary>
        /// <returns></returns>
        public int GetRGBcolor(string rgbString)
        {
            //var color = function.GetAttrState(FunctionAttributeKey.RGB).Split(",");
            //if(!string.IsNullOrEmpty(rgbString))
            //{
               var color = rgbString.Split(",");
            //}
            if(color.Length < 3)
            {
                color = new string[] {"255", "255", "255" };
            }
            int redColor = 0;
            int greenColor = 0;
            int blueColor = 0;
 
            int.TryParse(color[0], out redColor);
            int.TryParse(color[1], out greenColor);
            int.TryParse(color[2], out blueColor);
 
            int recolor = redColor * 256 * 256 + greenColor * 256 + blueColor;
 
            return recolor;
        }
        /// <summary>
        /// 获取rgb颜色数组
        /// </summary>
        /// <param name="function"></param>
        /// <returns></returns>
        public byte[] GetRGBbytes(Function function)
        {
            var color = function.GetAttrState(FunctionAttributeKey.RGB).Split(",");
 
            if (color.Length != 3)
            {
                color = new string[] { "100", "100", "100" };
            }
            byte redColor = 0;
            byte greenColor = 0;
            byte blueColor = 0;
 
            byte.TryParse(color[0], out redColor);
            byte.TryParse(color[1], out greenColor);
            byte.TryParse(color[2], out blueColor);
 
            return new byte[] { redColor, greenColor, blueColor };
        }
 
        /// <summary>
        /// 获取rgb 控制字符串
        /// </summary>
        /// <returns></returns>
        public string GetRGBcolorString(Function function)
        {
            var color = function.GetAttrState(FunctionAttributeKey.RGB).Split(",");
            return color[0] + "," + color[1] + "," + color[2];
        }
 
        /// <summary>
        /// 获取rgb颜色
        /// </summary>
        /// <param name="index">0:red 1:green 2:blue</param>
        /// <returns></returns>
        public int GetColor( int index,Function function)
        {
            int color = 0;
            if (index <= 2)
            {
                try
                {
                    int.TryParse(function.GetAttrState(FunctionAttributeKey.RGB).Split(",")[index], out color);
                }
                catch { }
            }
            return color;
        }
 
        /// <summary>
        /// 设置rgb颜色
        /// </summary>
        public void SetRGBcolor(byte[] color,Function function)
        {
            function.SetAttrState(FunctionAttributeKey.RGB, color[0] + "," + color[1] + "," + color[2]);
        }
 
        /// <summary>
        /// 设置色温
        /// </summary>
        /// <param name="cct"></param>
        /// <param name="function"></param>
        public void SetCCT(byte[] cct,Function function)
        {
            function.SetAttrState(FunctionAttributeKey.CCT, cct[0] * 256 + cct[1]);
        }
 
        #endregion
 
        #region RGBW
        /// <summary>
        /// 获取rgbw颜色
        /// </summary>
        /// <returns></returns>
        public int GetRGBWcolor(string rgbwString)
        {
            var color = rgbwString.Split(",");
            //}
            if (color.Length != 4)
            {
                color = new string[] { "255", "255", "255" };
            }
            int redColor = 0;
            int greenColor = 0;
            int blueColor = 0;
 
            int.TryParse(color[0], out redColor);
            int.TryParse(color[1], out greenColor);
            int.TryParse(color[2], out blueColor);
 
            int recolor = redColor * 256 * 256 + greenColor * 256 + blueColor;
 
            return recolor;
        }
        /// <summary>
        /// 获取rgb颜色数组
        /// </summary>
        /// <param name="function"></param>
        /// <returns></returns>
        public byte[] GetRGBWbytes(Function function)
        {
            var color = function.GetAttrState(FunctionAttributeKey.RGBW).Split(",");
 
            if (color.Length != 4)
            {
                color = new string[] { "100", "100", "100","100" };
            }
            byte redColor = 0;
            byte greenColor = 0;
            byte blueColor = 0;
            byte wColor = 0;
 
            byte.TryParse(color[0], out redColor);
            byte.TryParse(color[1], out greenColor);
            byte.TryParse(color[2], out blueColor);
            byte.TryParse(color[3], out wColor);
 
            return new byte[] { redColor, greenColor, blueColor,wColor };
        }
 
        /// <summary>
        /// 获取rgbw 控制字符串
        /// </summary>
        /// <returns></returns>
        public string GetRGBWcolorString(Function function)
        {
            var color = function.GetAttrState(FunctionAttributeKey.RGBW).Split(",");
            return color[0] + "," + color[1] + "," + color[2] + "," + color[3];
        }
 
        /// <summary>
        /// 获取rgbW颜色
        /// </summary>
        /// <param name="index">0:red 1:green 2:blue</param>
        /// <returns></returns>
        public int GetRGBWColor(int index, Function function)
        {
            int color = 0;
            if (index <= 2)
            {
                try
                {
                    int.TryParse(function.GetAttrState(FunctionAttributeKey.RGBW).Split(",")[index], out color);
                }
                catch { }
            }
            return color;
        }
 
        /// <summary>
        /// 设置rgbw颜色
        /// </summary>
        public void SetRGBWcolor(byte[] color, Function function)
        {
 
            function.SetAttrState(FunctionAttributeKey.RGBW, color[0] + "," + color[1] + "," + color[2] + "," + color[3]);
        }
 
 
        #endregion
 
    }
}