asmrobot
2019-10-14 730fe7ea65bcadbe235e40bb54b2410d14495267
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace RichCreator.Utility
{
    public class ColorUtils
    {
        /// <summary>
        /// RGB转换为HSV颜色空间
        /// </summary>
        /// <param name="r">Range(0-255)</param>
        /// <param name="g">Range(0-255)</param>
        /// <param name="b">Range(0-255)</param>
        /// <param name="h">Range(0-1.0)</param>
        /// <param name="s">Range(0-1.0)</param>
        /// <param name="v">Range(0-1.0)</param>
        public static void RGBtoHSV(byte r, byte g, byte b, out float h, out float s, out float v)
        {            
            float min, max, delta;
            min = Math.Min(r, Math.Min(g, b));
            max = Math.Max(r, Math.Max(g, b));
            v = max;               // v
            delta = max - min;
            if (max != 0)
            {
                s = delta / max;       // s
            }
            else
            {
                // r = g = b = 0        // s = 0, v is undefined
                s = 0;
                h = 0;
                return;
            }
            if (r == max)
            {
                h = delta == 0 ? 0 : (g - b) / delta;// between yellow & magenta
            }
            else if (g == max)
            {
                h = 2 + (b - r) / delta; // between cyan & yellow
            }
            else
            {
                h = 4 + (r - g) / delta; // between magenta & cyan
            }            
            h *= 60;               // degrees
            if (h < 0)
            {
                h += 360;
            }
 
            h = h / 360;
            v = v / 255;
        }
 
        /// <summary>
        /// HSV转换为RGB颜色空间
        /// </summary>
        /// <param name="h">Range(0.0~1.0)</param>
        /// <param name="s">Range(0.0~1.0)</param>
        /// <param name="v">Range(0.0~1.0)</param>
        /// <param name="r">Range(0~255)</param>
        /// <param name="g">Range(0~255)</param>
        /// <param name="b">Range(0~255)</param>
        public static void HSVtoRGB(float h, float s, float v, out byte r, out byte g, out byte b)
        {
            h = h * 360;
            v = v * 255;
            int i;
            float f, p, q, t;
            if (s == 0)
            {
                // achromatic (grey)
                r = g = b = (byte)Math.Round(v);
                return;
            }
            
            h /= 60;            // sector 0 to 5
            i = (Int32)Math.Floor(h);
            f = h - i;          // factorial part of h
            p = v * (1 - s);
            q = v * (1 - s * f);
            t = v * (1 - s * (1 - f));
            switch (i)
            {
                case 0:
                    r = (byte)Math.Round(v);
                    g = (byte)Math.Round(t);
                    b = (byte)Math.Round(p);
                    break;
                case 1:
                    r = (byte)Math.Round(q);
                    g = (byte)Math.Round(v);
                    b = (byte)Math.Round(p);
                    break;
                case 2:
                    r = (byte)Math.Round(p);
                    g = (byte)Math.Round(v);
                    b = (byte)Math.Round(t);
                    break;
                case 3:
                    r = (byte)Math.Round(p);
                    g = (byte)Math.Round(q);
                    b = (byte)Math.Round(v);
                    break;
                case 4:
                    r = (byte)Math.Round(t);
                    g = (byte)Math.Round(p);
                    b = (byte)Math.Round(v);
                    break;
                default:        // case 5:
                    r = (byte)Math.Round(v);
                    g = (byte)Math.Round(p);
                    b = (byte)Math.Round(q);
                    break;
            }
        }
 
        
    }
}