Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
Previous Versions
.NET Framework 2.0
System.Drawing
Color Structure

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:
Color Structure
Represents an ARGB (alpha, red, green, blue) color.

Namespace: System.Drawing
Assembly: System.Drawing (in system.drawing.dll)

Visual Basic (Declaration)
<SerializableAttribute> _
<TypeConverterAttribute(GetType(ColorConverter))> _
Public Structure Color
Visual Basic (Usage)
Dim instance As Color
C#
[SerializableAttribute] 
[TypeConverterAttribute(typeof(ColorConverter))] 
public struct Color
C++
[SerializableAttribute] 
[TypeConverterAttribute(typeof(ColorConverter))] 
public value class Color
J#
/** @attribute SerializableAttribute() */ 
/** @attribute TypeConverterAttribute(System.Drawing.ColorConverter) */ 
public final class Color extends ValueType
JScript
XAML
Not applicable.

Named colors are represented by using the properties of the Color structure. For more information about these colors, see "Colors by Name" in the MDSN Library at http://msdn.microsoft.com/library. To determine the alpha, red, green, or blue component of a color, use the A, R, G, or B property, respectively.

You can create a custom color by using one of the FromArgb methods.

The following code example demonstrates the G, B, R, and A properties of a Color, and the op_Implicit member.

This example is designed to be used with a Windows Form. Paste the code into the form and call the ShowPropertiesOfSlateBlue method from the form's Paint event-handling method, passing e as PaintEventArgs.

Visual Basic
Private Sub ShowPropertiesOfSlateBlue(ByVal e As PaintEventArgs)
    Dim slateBlue As Color = Color.FromName("SlateBlue")
    Dim g As Byte = slateBlue.G
    Dim b As Byte = slateBlue.B
    Dim r As Byte = slateBlue.R
    Dim a As Byte = slateBlue.A
    Dim text As String = _
    String.Format("Slate Blue has these ARGB values: Alpha:{0}, " _
       & "red:{1}, green: {2}, blue {3}", New Object() {a, r, g, b})
    e.Graphics.DrawString(text, New Font(Me.Font, FontStyle.Italic), _
        New SolidBrush(slateBlue), _
        New RectangleF(New PointF(0.0F, 0.0F), _
        Size.op_Implicit(Me.Size)))
End Sub
C#
private void ShowPropertiesOfSlateBlue(PaintEventArgs e)
{
    Color slateBlue = Color.FromName("SlateBlue");
    byte g = slateBlue.G;
    byte b = slateBlue.B;
    byte r = slateBlue.R;
    byte a = slateBlue.A;
    string text = String.Format("Slate Blue has these ARGB values: Alpha:{0}, " +
        "red:{1}, green: {2}, blue {3}", new object[]{a, r, g, b});
    e.Graphics.DrawString(text, 
        new Font(this.Font, FontStyle.Italic), 
        new SolidBrush(slateBlue), 
        new RectangleF(new PointF(0.0F, 0.0F), this.Size));
}
C++
void ShowPropertiesOfSlateBlue( PaintEventArgs^ e )
{
   Color slateBlue = Color::FromName( "SlateBlue" );
   Byte g = slateBlue.G;
   Byte b = slateBlue.B;
   Byte r = slateBlue.R;
   Byte a = slateBlue.A;
   array<Object^>^temp0 = {a,r,g,b};
   String^ text = String::Format( "Slate Blue has these ARGB values: Alpha:{0}, "
   "red:{1}, green: {2}, blue {3}", temp0 );
   e->Graphics->DrawString( text, gcnew System::Drawing::Font( this->Font,FontStyle::Italic ), gcnew SolidBrush( slateBlue ), RectangleF(PointF(0.0F,0.0F),this->Size) );
}
J#
private void ShowPropertiesOfSlateBlue(PaintEventArgs e)
{
    Color slateBlue = Color.FromName("SlateBlue");
    ubyte g = slateBlue.get_G();
    ubyte b = slateBlue.get_B();
    ubyte r = slateBlue.get_R();
    ubyte a = slateBlue.get_A();
    String text = String.Format(
        "Slate Blue has these ARGB values: Alpha:{0}, "  
        + "red:{1}, green: {2}, blue {3}", 
        new Object[] { (UInt16)a, (UInt16)r, (UInt16)g, (UInt16)b });

    e.get_Graphics().DrawString(text, 
        new Font(this.get_Font(), FontStyle.Italic), 
        new SolidBrush(slateBlue), 
        new RectangleF(new PointF(0, 0), 
        Size.op_Implicit(this.get_Size())));
} //ShowPropertiesOfSlateBlue
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, 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.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
ColorToHex + HexToColor      AllenG   |   Edit   |   Show History
     // Author: Allen Gammel
     // Email: allen|p-soft|org    
     Public Shared Function HexToColor(ByVal hexString As String) As Color
        Try
            Dim r, g, b As Integer
            hexString = hexString.Replace("#", Nothing)
            If Not hexString.Length = 6 Then
                Return Color.Black
            End If
            r = HexToInt(hexString.Substring(0, 2))
            g = HexToInt(hexString.Substring(2, 2))
            b = HexToInt(hexString.Substring(4, 2))
            Return Color.FromArgb(255, r, g, b)
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            Return Color.Black
        End Try

    End Function

    Private Shared Function HexToInt(ByVal hexString As String) As Int32
        Return Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber, Nothing)
    End Function

    Public Shared Function Color2Hex(ByVal clr As Color) As String
        Return "#" & clr.ToArgb().ToString("x").Substring(2)
    End Function

 

These snippets helped me alot, maybe they'll help you aswell.

Tags What's this?: Add a tag
Flag as ContentBug
Shorter Hex to Color Example      MikeFrey   |   Edit   |   Show History

// Author: Mike Frey
// Email: mikef(a)magenic com
public static Color HexToColor(string strHex) {
    try {
        return Color.FromArgb(Convert.ToInt32(strHex, 16));
    } catch (Exception ex) {
        return Color.Black;
    }
}
Tags What's this?: Add a tag
Flag as ContentBug
You could use ColorTranslator.FromHtml      Eggdisk   |   Edit   |   Show History

You could use ColorTranslator.FromHtml like this

Color c = ColorTranslator.FromHtml("#aabbcc");
Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker