wxr
2021-07-05 1ee407dd57a7273ef6b686ac25a40d4199f5bce9
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
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(Function function )
        {
            var color = function.GetAttrState(FunctionAttributeKey.RGB).Split(",");
 
            if(color.Length!= 3)
            {
                color = new string[] {"100", "100", "100" };
            }
            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
 
    }
}