Tanoda
HSVUtil.cs
Go to the documentation of this file.
1using UnityEngine;
2using System;
3
4
5
6#region ColorUtilities
7
8public static class HSVUtil
9{
10
11 public static HsvColor ConvertRgbToHsv(Color color)
12 {
13 return ConvertRgbToHsv((int)(color.r * 255), (int)(color.g * 255), (int)(color.b * 255));
14 }
15
16 //Converts an RGB color to an HSV color.
17 public static HsvColor ConvertRgbToHsv(double r, double b, double g)
18 {
19 double delta, min;
20 double h = 0, s, v;
21
22 min = Math.Min(Math.Min(r, g), b);
23 v = Math.Max(Math.Max(r, g), b);
24 delta = v - min;
25
26 if (v.Equals(0))
27 s = 0;
28 else
29 s = delta / v;
30
31 if (s.Equals(0))
32 h = 360;
33 else
34 {
35 if (r.Equals(v))
36 h = (g - b) / delta;
37 else if (g.Equals(v))
38 h = 2 + (b - r) / delta;
39 else if (b.Equals(v))
40 h = 4 + (r - g) / delta;
41
42 h *= 60;
43 if (h <= 0.0)
44 h += 360;
45 }
46
47 HsvColor hsvColor = new HsvColor();
48 hsvColor.H = 360 - h;
49 hsvColor.S = s;
50 hsvColor.V = v / 255;
51
52 return hsvColor;
53
54 }
55
56 // Converts an HSV color to an RGB color.
57 public static Color ConvertHsvToRgb(double h, double s, double v, float alpha)
58 {
59
60 double r = 0, g = 0, b = 0;
61
62 if (s.Equals(0))
63 {
64 r = v;
65 g = v;
66 b = v;
67 }
68
69 else
70 {
71 int i;
72 double f, p, q, t;
73
74
75 if (h.Equals(360))
76 h = 0;
77 else
78 h = h / 60;
79
80 i = (int)(h);
81 f = h - i;
82
83 p = v * (1.0 - s);
84 q = v * (1.0 - (s * f));
85 t = v * (1.0 - (s * (1.0f - f)));
86
87
88 switch (i)
89 {
90 case 0:
91 r = v;
92 g = t;
93 b = p;
94 break;
95
96 case 1:
97 r = q;
98 g = v;
99 b = p;
100 break;
101
102 case 2:
103 r = p;
104 g = v;
105 b = t;
106 break;
107
108 case 3:
109 r = p;
110 g = q;
111 b = v;
112 break;
113
114 case 4:
115 r = t;
116 g = p;
117 b = v;
118 break;
119
120 default:
121 r = v;
122 g = p;
123 b = q;
124 break;
125 }
126
127 }
128
129 return new Color((float)r, (float)g, (float)b, alpha);
130
131 }
132}
133
134
135#endregion ColorUtilities
136
137
138// Describes a color in terms of
139// Hue, Saturation, and Value (brightness)
140#region HsvColor
141public struct HsvColor
142{
146 public double H;
147
151 public double S;
152
153 // The value (brightness), ranges between 0 and 1
154 public double V;
155
156 public float normalizedH
157 {
158 get
159 {
160 return (float)H / 360f;
161 }
162
163 set
164 {
165 H = (double)value * 360;
166 }
167 }
168
169 public float normalizedS
170 {
171 get
172 {
173 return (float)S;
174 }
175 set
176 {
177 S = (double)value;
178 }
179 }
180
181 public float normalizedV
182 {
183 get
184 {
185 return (float)V;
186 }
187 set
188 {
189 V = (double)value;
190 }
191 }
192
193 public HsvColor(double h, double s, double v)
194 {
195 this.H = h;
196 this.S = s;
197 this.V = v;
198 }
199
200 public override string ToString()
201 {
202 return "{" + H.ToString("f2") + "," + S.ToString("f2") + "," + V.ToString("f2") + "}";
203 }
204}
205#endregion HsvColor
206
207
208
209
Es.InkPainter.Math Math
Definition: PaintTest.cs:7
UnityEngine.Color Color
Definition: TestScript.cs:32
override string ToString()
Definition: HSVUtil.cs:200
HsvColor(double h, double s, double v)
Definition: HSVUtil.cs:193
float normalizedH
Definition: HSVUtil.cs:157
float normalizedS
Definition: HSVUtil.cs:170
float normalizedV
Definition: HSVUtil.cs:182
double H
The Hue, ranges between 0 and 360
Definition: HSVUtil.cs:146
double S
The saturation, ranges between 0 and 1
Definition: HSVUtil.cs:151
double V
Definition: HSVUtil.cs:154