-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorConverter.cs
More file actions
59 lines (53 loc) · 2.33 KB
/
ColorConverter.cs
File metadata and controls
59 lines (53 loc) · 2.33 KB
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
using System;
using System.Drawing;
namespace ColorHelper
{
/// <summary>
/// the colorconverter class can be used to convert different library, color spaces or denotations
/// </summary>
public static class ColorConverter
{
public static System.Drawing.Color ConvertColor(System.Windows.Media.Color mediaColor)
{
return System.Drawing.Color.FromArgb(mediaColor.A, mediaColor.R, mediaColor.G, mediaColor.B);
}
public static System.Windows.Media.Color ConvertColor(System.Drawing.Color drawingColor)
{
return System.Windows.Media.Color.FromArgb(drawingColor.A, drawingColor.R, drawingColor.G, drawingColor.B);
}
public static System.Windows.Media.Color FromHex(string color)
{
return (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(color);
}
public static void ColorToHSV(Color color, out double hue, out double saturation, out double value)
{
int max = Math.Max(color.R, Math.Max(color.G, color.B));
int min = Math.Min(color.R, Math.Min(color.G, color.B));
hue = color.GetHue();
saturation = (max == 0) ? 0 : 1d - (1d * min / max);
value = max / 255d;
}
public static Color ColorFromHSV(double hue, double saturation, double brightnessValue)
{
int hi = Convert.ToInt32(Math.Floor(hue / 60)) % 6;
double f = hue / 60 - Math.Floor(hue / 60);
brightnessValue = brightnessValue * 255;
int v = Convert.ToInt32(brightnessValue);
int p = Convert.ToInt32(brightnessValue * (1 - saturation));
int q = Convert.ToInt32(brightnessValue * (1 - f * saturation));
int t = Convert.ToInt32(brightnessValue * (1 - (1 - f) * saturation));
if (hi == 0)
return Color.FromArgb(255, v, t, p);
else if (hi == 1)
return Color.FromArgb(255, q, v, p);
else if (hi == 2)
return Color.FromArgb(255, p, v, t);
else if (hi == 3)
return Color.FromArgb(255, p, q, v);
else if (hi == 4)
return Color.FromArgb(255, t, p, v);
else
return Color.FromArgb(255, v, p, q);
}
}
}