📄 colorutil.cs
字号:
}
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;
controlColor = value;
}
}
static public Color VSNetPressedColor
{
get
{
if ( useCustomColor && pressedColor != Color.Empty )
return pressedColor;
else
return CalculateColor(SystemColors.Highlight, ColorUtil.VSNetSelectionColor, 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 VSNetColors that I use throughout
// the UtilityLibrary
useCustomColor = true;
pressedColor = value;
}
}
static public Color VSNetCheckedColor
{
get
{
if ( useCustomColor && pressedColor != Color.Empty )
return checkedColor;
else
return CalculateColor(SystemColors.Highlight, SystemColors.Window, 30);
}
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;
checkedColor = value;
}
}
static public Color VSNetBorderColor
{
get
{
if ( useCustomColor && borderColor != Color.Empty )
return borderColor;
else
{
// This color is the default color unless we are using
// custom colors
return SystemColors.Highlight;
}
}
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;
borderColor = value;
}
}
public static Color CalculateColor(Color front, Color back, int alpha)
{
// Use alpha blending to brigthen the colors but don't use it
// directly. Instead derive an opaque color that we can use.
// -- if we use a color with alpha blending directly we won't be able
// to paint over whatever color was in the background and there
// would be shadows of that color showing through
Color frontColor = Color.FromArgb(255, front);
Color backColor = Color.FromArgb(255, back);
float frontRed = frontColor.R;
float frontGreen = frontColor.G;
float frontBlue = frontColor.B;
float backRed = backColor.R;
float backGreen = backColor.G;
float backBlue = backColor.B;
float fRed = frontRed*alpha/255 + backRed*((float)(255-alpha)/255);
byte newRed = (byte)fRed;
float fGreen = frontGreen*alpha/255 + backGreen*((float)(255-alpha)/255);
byte newGreen = (byte)fGreen;
float fBlue = frontBlue*alpha/255 + backBlue*((float)(255-alpha)/255);
byte newBlue = (byte)fBlue;
return Color.FromArgb(255, newRed, newGreen, newBlue);
}
#endregion
#region General functions
static public Color ColorFromPoint(Graphics g, int x, int y)
{
IntPtr hDC = g.GetHdc();
// Get the color of the pixel first
uint colorref = APIsGdi.GetPixel(hDC, x, y);
byte Red = GetRValue(colorref);
byte Green = GetGValue(colorref);
byte Blue = GetBValue(colorref);
g.ReleaseHdc(hDC);
return Color.FromArgb(Red, Green, Blue);
}
static public bool IsKnownColor(Color color, ref Color knownColor, bool useTransparent)
{
// Using the Color structrure "FromKnowColor" does not work if
// we did not create the color as a known color to begin with
// we need to compare the rgbs of both color
Color currentColor = Color.Empty;
bool badColor = false;
for (KnownColor enumValue = 0; enumValue <= KnownColor.YellowGreen; enumValue++)
{
currentColor = Color.FromKnownColor(enumValue);
string colorName = currentColor.Name;
if ( !useTransparent )
badColor = (colorName == "Transparent");
if ( color.A == currentColor.A && color.R == currentColor.R && color.G == currentColor.G
&& color.B == currentColor.B && !currentColor.IsSystemColor
&& !badColor )
{
knownColor = currentColor;
return true;
}
}
return false;
}
static public bool IsSystemColor(Color color, ref Color knownColor)
{
// Using the Color structrure "FromKnowColor" does not work if
// we did not create the color as a known color to begin with
// we need to compare the rgbs of both color
Color currentColor = Color.Empty;
for (KnownColor enumValue = 0; enumValue <= KnownColor.YellowGreen; enumValue++)
{
currentColor = Color.FromKnownColor(enumValue);
string colorName = currentColor.Name;
if ( color.R == currentColor.R && color.G == currentColor.G
&& color.B == currentColor.B && currentColor.IsSystemColor )
{
knownColor = currentColor;
return true;
}
}
return false;
}
static public uint GetCOLORREF(Color color)
{
return RGB(color.R, color.G, color.B);
}
static public Color ColorFromRGBString(string text)
{
Color rgbColor = Color.Empty;
string[] RGBs = text.Split(',');
if ( RGBs.Length != 3 )
{
// If we don't have three pieces of information, then the
// string is not properly formatted, inform the use
throw new Exception("RGB color string is not well formed");
}
string stringR = RGBs[0];
string stringG = RGBs[1];
string stringB = RGBs[2];
int R, G, B;
try
{
R = Convert.ToInt32(stringR);
G = Convert.ToInt32(stringG);
B = Convert.ToInt32(stringB);
if ( ( R < 0 || R > 255 ) || ( G < 0 || G > 255 ) || ( B < 0 || B > 255 ) )
{
throw new Exception("Out of bounds RGB value");
}
else
{
// Convert to color
rgbColor = Color.FromArgb(R, G, B);
// See if we have either a web color or a systgem color
Color knownColor = Color.Empty;
bool isKnown = ColorUtil.IsKnownColor( rgbColor, ref knownColor, true);
if ( !isKnown )
isKnown = ColorUtil.IsSystemColor(rgbColor, ref knownColor);
if ( isKnown )
rgbColor = knownColor;
}
}
catch ( InvalidCastException )
{
throw new Exception("Invalid RGB value");
}
return rgbColor;
}
static public Color LightColor(Color color, int inc)
{
int red = color.R;
int green = color.G;
int blue = color.B;
if ( red + inc <= 255 )
red += inc;
if ( green + inc <= 255 )
green += inc;
if ( blue + inc <= 255 )
blue += inc;
return Color.FromArgb(red, green, blue);
}
static public Color DarkColor(Color color, int inc)
{
int red = color.R;
int green = color.G;
int blue = color.B;
if ( red >= inc )
red -= inc;
if ( green >= inc )
green -= inc;
if ( blue >= inc )
blue -= inc;
return Color.FromArgb(red, green, blue);
}
#endregion
#region Windows RGB related macros
static public byte GetRValue(uint color)
{
return (byte)color;
}
static public byte GetGValue(uint color)
{
return ((byte)(((short)(color)) >> 8));
}
static public byte GetBValue(uint color)
{
return ((byte)((color)>>16));
}
static public uint RGB(int r, int g, int b)
{
return ((uint)(((byte)(r)|((short)((byte)(g))<<8))|(((short)(byte)(b))<<16)));
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -