Color.GetBrightness Method

Gets the hue-saturation-brightness (HSB) brightness value for this Color structure.

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

public:
float GetBrightness ()
public float GetBrightness ()
public function GetBrightness () : float
Not applicable.

Return Value

The brightness of this Color. The brightness ranges from 0.0 through 1.0, where 0.0 represents black and 1.0 represents white.

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 an instance of a Color structure, redShade, to be used for comparisons.

  • Iterates through the KnownColor enumeration elements to find all known colors that have the same brightness as redShade. The iterations are terminated when 15 matches are found or the value of the loop counter is greater than the last KnownColor element.

  • During each iteration, saves the KnownColor element—if it matches the criteria—in an array.

  • Uses a brush to paint rectangles.

The first rectangle is painted the color represented by redShade. Each of the other rectangles is painted a KnownColor that matches the brightness of the redShade.

void KnownColorBrightnessExample2( PaintEventArgs^ e )
{
   Graphics^ g = e->Graphics;

   // Color structures. One is a variable used for temporary storage. The other
   // is a constant used for comparisons.
   Color someColor = Color::FromArgb( 0 );
   Color redShade = Color::FromArgb( 255, 200, 0, 100 );

   // Array to store KnownColor values that match the brightness of the
   // redShade color.
   array<KnownColor>^colorMatches = gcnew array<KnownColor>(15);

   // Number of matches found.
   int count = 0;

   // Iterate through the KnownColor enums until 15 matches are found.
   for ( KnownColor enumValue = (KnownColor)0; enumValue <= KnownColor::YellowGreen && count < 15; enumValue = enumValue + (KnownColor)1 )
   {
      someColor = Color::FromKnownColor( enumValue );
      if ( someColor.GetBrightness() == redShade.GetBrightness() )
               colorMatches[ count++ ] = enumValue;
   }

   // display the redShade color and its argb value.
   SolidBrush^ myBrush1 = gcnew SolidBrush( redShade );
   System::Drawing::Font^ myFont = gcnew System::Drawing::Font( "Arial",12 );
   int x = 20;
   int y = 20;
   someColor = redShade;
   g->FillRectangle( myBrush1, x, y, 100, 30 );
   g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 120, (float)y );

   // Iterate through the matches that were found and display each color that
   // corresponds with the enum value in the array. also display the name of
   // The KnownColor.
   for ( int i = 0; i < count; i++ )
   {
      y += 40;
      someColor = Color::FromKnownColor( colorMatches[ i ] );
      myBrush1->Color = someColor;
      g->FillRectangle( myBrush1, x, y, 100, 30 );
      g->DrawString( someColor.ToString(), myFont, Brushes::Black, (float)x + 120, (float)y );
   }
}

public void KnownColorBrightnessExample2(PaintEventArgs e)
{
    Graphics g = e.get_Graphics();

    // Color structures. One is a variable used for temporary storage.
    //  The other is a constant used for comparisons.
    Color someColor = Color.FromArgb(0);
    Color redShade = Color.FromArgb(255, 200, 0, 100);

    // Array to store KnownColor values that match the brightness of the
    // redShade color.
    KnownColor colorMatches[] = new KnownColor[15];

    // Number of matches found.
    int count = 0;

    // Iterate through the KnownColor enums until 15 matches are found.
    for (KnownColor enumValue = (KnownColor)0; 
        (enumValue.CompareTo(KnownColor.YellowGreen) <= 0) 
        && (count < 15); enumValue++) {

        someColor = Color.FromKnownColor(enumValue);
        if (someColor.GetBrightness() == redShade.GetBrightness()) {
            colorMatches.set_Item(count++, enumValue);
        }
    }

    // display the redShade color and its argb value.
    SolidBrush myBrush1 = new SolidBrush(redShade);
    Font myFont = new Font("Arial", 12);
    int x = 20;
    int y = 20;

    someColor = redShade;
    g.FillRectangle(myBrush1, x, y, 100, 30);
    g.DrawString(someColor.ToString(), myFont, Brushes.get_Black(), 
        x + 120, y);

    // Iterate through the matches that were found and display each color 
    // that corresponds with the enum value in the array. also display the 
    // name of The KnownColor.
    for (int i = 0; i < count; i++) {
        y += 40;
        someColor = Color.FromKnownColor(
            (KnownColor)colorMatches.get_Item(i));
        myBrush1.set_Color(someColor);
        g.FillRectangle(myBrush1, x, y, 100, 30);
        g.DrawString(someColor.ToString(), myFont, Brushes.get_Black(), 
            x + 120, y);
    }
} //KnownColorBrightnessExample2

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.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show: