-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathUtil.cs
185 lines (175 loc) · 6.65 KB
/
Util.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Diagnostics;
namespace System.Drawing.PieChart {
/// <summary>
/// Enumeration for different shadow styles
/// </summary>
public enum ShadowStyle {
/// <summary>
/// No shadow. Sides are drawn in the same color as the top od the
/// pie.
/// </summary>
NoShadow,
/// <summary>
/// Uniform shadow. Sides are drawn somewhat darker.
/// </summary>
UniformShadow,
/// <summary>
/// Gradual shadow is used to fully simulate 3-D shadow.
/// </summary>
GradualShadow
}
/// <summary>
/// Enumeration for edge color types.
/// </summary>
public enum EdgeColorType {
/// <summary>
/// Edges are not drawn at all.
/// </summary>
NoEdge,
/// <summary>
/// System (window text) color is used to draw edges.
/// </summary>
SystemColor,
/// <summary>
/// Surface color is used for edges.
/// </summary>
SurfaceColor,
/// <summary>
/// A color that is little darker than surface color is used for
/// edges.
/// </summary>
DarkerThanSurface,
/// <summary>
/// A color that is significantly darker than surface color is used
/// for edges.
/// </summary>
DarkerDarkerThanSurface,
/// <summary>
/// A color that is little lighter than surface color is used for
/// edges.
/// </summary>
LighterThanSurface,
/// <summary>
/// A color that is significantly lighter than surface color is used
/// for edges.
/// </summary>
LighterLighterThanSurface,
/// <summary>
/// Contrast color is used for edges.
/// </summary>
Contrast,
/// <summary>
/// Enhanced contrast color is used for edges.
/// </summary>
EnhancedContrast,
/// <summary>
/// Black color is used for light surfaces and white for dark
/// surfaces.
/// </summary>
FullContrast
}
/// <summary>
/// Structure representing edge color used for rendering.
/// </summary>
public struct EdgeColor {
/// <summary>
/// Gets the actual color used for rendering.
/// </summary>
public static Color GetRenderingColor(EdgeColorType edgeColorType, Color color) {
Debug.Assert(color != Color.Empty);
if (edgeColorType == EdgeColorType.Contrast || edgeColorType == EdgeColorType.EnhancedContrast)
edgeColorType = GetContrastColorType(color, edgeColorType);
float correctionFactor = 0;
switch (edgeColorType) {
case EdgeColorType.SystemColor:
return SystemColors.WindowText;
case EdgeColorType.SurfaceColor:
return color;
case EdgeColorType.FullContrast:
return GetFullContrastColor(color);
case EdgeColorType.DarkerThanSurface:
correctionFactor = -ColorUtil.BrightnessEnhancementFactor1;
break;
case EdgeColorType.DarkerDarkerThanSurface:
correctionFactor = -ColorUtil.BrightnessEnhancementFactor2;
break;
case EdgeColorType.LighterThanSurface:
correctionFactor = +ColorUtil.BrightnessEnhancementFactor1;
break;
case EdgeColorType.LighterLighterThanSurface:
correctionFactor = +ColorUtil.BrightnessEnhancementFactor2;
break;
case EdgeColorType.NoEdge:
return System.Drawing.Color.Transparent;
}
return ColorUtil.CreateColorWithCorrectedLightness(color, correctionFactor);
}
private static EdgeColorType GetContrastColorType(Color color, EdgeColorType colorType) {
Debug.Assert(colorType == EdgeColorType.Contrast || colorType == EdgeColorType.EnhancedContrast);
if (color.GetBrightness() > s_brightnessThreshold) {
if (colorType == EdgeColorType.Contrast)
return EdgeColorType.DarkerThanSurface;
else
return EdgeColorType.DarkerDarkerThanSurface;
}
if (colorType == EdgeColorType.Contrast)
return EdgeColorType.LighterThanSurface;
else
return EdgeColorType.LighterLighterThanSurface;
}
private static Color GetFullContrastColor(Color color) {
if (color.GetBrightness() > s_brightnessThreshold)
return Color.Black;
return Color.White;
}
private static readonly float s_brightnessThreshold = 0.4F;
}
/// <summary>
/// Color utility structure.
/// </summary>
public struct ColorUtil {
/// <summary>
/// Creates color with corrected lightness.
/// </summary>
/// <param name="color">
/// Color to correct.
/// </param>
/// <param name="correctionFactor">
/// Correction factor, with a value between -1 and 1. Negative values
/// create darker color, positive values lighter color. Zero value
/// returns the current color.
/// </param>
/// <returns>
/// Corrected <c>Color</c> structure.
/// </returns>
public static Color CreateColorWithCorrectedLightness(Color color, float correctionFactor) {
Debug.Assert(correctionFactor <= 1 && correctionFactor >= -1);
if (correctionFactor == 0)
return color;
float red = (float)color.R;
float green = (float)color.G;
float blue = (float)color.B;
if (correctionFactor < 0) {
correctionFactor = 1 + correctionFactor;
red *= correctionFactor;
green *= correctionFactor;
blue *= correctionFactor;
}
else {
red = (255 - red) * correctionFactor + red;
green = (255 - green) * correctionFactor + green;
blue = (255 - blue) * correctionFactor + blue;
}
return System.Drawing.Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
}
/// <summary>
/// Small brightness change factor.
/// </summary>
public static readonly float BrightnessEnhancementFactor1 = 0.3F;
/// <summary>
/// Large brightness change factor.
/// </summary>
public static readonly float BrightnessEnhancementFactor2 = 0.5F;
}
}