Color.GetSaturation メソッド

定義

この Color 構造体の HSL (色相値-彩度値-輝度値) の彩度値を取得します。

public:
 float GetSaturation();
public float GetSaturation ();
member this.GetSaturation : unit -> single
Public Function GetSaturation () As Single

戻り値

この Color の鮮やかさ。 鮮やかさの範囲は 0.0 から 1.0 で、0.0 はグレースケールを、1.0 は最も鮮やかであることを示します。

次のコード例は、Windows フォームで使用するように設計されており、イベント ハンドラーのPaintパラメーターである が必要PaintEventArgseです。 コードは、次のアクションを実行します。

  • 比較に使用する 構造体 redShadeColorインスタンスを作成します。

  • 列挙要素を反復処理して、 KnownColor と同じ彩度 redShadeを持つ既知のすべての色を検索します。 イテレーションは、15 個の一致が見つかった場合、またはループ カウンターの値が最後 KnownColor の要素より大きい場合に終了します。

  • 各反復の間に、 要素が条件と KnownColor 一致する場合は、 配列に保存されます。

  • ブラシを使用して四角形を塗りつぶします。

最初の四角形は、 で表される色で redShade塗りつぶされます。 他の各四角形には、 の彩度に一致する が描画されますKnownColorredShade

void GetSatExample( 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 saturation 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.GetSaturation() == redShade.GetSaturation() )
               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 GetSatExample(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 saturation 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.GetSaturation() == redShade.GetSaturation())
            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 GetSatExample(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 saturation 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.GetSaturation()) = (redShade.GetSaturation()) 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

適用対象