RGB转长转RGB
RGB To Long To RGB
如何在 C#
中正确地将 Red, Green, Blue
值转换为 Long
并返回 Red, Green, Blue
?
我到处都找遍了,找不到任何有用的东西..
public enum ColorFormat
{
RGB, RGBA, ARGB
}
public static int ColorToDecimal(Color color, ColorFormat format = ColorFormat.RGB)
{
switch (format)
{
default:
case ColorFormat.RGB:
return color.R << 16 | color.G << 8 | color.B;
case ColorFormat.RGBA:
return color.R << 24 | color.G << 16 | color.B << 8 | color.A;
case ColorFormat.ARGB:
return color.A << 24 | color.R << 16 | color.G << 8 | color.B;
}
}
public static Color DecimalToColor(int dec, ColorFormat format = ColorFormat.RGB)
{
switch (format)
{
default:
case ColorFormat.RGB:
return Color.FromArgb((dec >> 16) & 0xFF, (dec >> 8) & 0xFF, dec & 0xFF);
case ColorFormat.RGBA:
return Color.FromArgb(dec & 0xFF, (dec >> 24) & 0xFF, (dec >> 16) & 0xFF, (dec >> 8) & 0xFF);
case ColorFormat.ARGB:
return Color.FromArgb((dec >> 24) & 0xFF, (dec >> 16) & 0xFF, (dec >> 8) & 0xFF, dec & 0xFF);
}
}
Color c = Color.FromArgb(30, 20, 120); // construct a color from RGB values
long redVal = c.R; //Extract Red value as long
long greenVal = c.G;//Extract Green Value as Long
long blueVal = c.B;//Extract Blue Value as Long
您也可以像下面这样声明颜色:
Color c = Color.Beige;
然后
c.R=245,
c.G=245,
c.B=220
如何在 C#
中正确地将 Red, Green, Blue
值转换为 Long
并返回 Red, Green, Blue
?
我到处都找遍了,找不到任何有用的东西..
public enum ColorFormat
{
RGB, RGBA, ARGB
}
public static int ColorToDecimal(Color color, ColorFormat format = ColorFormat.RGB)
{
switch (format)
{
default:
case ColorFormat.RGB:
return color.R << 16 | color.G << 8 | color.B;
case ColorFormat.RGBA:
return color.R << 24 | color.G << 16 | color.B << 8 | color.A;
case ColorFormat.ARGB:
return color.A << 24 | color.R << 16 | color.G << 8 | color.B;
}
}
public static Color DecimalToColor(int dec, ColorFormat format = ColorFormat.RGB)
{
switch (format)
{
default:
case ColorFormat.RGB:
return Color.FromArgb((dec >> 16) & 0xFF, (dec >> 8) & 0xFF, dec & 0xFF);
case ColorFormat.RGBA:
return Color.FromArgb(dec & 0xFF, (dec >> 24) & 0xFF, (dec >> 16) & 0xFF, (dec >> 8) & 0xFF);
case ColorFormat.ARGB:
return Color.FromArgb((dec >> 24) & 0xFF, (dec >> 16) & 0xFF, (dec >> 8) & 0xFF, dec & 0xFF);
}
}
Color c = Color.FromArgb(30, 20, 120); // construct a color from RGB values
long redVal = c.R; //Extract Red value as long
long greenVal = c.G;//Extract Green Value as Long
long blueVal = c.B;//Extract Blue Value as Long
您也可以像下面这样声明颜色:
Color c = Color.Beige;
然后 c.R=245, c.G=245, c.B=220