Color.GetBrightness Metodo
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Ottiene il valore di leggerezza della saturazione delle tonalità (HSL) per questa struttura Color.
public:
float GetBrightness();
public float GetBrightness ();
member this.GetBrightness : unit -> single
Public Function GetBrightness () As Single
Restituisce
La leggerezza di questa Color. La leggerezza varia da 0,0 a 1,0, dove 0,0 rappresenta il nero e 1,0 rappresenta il bianco.
Esempio
L'esempio di codice seguente è progettato per l'uso con Windows Form e richiede PaintEventArgse
, che è un parametro del gestore eventi Paint. Il codice esegue le azioni seguenti:
Crea un'istanza di una struttura Color,
redShade
, da usare per i confronti.Scorre gli elementi di enumerazione KnownColor per trovare tutti i colori noti con la stessa leggerezza di
redShade
. Le iterazioni vengono terminate quando vengono trovate 15 corrispondenze o il valore del contatore del ciclo è maggiore dell'ultimo elemento KnownColor.Durante ogni iterazione, salva l'elemento KnownColor, se corrisponde ai criteri, in una matrice.
Usa un pennello per disegnare rettangoli.
Il primo rettangolo viene disegnato il colore rappresentato da redShade
. Ognuno degli altri rettangoli viene dipinto un KnownColor che corrisponde alla leggerezza del 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.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 = 0;
enumValue <= KnownColor.YellowGreen && count < 15; enumValue++)
{
someColor = Color.FromKnownColor(enumValue);
if (someColor.GetBrightness() == redShade.GetBrightness())
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);
}
}
Public Sub KnownColorBrightnessExample2(ByVal 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 to store KnownColor values that match the brightness of the
' redShade color.
Dim colorMatches(15) As KnownColor
' Number of matches found.
Dim count As Integer = 0
' 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.GetBrightness()) = (redShade.GetBrightness()) 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.
' 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