-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cs
135 lines (120 loc) · 4.23 KB
/
Utils.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
/*
Gamma Analyzer - Controlling application for Burn
Copyright (C) 2016 Norwegian Radiation Protection Authority
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Authors: Dag Robole,
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
namespace crash
{
// Class with global helper functions and objects
public static class Utils
{
public static void Swap<T>(ref T arg1, ref T arg2)
{
T tmp = arg1;
arg1 = arg2;
arg2 = tmp;
}
// Convert a color to an integer
public static int ToArgb(Color color)
{
int a = color.A;
int r = color.R;
int g = color.G;
int b = color.B;
return a << 24 | r << 16 | g << 8 | b;
}
// Convert an integer to a color
public static Color ToColor(int argb)
{
byte[] b = BitConverter.GetBytes(argb);
return Color.FromArgb(b[3], b[2], b[1], b[0]);
}
public static Color MapColor(double min, double max, double val)
{
Color c = Color.FromArgb(0, 0, 255);
double f = (val - min) / (max - min);
double a = (1.0 - f) / 0.25;
double x = Math.Floor(a);
double y = Math.Floor(255.0 * (a - x));
switch ((int)x)
{
case 0:
c = Color.FromArgb(255, (int)y, 0);
break;
case 1:
c = Color.FromArgb(255 - (int)y, 255, 0);
break;
case 2:
c = Color.FromArgb(0, 255, (int)y);
break;
case 3:
c = Color.FromArgb(0, 255 - (int)y, 255);
break;
case 4:
c = Color.FromArgb(0, 0, 255);
break;
}
return c;
}
public static bool ValidateIP(string ip)
{
IPAddress ipAddr = null;
return IPAddress.TryParse(ip, out ipAddr);
}
public static bool ValidateUri(string uri)
{
Uri uriTest;
return Uri.TryCreate(uri, UriKind.Absolute, out uriTest) && (uriTest.Scheme == Uri.UriSchemeHttp || uriTest.Scheme == Uri.UriSchemeHttps);
}
public static HttpStatusCode GetResponseData(HttpWebRequest request, out string data)
{
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (var sr = new StreamReader(response.GetResponseStream()))
{
data = sr.ReadToEnd();
}
}
}
catch (WebException wex)
{
HttpWebResponse response = (HttpWebResponse)wex.Response;
if (request != null)
request.Abort();
if (wex.Status == WebExceptionStatus.Timeout)
{
data = WebExceptionStatus.Timeout.ToString();
return HttpStatusCode.RequestTimeout;
}
else if (response == null)
{
data = HttpStatusCode.InternalServerError.ToString();
return HttpStatusCode.InternalServerError;
}
else
{
data = response.StatusCode.ToString();
return response.StatusCode;
}
}
return HttpStatusCode.OK;
}
}
}