-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorModels.cs
47 lines (39 loc) · 1.31 KB
/
ColorModels.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
using System;
namespace LaneDetection
{
class HSV
{
public double H, S, V;
public HSV(double H, double S, double V)
{
this.H = H;
this.S = S;
this.V = V;
}
}
class RGB
{
public int R, G, B;
public RGB(int R, int G, int B)
{
this.R = R;
this.G = G;
this.B = B;
}
public static HSV ToHSV(RGB color)
{
int max = Math.Max(color.R, Math.Max(color.G, color.B));
int min = Math.Min(color.R, Math.Min(color.G, color.B));
double hue = 0;
if (min == max) hue = 0;
else if (max == color.R && color.G >= color.B) hue = 60d * (color.G - color.B) / (max - min);
else if (max == color.R && color.G < color.B) hue = 60d * (color.G - color.B) / (max - min) + 360;
else if (max == color.G) hue = 60d * (color.B - color.R) / (max - min) + 120;
else if (max == color.B) hue = 60d * (color.R - color.G) / (max - min) + 240;
double saturation = (max == 0) ? 0 : 1d - (1d * min / max);
double value = max / 255d;
HSV res = new HSV(hue, saturation, value);
return res;
}
}
}