This documentation is archived and is not being maintained.

Color.GetHue Method

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

[Visual Basic]
Public Function GetHue() As Single
[C#]
public float GetHue();
[C++]
public: float GetHue();
[JScript]
public function GetHue() : float;

Return Value

The hue, in degrees, of this Color structure. The hue is measured in degrees, ranging from 0.0 through 360.0, in HSB color space.

Example

[Visual Basic, C#] The following example is designed for use with Windows Forms, and it requires PaintEventArgs e, 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 KnownColors that have the same hue 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.

[Visual Basic, C#] The first rectangle is painted the color represented by redShade. Each of the other rectangles is painted a KnownColor that matches the hue of the redShade.

[Visual Basic] 
Public Sub GetHueExample(e As PaintEventArgs)
Dim g As Graphics = e.Graphics
' Color structures. One is used for temporary storage. The other
' is a constant used for comparisons.
Dim someColor As Color = Color.FromArgb(0)
Dim redShade As Color = Color.FromArgb(255, 200, 0, 100)
' Array for KnownColor values that match the hue of redShade
' color.
Dim colorMatches(15) As KnownColor
Dim count As Integer = 0 ' number of matches found
' Iterate through the KnownColor enums until 15 matches are found.
Dim enumValue As KnownColor
For enumValue = 0 To KnownColor.YellowGreen
someColor = Color.FromKnownColor(enumValue)
If (someColor.GetHue()) = (redShade.GetHue()) Then
colorMatches(count) = enumValue
count += 1
If count > 15 Then
Exit For
End If
End If
Next enumValue
' Display the redShade color and its argb value.
Dim myBrush1 As New SolidBrush(redShade)
Dim myFont As New Font("Arial", 12)
Dim x As Integer = 20
Dim y As Integer = 20
someColor = redShade
g.FillRectangle(myBrush1, x, y, 100, 30)
g.DrawString(someColor.ToString(), myFont, Brushes.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.
Dim i As Integer
For i = 0 To count - 1
y += 40
someColor = Color.FromKnownColor(colorMatches(i))
myBrush1.Color = someColor
g.FillRectangle(myBrush1, x, y, 100, 30)
g.DrawString(someColor.ToString(), myFont, Brushes.Black, _
x + 120, y)
Next i
End Sub
        
[C#] 
public void GetHueExample(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 hue of the redShade
// color.
KnownColor[]  colorMatches = new KnownColor[15];
int  count = 0;   // number of matches found
// Iterate through the KnownColor enums until 15 matches are found.
for (KnownColor enumValue = 0;
enumValue <= KnownColor.YellowGreen && count < 15; enumValue++)
{
someColor = Color.FromKnownColor(enumValue);
if ( someColor.GetHue() == redShade.GetHue() )
colorMatches[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.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(colorMatches[i]);
myBrush1.Color = someColor;
g.FillRectangle(myBrush1, x, y, 100, 30);
g.DrawString(someColor.ToString(), myFont, Brushes.Black, x + 120, y);
}
}
        

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Color Structure | Color Members | System.Drawing Namespace

Show: