Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / fx / src / xsp / System / Web / UI / WebControls / WebColorConverter.cs / 1 / WebColorConverter.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Text;
using System.Web.Util;
using System.Web.UI;
using System.Globalization;
using System.Security.Permissions;
///
///
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public class WebColorConverter : ColorConverter {
private static Hashtable htmlSysColorTable;
///
///
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
string colorText = ((string)value).Trim();
Color c = Color.Empty;
// empty color
if (String.IsNullOrEmpty(colorText))
return c;
// #RRGGBB notation is handled by ColorConverter
if (colorText[0] == '#') {
return base.ConvertFrom(context, culture, value);
}
// special case. HTML requires LightGrey, but System.Drawing.KnownColor has LightGray
if (StringUtil.EqualsIgnoreCase(colorText, "LightGrey")) {
return Color.LightGray;
}
// System color
if (htmlSysColorTable == null) {
InitializeHTMLSysColorTable();
}
object o = htmlSysColorTable[colorText];
if (o != null) {
return (Color)o;
}
}
// ColorConverter handles all named and KnownColors
return base.ConvertFrom(context, culture, value);
}
///
///
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(string)) {
if (value != null) {
Color c = (Color)value;
if (c == Color.Empty) {
return String.Empty;
}
/*
if (c.IsKnownColor) {
// Handle Web system colors and any 'special' named colors
string colorString = null;
switch (c.ToKnownColor()) {
case KnownColor.ActiveBorder: colorString = "ActiveBorder"; break;
case KnownColor.ActiveCaption: colorString = "ActiveCaption"; break;
case KnownColor.AppWorkspace: colorString = "AppWorkspace"; break;
case KnownColor.Desktop: colorString = "Background"; break;
case KnownColor.Control: colorString = "ButtonFace"; break;
case KnownColor.ControlLight: colorString = "ButtonHighlight"; break;
case KnownColor.ControlDark: colorString = "ButtonShadow"; break;
case KnownColor.ControlText: colorString = "ButtonText"; break;
case KnownColor.ActiveCaptionText: colorString = "CaptionText"; break;
case KnownColor.GrayText: colorString = "GrayText"; break;
case KnownColor.HotTrack:
case KnownColor.Highlight: colorString = "Highlight"; break;
case KnownColor.HighlightText: colorString = "HighlightText"; break;
case KnownColor.InactiveBorder: colorString = "InactiveBorder"; break;
case KnownColor.InactiveCaption: colorString = "InactiveCaption"; break;
case KnownColor.InactiveCaptionText: colorString = "InactiveCaptionText"; break;
case KnownColor.Info: colorString = "InfoBackground"; break;
case KnownColor.InfoText: colorString = "InfoText"; break;
case KnownColor.Menu: colorString = "Menu"; break;
case KnownColor.MenuText: colorString = "MenuText"; break;
case KnownColor.ScrollBar: colorString = "Scrollbar"; break;
case KnownColor.ControlDarkDark: colorString = "ThreeDDarkShadow"; break;
case KnownColor.ControlLightLight: colorString = "ButtonHighlight"; break;
case KnownColor.Window: colorString = "Window"; break;
case KnownColor.WindowFrame: colorString = "WindowFrame"; break;
case KnownColor.WindowText: colorString = "WindowText"; break;
case KnownColor.LightGray: colorString = "LightGrey"; break;
}
if (colorString != null) {
return colorString;
}
}
*/
if (c.IsKnownColor == false) {
// in the Web scenario, colors should be formatted in #RRGGBB notation
StringBuilder sb = new StringBuilder("#", 7);
sb.Append((c.R).ToString("X2", CultureInfo.InvariantCulture));
sb.Append((c.G).ToString("X2", CultureInfo.InvariantCulture));
sb.Append((c.B).ToString("X2", CultureInfo.InvariantCulture));
return sb.ToString();
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
private static void InitializeHTMLSysColorTable() {
Hashtable t = new Hashtable(StringComparer.OrdinalIgnoreCase);
t["activeborder"] = Color.FromKnownColor(KnownColor.ActiveBorder);
t["activecaption"] = Color.FromKnownColor(KnownColor.ActiveCaption);
t["appworkspace"] = Color.FromKnownColor(KnownColor.AppWorkspace);
t["background"] = Color.FromKnownColor(KnownColor.Desktop);
t["buttonface"] = Color.FromKnownColor(KnownColor.Control);
t["buttonhighlight"] = Color.FromKnownColor(KnownColor.ControlLightLight);
t["buttonshadow"] = Color.FromKnownColor(KnownColor.ControlDark);
t["buttontext"] = Color.FromKnownColor(KnownColor.ControlText);
t["captiontext"] = Color.FromKnownColor(KnownColor.ActiveCaptionText);
t["graytext"] = Color.FromKnownColor(KnownColor.GrayText);
t["highlight"] = Color.FromKnownColor(KnownColor.Highlight);
t["highlighttext"] = Color.FromKnownColor(KnownColor.HighlightText);
t["inactiveborder"] = Color.FromKnownColor(KnownColor.InactiveBorder);
t["inactivecaption"] = Color.FromKnownColor(KnownColor.InactiveCaption);
t["inactivecaptiontext"] = Color.FromKnownColor(KnownColor.InactiveCaptionText);
t["infobackground"] = Color.FromKnownColor(KnownColor.Info);
t["infotext"] = Color.FromKnownColor(KnownColor.InfoText);
t["menu"] = Color.FromKnownColor(KnownColor.Menu);
t["menutext"] = Color.FromKnownColor(KnownColor.MenuText);
t["scrollbar"] = Color.FromKnownColor(KnownColor.ScrollBar);
t["threeddarkshadow"] = Color.FromKnownColor(KnownColor.ControlDarkDark);
t["threedface"] = Color.FromKnownColor(KnownColor.Control);
t["threedhighlight"] = Color.FromKnownColor(KnownColor.ControlLight);
t["threedlightshadow"] = Color.FromKnownColor(KnownColor.ControlLightLight);
t["window"] = Color.FromKnownColor(KnownColor.Window);
t["windowframe"] = Color.FromKnownColor(KnownColor.WindowFrame);
t["windowtext"] = Color.FromKnownColor(KnownColor.WindowText);
htmlSysColorTable = t;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Web.UI.WebControls {
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Text;
using System.Web.Util;
using System.Web.UI;
using System.Globalization;
using System.Security.Permissions;
///
///
[AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.Minimal)]
public class WebColorConverter : ColorConverter {
private static Hashtable htmlSysColorTable;
///
///
///
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
if (value is string) {
string colorText = ((string)value).Trim();
Color c = Color.Empty;
// empty color
if (String.IsNullOrEmpty(colorText))
return c;
// #RRGGBB notation is handled by ColorConverter
if (colorText[0] == '#') {
return base.ConvertFrom(context, culture, value);
}
// special case. HTML requires LightGrey, but System.Drawing.KnownColor has LightGray
if (StringUtil.EqualsIgnoreCase(colorText, "LightGrey")) {
return Color.LightGray;
}
// System color
if (htmlSysColorTable == null) {
InitializeHTMLSysColorTable();
}
object o = htmlSysColorTable[colorText];
if (o != null) {
return (Color)o;
}
}
// ColorConverter handles all named and KnownColors
return base.ConvertFrom(context, culture, value);
}
///
///
///
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) {
if (destinationType == null) {
throw new ArgumentNullException("destinationType");
}
if (destinationType == typeof(string)) {
if (value != null) {
Color c = (Color)value;
if (c == Color.Empty) {
return String.Empty;
}
/*
if (c.IsKnownColor) {
// Handle Web system colors and any 'special' named colors
string colorString = null;
switch (c.ToKnownColor()) {
case KnownColor.ActiveBorder: colorString = "ActiveBorder"; break;
case KnownColor.ActiveCaption: colorString = "ActiveCaption"; break;
case KnownColor.AppWorkspace: colorString = "AppWorkspace"; break;
case KnownColor.Desktop: colorString = "Background"; break;
case KnownColor.Control: colorString = "ButtonFace"; break;
case KnownColor.ControlLight: colorString = "ButtonHighlight"; break;
case KnownColor.ControlDark: colorString = "ButtonShadow"; break;
case KnownColor.ControlText: colorString = "ButtonText"; break;
case KnownColor.ActiveCaptionText: colorString = "CaptionText"; break;
case KnownColor.GrayText: colorString = "GrayText"; break;
case KnownColor.HotTrack:
case KnownColor.Highlight: colorString = "Highlight"; break;
case KnownColor.HighlightText: colorString = "HighlightText"; break;
case KnownColor.InactiveBorder: colorString = "InactiveBorder"; break;
case KnownColor.InactiveCaption: colorString = "InactiveCaption"; break;
case KnownColor.InactiveCaptionText: colorString = "InactiveCaptionText"; break;
case KnownColor.Info: colorString = "InfoBackground"; break;
case KnownColor.InfoText: colorString = "InfoText"; break;
case KnownColor.Menu: colorString = "Menu"; break;
case KnownColor.MenuText: colorString = "MenuText"; break;
case KnownColor.ScrollBar: colorString = "Scrollbar"; break;
case KnownColor.ControlDarkDark: colorString = "ThreeDDarkShadow"; break;
case KnownColor.ControlLightLight: colorString = "ButtonHighlight"; break;
case KnownColor.Window: colorString = "Window"; break;
case KnownColor.WindowFrame: colorString = "WindowFrame"; break;
case KnownColor.WindowText: colorString = "WindowText"; break;
case KnownColor.LightGray: colorString = "LightGrey"; break;
}
if (colorString != null) {
return colorString;
}
}
*/
if (c.IsKnownColor == false) {
// in the Web scenario, colors should be formatted in #RRGGBB notation
StringBuilder sb = new StringBuilder("#", 7);
sb.Append((c.R).ToString("X2", CultureInfo.InvariantCulture));
sb.Append((c.G).ToString("X2", CultureInfo.InvariantCulture));
sb.Append((c.B).ToString("X2", CultureInfo.InvariantCulture));
return sb.ToString();
}
}
}
return base.ConvertTo(context, culture, value, destinationType);
}
private static void InitializeHTMLSysColorTable() {
Hashtable t = new Hashtable(StringComparer.OrdinalIgnoreCase);
t["activeborder"] = Color.FromKnownColor(KnownColor.ActiveBorder);
t["activecaption"] = Color.FromKnownColor(KnownColor.ActiveCaption);
t["appworkspace"] = Color.FromKnownColor(KnownColor.AppWorkspace);
t["background"] = Color.FromKnownColor(KnownColor.Desktop);
t["buttonface"] = Color.FromKnownColor(KnownColor.Control);
t["buttonhighlight"] = Color.FromKnownColor(KnownColor.ControlLightLight);
t["buttonshadow"] = Color.FromKnownColor(KnownColor.ControlDark);
t["buttontext"] = Color.FromKnownColor(KnownColor.ControlText);
t["captiontext"] = Color.FromKnownColor(KnownColor.ActiveCaptionText);
t["graytext"] = Color.FromKnownColor(KnownColor.GrayText);
t["highlight"] = Color.FromKnownColor(KnownColor.Highlight);
t["highlighttext"] = Color.FromKnownColor(KnownColor.HighlightText);
t["inactiveborder"] = Color.FromKnownColor(KnownColor.InactiveBorder);
t["inactivecaption"] = Color.FromKnownColor(KnownColor.InactiveCaption);
t["inactivecaptiontext"] = Color.FromKnownColor(KnownColor.InactiveCaptionText);
t["infobackground"] = Color.FromKnownColor(KnownColor.Info);
t["infotext"] = Color.FromKnownColor(KnownColor.InfoText);
t["menu"] = Color.FromKnownColor(KnownColor.Menu);
t["menutext"] = Color.FromKnownColor(KnownColor.MenuText);
t["scrollbar"] = Color.FromKnownColor(KnownColor.ScrollBar);
t["threeddarkshadow"] = Color.FromKnownColor(KnownColor.ControlDarkDark);
t["threedface"] = Color.FromKnownColor(KnownColor.Control);
t["threedhighlight"] = Color.FromKnownColor(KnownColor.ControlLight);
t["threedlightshadow"] = Color.FromKnownColor(KnownColor.ControlLightLight);
t["window"] = Color.FromKnownColor(KnownColor.Window);
t["windowframe"] = Color.FromKnownColor(KnownColor.WindowFrame);
t["windowtext"] = Color.FromKnownColor(KnownColor.WindowText);
htmlSysColorTable = t;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WebErrorHandler.cs
- AtlasWeb.Designer.cs
- DoubleLink.cs
- PersonalizationStateInfo.cs
- NamedPipeChannelListener.cs
- AddInContractAttribute.cs
- ButtonBaseAutomationPeer.cs
- SourceItem.cs
- AssemblyNameUtility.cs
- JsonSerializer.cs
- GeneralTransform3DTo2D.cs
- ToolStripContainer.cs
- RecordManager.cs
- BamlStream.cs
- CompilerResults.cs
- RegexMatch.cs
- ExtensionQuery.cs
- TextureBrush.cs
- BindingExpression.cs
- ToolStripStatusLabel.cs
- Rfc2898DeriveBytes.cs
- XmlValidatingReader.cs
- CreateUserWizardDesigner.cs
- ContextStaticAttribute.cs
- TextProperties.cs
- DataKey.cs
- GuidConverter.cs
- Interfaces.cs
- SafeFileHandle.cs
- DataServiceKeyAttribute.cs
- ColumnHeader.cs
- HMACSHA256.cs
- TraceUtility.cs
- NetworkInformationPermission.cs
- HttpFileCollection.cs
- ParenthesizePropertyNameAttribute.cs
- StringSorter.cs
- BaseTemplateCodeDomTreeGenerator.cs
- ToolStripPanel.cs
- PasswordRecoveryAutoFormat.cs
- IsolatedStorageException.cs
- JulianCalendar.cs
- MaskPropertyEditor.cs
- HMACSHA512.cs
- DetailsViewInsertEventArgs.cs
- TextViewBase.cs
- FixedFindEngine.cs
- ObjectDataSourceFilteringEventArgs.cs
- ReadOnlyTernaryTree.cs
- SqlRewriteScalarSubqueries.cs
- WsatStrings.cs
- MetabaseServerConfig.cs
- MinMaxParagraphWidth.cs
- RequestCachePolicyConverter.cs
- UserNameServiceElement.cs
- RuleSettingsCollection.cs
- PreviewKeyDownEventArgs.cs
- NameNode.cs
- PanelStyle.cs
- CapabilitiesPattern.cs
- FileSystemEventArgs.cs
- RepeaterItem.cs
- DefaultShape.cs
- DocumentViewerBase.cs
- WindowVisualStateTracker.cs
- formatter.cs
- InfiniteTimeSpanConverter.cs
- QueuePropertyVariants.cs
- HelloMessageCD1.cs
- ToolStripProfessionalLowResolutionRenderer.cs
- _FixedSizeReader.cs
- FollowerQueueCreator.cs
- SelectorItemAutomationPeer.cs
- GenericEnumConverter.cs
- EmbeddedMailObject.cs
- documentation.cs
- WaitHandleCannotBeOpenedException.cs
- MetadataReference.cs
- Nodes.cs
- DataGridViewTextBoxEditingControl.cs
- NavigationService.cs
- PropertyStore.cs
- RemotingConfigParser.cs
- DataServiceQuery.cs
- XmlSchemaSimpleContentRestriction.cs
- QilReference.cs
- DoWorkEventArgs.cs
- StaticFileHandler.cs
- ExtractedStateEntry.cs
- SyndicationItemFormatter.cs
- SafeCertificateStore.cs
- BindingContext.cs
- InputScopeNameConverter.cs
- SchemaTableColumn.cs
- PeerCollaboration.cs
- LazyInitializer.cs
- ZipIOCentralDirectoryDigitalSignature.cs
- MessageQueueAccessControlEntry.cs
- FaultConverter.cs
- SystemUnicastIPAddressInformation.cs