-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphics.cs
135 lines (118 loc) · 4.29 KB
/
Graphics.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Fractal
{
public class Graphics
{
public Color HsvToRgb(double hue, double saturation, double value)
{
int hi = Convert.ToInt32(Math.Floor(hue / 60.0)) % 6;
double f = hue / 60.0 - Math.Floor(hue / 60.0);
value = value * 255;
int p = Convert.ToInt32(value * (1 - saturation));
int q = Convert.ToInt32(value * (1 - f * saturation));
int v = Convert.ToInt32(value);
int t = Convert.ToInt32(value * (1 - (1 - f) * saturation));
if (hi == 0)
return Color.FromArgb(255, (byte)(v*255.0), (byte)(t * 255.0), (byte)(p * 255.0));
else if (hi == 1)
return Color.FromArgb(255, (byte)(q * 255.0), (byte)(v * 255.0), (byte)(p * 255.0));
else if (hi == 2)
return Color.FromArgb(255, (byte)(p * 255.0), (byte)(v * 255.0), (byte)(t * 255.0));
else if (hi == 3)
return Color.FromArgb(255, (byte)(p * 255.0), (byte)(q * 255.0), (byte)(v * 255.0));
else if (hi == 4)
return Color.FromArgb(255, (byte)(t * 255.0), (byte)(p * 255.0), (byte)(v * 255.0));
else
return Color.FromArgb(255, (byte)(v * 255.0), (byte)(p * 255.0), (byte)(q * 255.0));
}
public void HsvToRgb(double h, double S, double V, out int r, out int g, out int b)
{
double H = h;
while (H < 0) { H += 360; };
while (H >= 360) { H -= 360; };
double R, G, B;
if (V <= 0)
{ R = G = B = 0; }
else if (S <= 0)
{
R = G = B = V;
}
else
{
double hf = H / 60.0;
int i = (int)Math.Floor(hf);
double f = hf - i;
double pv = V * (1 - S);
double qv = V * (1 - S * f);
double tv = V * (1 - S * (1 - f));
switch (i)
{
// Red is the dominant color
case 0:
R = V;
G = tv;
B = pv;
break;
// Green is the dominant color
case 1:
R = qv;
G = V;
B = pv;
break;
case 2:
R = pv;
G = V;
B = tv;
break;
// Blue is the dominant color
case 3:
R = pv;
G = qv;
B = V;
break;
case 4:
R = tv;
G = pv;
B = V;
break;
// Red is the dominant color
case 5:
R = V;
G = pv;
B = qv;
break;
// Just in case we overshoot on our math by a little, we put these here. Since its a switch it won't slow us down at all to put these here.
case 6:
R = V;
G = tv;
B = pv;
break;
case -1:
R = V;
G = pv;
B = qv;
break;
// The color is not defined, we should throw an error.
default:
//LFATAL("i Value error in Pixel conversion, Value is %d", i);
R = G = B = V; // Just pretend its black/white
break;
}
}
r = Clamp((int)(R * 255.0));
g = Clamp((int)(G * 255.0));
b = Clamp((int)(B * 255.0));
}
private int Clamp(int i)
{
if (i < 0) return 0;
if (i > 255) return 255;
return i;
}
}
}