次の方法で共有


Color.GetBrightness メソッド

この Color 構造体の、HSB (hue-saturation-brightness) の明るさの値を取得します。

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

戻り値

この Color 構造体の明るさ。明るさの範囲は 0.0 から 1.0 で、0.0 は黒を、1.0 は白を表します。

使用例

[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードは次のアクションを実行します。

  • 比較に使用する Color 構造体のインスタンス、 redShade を作成します。
  • KnownColor 列挙体要素を反復処理して、 redShade と同じ明るさの KnownColors をすべて検索します。反復処理は、15 の一致が見つかった場合や、ループ カウンタの値が直前の KnownColor 要素よりも大きくなった場合に終了します。
  • 各反復処理中に、基準と一致する場合は、配列の KnownColor 要素を保存します。
  • ブラシを使用して四角形を塗りつぶします。

[Visual Basic, C#] 最初の四角形は、 redShade が表す色で塗りつぶします。残りの各四角形は、 redShade の明るさと一致する KnownColor で塗りつぶされます。

 
Public Sub KnownColorBrightnessExample(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
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.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
        
[C#] 
public void KnownColorBrightnessExample(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];
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.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);
}
}
        

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Color 構造体 | Color メンバ | System.Drawing 名前空間