英語で読む

次の方法で共有


Color.GetHue メソッド

定義

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

C#
public float GetHue ();

戻り値

この Colorの色合い (度単位)。 色相は、HSL 色空間で 0.0 から 360.0 までの範囲の度数で測定されます。

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

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

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

  • 反復処理のたびに、KnownColor 要素 (条件と一致する場合) を配列に保存します。

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

最初の四角形は、redShadeで表される色で塗りつぶされます。 他の四角形はそれぞれ、redShadeの色合いに一致する KnownColor が描画されます。

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];

    // 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.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);
    }
}

適用対象

製品 バージョン
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1