📄 colorutil.cs
字号:
using System;
using System.Drawing;
namespace System.Runtime.InteropServices.APIs
{
/// <summary>
/// Summary description for ColorConvert.
/// </summary>
public class ColorUtil
{
#region Class Variables
static Color backgroundColor = Color.Empty;
static Color selectionColor = Color.Empty;
static Color selectionUnfocusedColor = Color.Empty;
static Color controlColor = Color.Empty;
static Color pressedColor = Color.Empty;
static Color checkedColor = Color.Empty;
static Color borderColor = Color.Empty;
static bool useCustomColor = false;
#endregion
#region Constructor
// No need to construct this object
private ColorUtil()
{
}
#endregion
#region Properties
static public bool UsingCustomColor
{
get { return useCustomColor;}
}
#endregion
#region Knowncolor names
static public string[] KnownColorNames =
{ "Transparent", "Black", "DimGray", "Gray", "DarkGray", "Silver", "LightGray", "Gainsboro", "WhiteSmoke", "White",
"RosyBrown", "IndianRed", "Brown", "Firebrick", "LightCoral", "Maroon", "DarkRed", "Red", "Snow", "MistyRose",
"Salmon", "Tomato", "DarkSalmon", "Coral", "OrangeRed", "LightSalmon", "Sienna", "SeaShell", "Chocalate",
"SaddleBrown", "SandyBrown", "PeachPuff", "Peru", "Linen", "Bisque", "DarkOrange", "BurlyWood", "Tan", "AntiqueWhite",
"NavajoWhite", "BlanchedAlmond", "PapayaWhip", "Mocassin", "Orange", "Wheat", "OldLace", "FloralWhite", "DarkGoldenrod",
"Cornsilk", "Gold", "Khaki", "LemonChiffon", "PaleGoldenrod", "DarkKhaki", "Beige", "LightGoldenrod", "Olive",
"Yellow", "LightYellow", "Ivory", "OliveDrab", "YellowGreen", "DarkOliveGreen", "GreenYellow", "Chartreuse", "LawnGreen",
"DarkSeaGreen", "ForestGreen", "LimeGreen", "PaleGreen", "DarkGreen", "Green", "Lime", "Honeydew", "SeaGreen", "MediumSeaGreen",
"SpringGreen", "MintCream", "MediumSpringGreen", "MediumAquaMarine", "YellowAquaMarine", "Turquoise", "LightSeaGreen",
"MediumTurquoise", "DarkSlateGray", "PaleTurquoise", "Teal", "DarkCyan", "Aqua", "Cyan", "LightCyan", "Azure", "DarkTurquoise",
"CadetBlue", "PowderBlue", "LightBlue", "DeepSkyBlue", "SkyBlue", "LightSkyBlue", "SteelBlue", "AliceBlue", "DodgerBlue",
"SlateGray", "LightSlateGray", "LightSteelBlue", "CornflowerBlue", "RoyalBlue", "MidnightBlue", "Lavender", "Navy",
"DarkBlue", "MediumBlue", "Blue", "GhostWhite", "SlateBlue", "DarkSlateBlue", "MediumSlateBlue", "MediumPurple",
"BlueViolet", "Indigo", "DarkOrchid", "DarkViolet", "MediumOrchid", "Thistle", "Plum", "Violet", "Purple", "DarkMagenta",
"Magenta", "Fuchsia", "Orchid", "MediumVioletRed", "DeepPink", "HotPink", "LavenderBlush", "PaleVioletRed", "Crimson",
"Pink", "LightPink" };
#endregion
#region Systemcolors names
static public string[] SystemColorNames =
{
"ActiveBorder", "ActiveCaption", "ActiveCaptionText", "AppWorkspace", "Control", "ControlDark", "ControlDarkDark",
"ControlLight", "ControlLightLight", "ControlText", "Desktop", "GrayText", "HighLight", "HighLightText",
"HotTrack", "InactiveBorder", "InactiveCaption", "InactiveCaptionText", "Info", "InfoText", "Menu", "MenuText",
"ScrollBar", "Window", "WindowFrame", "WindowText" };
#endregion
#region Conversion between RGB and Hue, Saturation and Luminosity function helpers
static public void HSLToRGB(float h, float s, float l, ref float r, ref float g, ref float b)
{
// given h,s,l,[240 and r,g,b [0-255]
// convert h [0-360], s,l,r,g,b [0-1]
h=(h/240)*360;
s /= 240;
l /= 240;
r /= 255;
g /= 255;
b /= 255;
// Begin Foley
float m1,m2;
// Calc m2
if (l<=0.5f)
{
//m2=(l*(l+s)); seems to be typo in Foley??, replace l for 1
m2=(l*(1+s));
}
else
{
m2=(l+s-l*s);
}
//calc m1
m1=2.0f*l-m2;
//calc r,g,b in [0-1]
if (s==0.0f)
{ // Achromatic: There is no hue
// leave out the UNDEFINED part, h will always have value
r=g=b=l;
}
else
{ // Chromatic: There is a hue
r= getRGBValue(m1,m2,h+120.0f);
g= getRGBValue(m1,m2,h);
b= getRGBValue(m1,m2,h-120.0f);
}
// End Foley
// convert to 0-255 ranges
r*=255;
g*=255;
b*=255;
}
static private float getRGBValue(float n1, float n2, float hue)
{
// Helper function for the HSLToRGB function above
if (hue>360.0f)
{
hue-=360.0f;
}
else if (hue<0.0f)
{
hue+=360.0f;
}
if (hue<60.0)
{
return n1+(n2-n1)*hue/60.0f;
}
else if (hue<180.0f)
{
return n2;
}
else if (hue<240.0f)
{
return n1+(n2-n1)*(240.0f-hue)/60.0f;
}
else
{
return n1;
}
}
static public void RGBToHSL(int r, int g, int b, ref float h, ref float s, ref float l)
{
float delta;
float fr = (float)r/255;
float fg = (float)g/255;
float fb = (float)b/255;
float max = Math.Max(fr,Math.Max(fg,fb));
float min = Math.Min(fr,Math.Min(fg,fb));
//calc the lightness
l = (max+min)/2;
if (max==min)
{
//should be undefined but this works for what we need
s = 0;
h = 240.0f;
}
else
{
delta = max-min;
//calc the Saturation
if (l < 0.5)
{
s = delta/(max+min);
}
else
{
s = delta/(2.0f-(max+min));
}
//calc the hue
if (fr==max)
{
h = (fg-fb)/delta;
}
else if (fg==max)
{
h = 2.0f + (fb-fr)/delta;
}
else if (fb==max)
{
h = 4.0f + (fr-fg)/delta;
}
//convert hue to degrees
h*=60.0f;
if (h<0.0f)
{
h+=360.0f;
}
}
//convert to 0-255 ranges
//h [0-360], h,l [0-1]
l*=240;
s*=240;
h=(h/360)*240;
}
#endregion
#region Visual Studio .NET colors calculation helpers
static public Color VSNetBackgroundColor
{
get
{
if ( useCustomColor && backgroundColor != Color.Empty )
return backgroundColor;
else
return CalculateColor(SystemColors.Window, SystemColors.Control, 220);
}
set
{
// Flag that we are going to use custom colors instead
// of calculating the color based on the system colors
// -- this is a way of hooking up into the VSNetColors that I use throughout
// the UtilityLibrary
useCustomColor = true;
backgroundColor = value;
}
}
static public Color VSNetSelectionUnfocusedColor
{
get
{
if ( useCustomColor && selectionColor != Color.Empty )
return selectionUnfocusedColor;
else
return CalculateColor(SystemColors.Highlight, SystemColors.Window, 25);
}
set
{
// Flag that we are going to use custom colors instead
// of calculating the color based on the system colors
// -- this is a way of hooking up into the VSNetColor that I use throughout
// the UtilityLibrary
useCustomColor = true;
selectionUnfocusedColor = value;
}
}
static public Color VSNetSelectionColor
{
get
{
if ( useCustomColor && selectionColor != Color.Empty )
return selectionColor;
else
return CalculateColor(SystemColors.Highlight, SystemColors.Window, 70);
}
set
{
// Flag that we are going to use custom colors instead
// of calculating the color based on the system colors
// -- this is a way of hooking up into the VSNetColor that I use throughout
// the UtilityLibrary
useCustomColor = true;
selectionColor = value;
}
}
static public Color VSNetControlColor
{
get
{ if ( useCustomColor && controlColor != Color.Empty )
return controlColor;
else
return CalculateColor(SystemColors.Control, VSNetBackgroundColor, 195);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -