Color.FromArgb Method (Int32, Color)
Assembly: System.Drawing (in system.drawing.dll)
public static Color FromArgb ( int alpha, Color baseColor )
public static function FromArgb ( alpha : int, baseColor : Color ) : Color
Not applicable.
Parameters
- alpha
The alpha value for the new Color. Valid values are 0 through 255.
- baseColor
The Color from which to create the new Color.
Return Value
The Color that this method creates.The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. The code performs the following actions:
-
Creates Color structures from the three color component values (red, green, blue). Three Color structures are created, one for each primary color.
-
Iterates through a range of alpha values, changing the alpha value of a color.
-
During each iteration, sets the color of a brush to the modified color and paints a rectangle to show the color.
-
Repeats steps 2 and 3 for each primary color.
The alpha value is never fully opaque and the rectangles overlap so you get color-combination effects.
void FromArgb3( PaintEventArgs^ e ) { Graphics^ g = e->Graphics; // Opaque colors (alpha value defaults to 255 -- max value). Color red = Color::FromArgb( 255, 0, 0 ); Color green = Color::FromArgb( 0, 255, 0 ); Color blue = Color::FromArgb( 0, 0, 255 ); // Solid brush initialized to red. SolidBrush^ myBrush = gcnew SolidBrush( red ); int alpha; // x coordinate of first red rectangle int x = 50; // y coordinate of first red rectangle int y = 50; // Fill rectangles with red, varying the alpha value from 25 to 250. for ( alpha = 25; alpha <= 250; alpha += 25 ) { myBrush->Color = Color::FromArgb( alpha, red ); g->FillRectangle( myBrush, x, y, 50, 100 ); g->FillRectangle( myBrush, x, y + 250, 50, 50 ); x += 50; } x = 50; // y coordinate of first green rectangle y += 50; // Fill rectangles with green, varying the alpha value from 25 to 250. for ( alpha = 25; alpha <= 250; alpha += 25 ) { myBrush->Color = Color::FromArgb( alpha, green ); g->FillRectangle( myBrush, x, y, 50, 150 ); x += 50; } x = 50; // y coordinate of first blue rectangle y += 100; // Fill rectangles with blue, varying the alpha value from 25 to 250. for ( alpha = 25; alpha <= 250; alpha += 25 ) { myBrush->Color = Color::FromArgb( alpha, blue ); g->FillRectangle( myBrush, x, y, 50, 150 ); x += 50; } }
public void FromArgb3(PaintEventArgs e)
{
Graphics g = e.get_Graphics();
// Opaque colors (alpha value defaults to 255 -- max value).
Color red = Color.FromArgb(255, 0, 0);
Color green = Color.FromArgb(0, 255, 0);
Color blue = Color.FromArgb(0, 0, 255);
// Solid brush initialized to red.
SolidBrush myBrush = new SolidBrush(red);
int alpha;
// x coordinate of first red rectangle
int x = 50;
// y coordinate of first red rectangle
int y = 50;
// Fill rectangles with red, varying the alpha value from 25 to 250.
for (alpha = 25; alpha <= 250; alpha+=25) {
myBrush.set_Color(Color.FromArgb(alpha, red));
g.FillRectangle(myBrush, x, y, 50, 100);
g.FillRectangle(myBrush, x, y + 250, 50, 50);
x += 50;
}
// x coordinate of first green rectangle
x = 50;
// y coordinate of first green rectangle
y += 50;
// Fill rectangles with green, varying the alpha value from 25 to 250.
for (alpha = 25; alpha <= 250; alpha+=25) {
myBrush.set_Color(Color.FromArgb(alpha, green));
g.FillRectangle(myBrush, x, y, 50, 150);
x += 50;
}
// x coordinate of first blue rectangle.
x = 50;
// y coordinate of first blue rectangle
y += 100;
// Fill rectangles with blue, varying the alpha value from 25 to 250.
for (alpha = 25; alpha <= 250; alpha+=25) {
myBrush.set_Color(Color.FromArgb(alpha, blue));
g.FillRectangle(myBrush, x, y, 50, 150);
x += 50;
}
} //FromArgb3
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.