-
Notifications
You must be signed in to change notification settings - Fork 15
/
ColorTranslator.cs
190 lines (169 loc) · 5.34 KB
/
ColorTranslator.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
186
187
188
189
190
//
// System.Drawing.ColorTranslator.cs
//
// Authors:
// Dennis Hayes ([email protected])
// Ravindra ([email protected])
// Sebastien Pouliot <[email protected]>
//
// Copyright (C) 2001 Ximian, Inc. http://www.ximian.com
// Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.ComponentModel;
using System.Globalization;
namespace System.Drawing {
public sealed class ColorTranslator {
private ColorTranslator ()
{
}
public static Color FromHtml (string htmlColor)
{
if ((htmlColor == null) || (htmlColor.Length == 0))
return Color.Empty;
switch (htmlColor.ToLowerInvariant ()) {
case "buttonface":
case "threedface":
return SystemColors.Control;
case "buttonhighlight":
case "threedlightshadow":
return SystemColors.ControlLightLight;
case "buttonshadow":
return SystemColors.ControlDark;
case "captiontext":
return SystemColors.ActiveCaptionText;
case "threeddarkshadow":
return SystemColors.ControlDarkDark;
case "threedhighlight":
return SystemColors.ControlLight;
case "background":
return SystemColors.Desktop;
case "buttontext":
return SystemColors.ControlText;
case "infobackground":
return SystemColors.Info;
// special case for Color.LightGray versus html's LightGrey (#340917)
case "lightgrey":
return Color.LightGray;
}
TypeConverter converter = TypeDescriptor.GetConverter (typeof (Color));
return (Color) converter.ConvertFromString (htmlColor);
}
internal static Color FromBGR (int bgr)
{
Color result = Color.FromArgb (0xFF, (bgr & 0xFF), ((bgr >> 8) & 0xFF), ((bgr >> 16) & 0xFF));
Color known = KnownColors.FindColorMatch (result);
return (known.IsEmpty) ? result : known;
}
public static Color FromOle (int oleColor)
{
// OleColor format is BGR
return FromBGR (oleColor);
}
public static Color FromWin32 (int win32Color)
{
// Win32Color format is BGR
return FromBGR (win32Color);
}
public static string ToHtml (Color c)
{
if (c.IsEmpty)
return String.Empty;
if (c.IsSystemColor) {
KnownColor kc = c.ToKnownColor ();
switch (kc) {
case KnownColor.ActiveBorder:
case KnownColor.ActiveCaption:
case KnownColor.AppWorkspace:
case KnownColor.GrayText:
case KnownColor.Highlight:
case KnownColor.HighlightText:
case KnownColor.InactiveBorder:
case KnownColor.InactiveCaption:
case KnownColor.InactiveCaptionText:
case KnownColor.InfoText:
case KnownColor.Menu:
case KnownColor.MenuText:
case KnownColor.ScrollBar:
case KnownColor.Window:
case KnownColor.WindowFrame:
case KnownColor.WindowText:
return KnownColors.GetName (kc).ToLower (CultureInfo.InvariantCulture);
case KnownColor.ActiveCaptionText:
return "captiontext";
case KnownColor.Control:
return "buttonface";
case KnownColor.ControlDark:
return "buttonshadow";
case KnownColor.ControlDarkDark:
return "threeddarkshadow";
case KnownColor.ControlLight:
return "buttonface";
case KnownColor.ControlLightLight:
return "buttonhighlight";
case KnownColor.ControlText:
return "buttontext";
case KnownColor.Desktop:
return "background";
case KnownColor.HotTrack:
return "highlight";
case KnownColor.Info:
return "infobackground";
default:
return String.Empty;
}
}
if (c.IsNamedColor) {
if (c == Color.LightGray)
return "LightGrey";
else
return c.Name;
}
return FormatHtml (c.R, c.G, c.B);
}
static char GetHexNumber (int b)
{
return (char) (b > 9 ? 55 + b : 48 + b);
}
static string FormatHtml (int r, int g, int b)
{
char [] htmlColor = new char [7];
htmlColor [0] = '#';
htmlColor [1] = GetHexNumber ((r >> 4) & 15);
htmlColor [2] = GetHexNumber (r & 15);
htmlColor [3] = GetHexNumber ((g >> 4) & 15);
htmlColor [4] = GetHexNumber (g & 15);
htmlColor [5] = GetHexNumber ((b >> 4) & 15);
htmlColor [6] = GetHexNumber (b & 15);
return new string (htmlColor);
}
public static int ToOle (Color c)
{
// OleColor format is BGR, same as Win32
return ((c.B << 16) | (c.G << 8) | c.R);
}
public static int ToWin32 (Color c)
{
// Win32Color format is BGR, Same as OleColor
return ((c.B << 16) | (c.G << 8) | c.R);
}
}
}