-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCRapColor.cs
More file actions
104 lines (94 loc) · 2.36 KB
/
CRapColor.cs
File metadata and controls
104 lines (94 loc) · 2.36 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
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
using System;
using System.Drawing;
namespace NSRapColor
{
public static class CRapColor
{
public static int R = 0;
public static int G = 0;
public static int B = 0;
public static double H = 0;
public static double L = 0;
public static double S = 0;
public static Color color = Color.Black;
public static void RgbToHsl(int r, int g, int b,
out double h, out double s, out double l)
{
double double_r = r / 255.0;
double double_g = g / 255.0;
double double_b = b / 255.0;
double max = double_r;
if (max < double_g) max = double_g;
if (max < double_b) max = double_b;
double min = double_r;
if (min > double_g) min = double_g;
if (min > double_b) min = double_b;
double diff = max - min;
l = (max + min) / 2;
if (Math.Abs(diff) < 0.00001)
{
s = 0;
h = 0;
}
else
{
if (l <= 0.5) s = diff / (max + min);
else s = diff / (2 - max - min);
double r_dist = (max - double_r) / diff;
double g_dist = (max - double_g) / diff;
double b_dist = (max - double_b) / diff;
if (double_r == max) h = b_dist - g_dist;
else if (double_g == max) h = 2 + r_dist - b_dist;
else h = 4 + g_dist - r_dist;
h *= 60;
if (h < 0) h += 360;
}
}
public static void HslToRgb(double h, double s, double l,
out int r, out int g, out int b)
{
double p2;
if (l <= 0.5) p2 = l * (1 + s);
else p2 = l + s - l * s;
double p1 = 2 * l - p2;
double double_r, double_g, double_b;
if (s == 0)
{
double_r = l;
double_g = l;
double_b = l;
}
else
{
double_r = QqhToRgb(p1, p2, h + 120);
double_g = QqhToRgb(p1, p2, h);
double_b = QqhToRgb(p1, p2, h - 120);
}
r = (int)(double_r * 255.0);
g = (int)(double_g * 255.0);
b = (int)(double_b * 255.0);
}
private static double QqhToRgb(double q1, double q2, double hue)
{
if (hue > 360) hue -= 360;
else if (hue < 0) hue += 360;
if (hue < 60) return q1 + (q2 - q1) * hue / 60;
if (hue < 180) return q2;
if (hue < 240) return q1 + (q2 - q1) * (240 - hue) / 60;
return q1;
}
public static void SetColor(Color c)
{
color = c;
R = color.R;
G = color.G;
B = color.B;
RgbToHsl(R, G, B, out H, out S, out L);
}
public static Color GetColor(double h, double s, double l)
{
HslToRgb(h, s, l, out int r, out int g, out int b);
return Color.FromArgb(r, g, b);
}
}
}