Public Static System. Drawing. Color convertstringtocolor ( String Colorstring)
{
System. Drawing. color;
If (Colorstring [ 0 ] = ' # ' && Colorstring. Length < 8 )
{
String S = Colorstring. substring ( 1 );
While (S. Length ! = 6 )
{
S = String . Concat ( " 0 " , S );
}
Int Red = Convert. toint32 (S. substring ( 0 , 2 ), 16 );
Int Green = Convert. toint32 (S. substring ( 2 , 2 ), 16 );
Int Blue = Convert. toint32 (S. substring ( 4 , 2 ), 16 );
Color = System. Drawing. color. fromargb (red, green, blue );
}
Else
{
Color = System. Drawing. color. fromname (colorstring );
}
Return Color;
}
Public Static String Colortostring (system. Drawing. color)
{
String Result;
If (Color. isknowncolor | Color. isnamedcolor | Color. issystemcolor)
{
Result = Color. Name;
}
Else
{
Result = String . Concat ( " # " , Color. toargb (). tostring ( " X " ). Substring ( 2 ));
}
Return Result;
}
>> convert color into a # ffffff string
color C = color. white;
string S = string. format ("# {0: X2} {1: X2} {2: X2}", C. r, C. g, C. B );
>># convert a ffffff string to a color
Color c = system. Drawing. colortranslator. fromhtml ("# ff0000 ");
Color Structure: argb color
[Serializableattribute]
Public struct color
Color. fromargb Method
Creates a color structure based on four 8-bit argb values (alpha, red, green, and blue.
Reload list:
Color. fromargb (int32) creates a color structure from a 32-bit argb Value
Note: The 32-bit argb values are in the byte order of aarrggbb, which respectively represent the Alpha component values, red, green, and blue.
Color. fromargb (0x78ff0000 );
Color. fromargb (int32, color) creates a color structure from the specified structure, but uses the newly specified Alpha value.
Note: Alpha is the Alpha value of the new color. Its valid value ranges from 0 to 255.
Color. fromargb (alpha, color. Red );
Color. fromargb (int32, int32, int32) creates a color structure from the specified 8-bit color value (RGB). Alpha is 255 completely opaque by default.
Color. froma # 0000ff;
Color. fromargb (int32, int32, int32, int32) creates a color structure from the four argb component values.
Color. fromargb (, 0 );
Common colors:
System color:
The colorconverter class converts the color from one data type to another, and accesses this type through typedescriptor.
Public class colorconverter: typeconverter
Note: When converting from a string to color, colorconverter requires a non-qualified color name; it should be blue
Instead of system. Drawing. color. Blue or color. Blue
Color mycolor = color. palevioletred;
System. componentmodel. typeconverter converter = system. componentmodel. typedescriptor. getconverter (mycolor );
String colorasstring = converter. converttostring (color. palevioletred );
There is also a colortranslator class sealed
Common Methods:
Fromhtml translates the HTML color representation into a GDI + Color Structure
Color mycolor = colortranslator. fromhtml ("blue ");
Fromole translates the OLE color into a GDI + Color Structure
Int olecolor = 0xff00;
Color mycolor = colortranslator. fromole (olecolor );
Fromwin32 translates windows color values into a GDI + color structure.
Int wincolor = 0xa000;
Color mycolor = colortranslator. fromwin32 (wincolor );