-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathColorHelper.cs
More file actions
163 lines (143 loc) · 5.46 KB
/
Copy pathColorHelper.cs
File metadata and controls
163 lines (143 loc) · 5.46 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
using System;
using System.Drawing;
namespace MaterialSkin2Framework
{
public static class ColorHelper
{
/// <summary>
/// Tints the color by the given percent.
/// </summary>
/// <param name="color">The color being tinted.</param>
/// <param name="percent">The percent to tint. Ex: 0.1 will make the color 10% lighter.</param>
/// <returns>The new tinted color.</returns>
public static Color Lighten(this Color color, float percent)
{
var lighting = color.GetBrightness();
lighting = lighting + lighting * percent;
if (lighting > 1.0)
{
lighting = 1;
}
else if (lighting <= 0)
{
lighting = 0.1f;
}
var tintedColor = ColorHelper.FromHsl(color.A, color.GetHue(), color.GetSaturation(), lighting);
return tintedColor;
}
/// <summary>
/// Tints the color by the given percent.
/// </summary>
/// <param name="color">The color being tinted.</param>
/// <param name="percent">The percent to tint. Ex: 0.1 will make the color 10% darker.</param>
/// <returns>The new tinted color.</returns>
public static Color Darken(this Color color, float percent)
{
var lighting = color.GetBrightness();
lighting = lighting - lighting * percent;
if (lighting > 1.0)
{
lighting = 1;
}
else if (lighting <= 0)
{
lighting = 0;
}
var tintedColor = ColorHelper.FromHsl(color.A, color.GetHue(), color.GetSaturation(), lighting);
return tintedColor;
}
/// <summary>
/// Converts the HSL values to a Color.
/// </summary>
/// <param name="alpha">The alpha.</param>
/// <param name="hue">The hue.</param>
/// <param name="saturation">The saturation.</param>
/// <param name="lighting">The lighting.</param>
/// <returns></returns>
public static Color FromHsl(int alpha, float hue, float saturation, float lighting)
{
if (0 > alpha || 255 < alpha)
{
throw new ArgumentOutOfRangeException("alpha");
}
if (0f > hue || 360f < hue)
{
throw new ArgumentOutOfRangeException("hue");
}
if (0f > saturation || 1f < saturation)
{
throw new ArgumentOutOfRangeException("saturation");
}
if (0f > lighting || 1f < lighting)
{
throw new ArgumentOutOfRangeException("lighting");
}
if (0 == saturation)
{
return Color.FromArgb(alpha, Convert.ToInt32(lighting * 255), Convert.ToInt32(lighting * 255), Convert.ToInt32(lighting * 255));
}
float fMax, fMid, fMin;
int iSextant, iMax, iMid, iMin;
if (0.5 < lighting)
{
fMax = lighting - (lighting * saturation) + saturation;
fMin = lighting + (lighting * saturation) - saturation;
}
else
{
fMax = lighting + (lighting * saturation);
fMin = lighting - (lighting * saturation);
}
iSextant = (int)Math.Floor(hue / 60f);
if (300f <= hue)
{
hue -= 360f;
}
hue /= 60f;
hue -= 2f * (float)Math.Floor(((iSextant + 1f) % 6f) / 2f);
if (0 == iSextant % 2)
{
fMid = hue * (fMax - fMin) + fMin;
}
else
{
fMid = fMin - hue * (fMax - fMin);
}
iMax = Convert.ToInt32(fMax * 255);
iMid = Convert.ToInt32(fMid * 255);
iMin = Convert.ToInt32(fMin * 255);
switch (iSextant)
{
case 1:
return Color.FromArgb(alpha, iMid, iMax, iMin);
case 2:
return Color.FromArgb(alpha, iMin, iMax, iMid);
case 3:
return Color.FromArgb(alpha, iMin, iMid, iMax);
case 4:
return Color.FromArgb(alpha, iMid, iMin, iMax);
case 5:
return Color.FromArgb(alpha, iMax, iMin, iMid);
default:
return Color.FromArgb(alpha, iMax, iMid, iMin);
}
}
/// <summary>
/// Removes alpha value without changing Color.
/// </summary>
/// <param name="foreground">The foreground color.</param>
/// <param name="background">The background color.</param>
/// <returns></returns>
public static Color RemoveAlpha(Color foreground, Color background)
{
if (foreground.A == 255)
return foreground;
var alpha = foreground.A / 255.0;
var diff = 1.0 - alpha;
return Color.FromArgb(255,
(byte)(foreground.R * alpha + background.R * diff),
(byte)(foreground.G * alpha + background.G * diff),
(byte)(foreground.B * alpha + background.B * diff));
}
}
}