英語で読む

次の方法で共有


Color.FromArgb メソッド

定義

4 つの 8 ビット ARGB コンポーネント (アルファ、赤、緑、青) の値から Color 構造体を作成します。

オーバーロード

FromArgb(Int32, Int32, Int32, Int32)

4 つの ARGB コンポーネント (アルファ、赤、緑、青) の値から Color 構造体を作成します。 このメソッドでは、コンポーネントごとに 32 ビットの値を渡すことができますが、各コンポーネントの値は 8 ビットに制限されます。

FromArgb(Int32, Int32, Int32)

指定した 8 ビットカラー値 (赤、緑、青) から Color 構造体を作成します。 アルファ値は暗黙的に 255 (完全に不透明) です。 このメソッドでは、カラー コンポーネントごとに 32 ビット値を渡すことができますが、各コンポーネントの値は 8 ビットに制限されます。

FromArgb(Int32, Color)

指定した Color 構造体から、新しく指定したアルファ値を使用して、Color 構造体を作成します。 このメソッドでは、アルファ値に対して 32 ビット値を渡すことができますが、値は 8 ビットに制限されます。

FromArgb(Int32)

32 ビット ARGB 値から Color 構造体を作成します。

FromArgb(Int32, Int32, Int32, Int32)

ソース:
Color.cs
ソース:
Color.cs
ソース:
Color.cs

4 つの ARGB コンポーネント (アルファ、赤、緑、青) の値から Color 構造体を作成します。 このメソッドでは、コンポーネントごとに 32 ビットの値を渡すことができますが、各コンポーネントの値は 8 ビットに制限されます。

public static System.Drawing.Color FromArgb (int alpha, int red, int green, int blue);

パラメーター

alpha
Int32

アルファ コンポーネント。 有効な値は 0 から 255 です。

red
Int32

赤いコンポーネント。 有効な値は 0 から 255 です。

green
Int32

緑のコンポーネント。 有効な値は 0 から 255 です。

blue
Int32

青いコンポーネント。 有効な値は 0 から 255 です。

戻り値

このメソッドが作成する Color

例外

alpharedgreen、または blue が 0 より小さいか、255 より大きい。

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

  • それぞれ異なる色の 3 つのブラシを作成します。 ブラシの作成に使用される各 Color 構造は、4 つのコンポーネント値 (アルファ、赤、緑、青) から作成されます。

  • 虚数三角形を使用して 3 つの円を配置します。

  • 円ごとに異なるブラシを使用して、三角形の 1 つの頂点を中心とする 3 つの重なり合う円を塗りつぶします。

public void FromArgb1(PaintEventArgs e)
{
    Graphics     g = e.Graphics;
             
    // Transparent red, green, and blue brushes.
    SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(120, 255, 0, 0));
    SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(120, 0, 255, 0));
    SolidBrush trnsBlueBrush = new SolidBrush(Color.FromArgb(120, 0, 0, 255));
             
    // Base and height of the triangle that is used to position the
    // circles. Each vertex of the triangle is at the center of one of the
    // 3 circles. The base is equal to the diameter of the circles.
    float   triBase = 100;
    float   triHeight = (float)Math.Sqrt(3*(triBase*triBase)/4);
             
    // Coordinates of first circle's bounding rectangle.
    float   x1 = 40;
    float   y1 = 40;
             
    // Fill 3 over-lapping circles. Each circle is a different color.
    g.FillEllipse(trnsRedBrush, x1, y1, 2*triHeight, 2*triHeight);
    g.FillEllipse(trnsGreenBrush, x1 + triBase/2, y1 + triHeight,
        2*triHeight, 2*triHeight);
    g.FillEllipse(trnsBlueBrush, x1 + triBase, y1, 2*triHeight, 2*triHeight);
}

注釈

不透明な色を作成するには、alpha を 255 に設定します。 半透明の色を作成するには、alpha を 1 ~ 254 の任意の値に設定します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.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

FromArgb(Int32, Int32, Int32)

ソース:
Color.cs
ソース:
Color.cs
ソース:
Color.cs

指定した 8 ビットカラー値 (赤、緑、青) から Color 構造体を作成します。 アルファ値は暗黙的に 255 (完全に不透明) です。 このメソッドでは、カラー コンポーネントごとに 32 ビット値を渡すことができますが、各コンポーネントの値は 8 ビットに制限されます。

public static System.Drawing.Color FromArgb (int red, int green, int blue);

パラメーター

red
Int32

新しい Colorの赤いコンポーネント値。 有効な値は 0 から 255 です。

green
Int32

新しい Colorの緑のコンポーネント値。 有効な値は 0 から 255 です。

blue
Int32

新しい Colorの青いコンポーネント値。 有効な値は 0 から 255 です。

戻り値

このメソッドが作成する Color

例外

redgreen、または blue が 0 より小さいか、255 より大きい。

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

  1. 3 つのカラー コンポーネント値 (赤、緑、青) から Color 構造体を作成します。 プライマリ カラーごとに 1 つずつ、3 つの Color 構造が作成されます。

  2. アルファ値の範囲を反復処理し、色のアルファ値を変更します。

  3. 各イテレーション中に、ブラシの色を変更された色に設定し、四角形を描画して色を表示します。

  4. プライマリ カラーごとに手順 2 と 3 を繰り返します。

アルファ値が完全に不透明になることがなく、四角形が重なっているので、色の組み合わせ効果が得られます。

public void FromArgb2(PaintEventArgs e)
{
    Graphics     g = e.Graphics;
             
    // Opaque colors (alpha value defaults to 255 -- max value).
    Color red = Color.FromArgb(255, 0, 0);
    Color green = Color.FromArgb(0, 255, 0);
    Color blue = Color.FromArgb(0, 0, 255);
             
    // Solid brush initialized to red.
    SolidBrush  myBrush = new SolidBrush(red);
    int alpha;

    // x coordinate of first red rectangle
    int x = 50;         
    
    // y coordinate of first red rectangle
    int y = 50;         
                   
    // Fill rectangles with red, varying the alpha value from 25 to 250.
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, red);
        g.FillRectangle(myBrush, x, y, 50, 100);
        g.FillRectangle(myBrush, x, y + 250, 50, 50);
        x += 50;
    }
    // x coordinate of first green rectangle.
    x = 50;             
    
    // y coordinate of first green rectangle.
    y += 50;            
                      
    // Fill rectangles with green, varying the alpha value from 25 to 250.
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, green);
        g.FillRectangle(myBrush, x, y, 50, 150);
        x += 50;
    }
    // x coordinate of first blue rectangle.
    x = 50;             
    
    // y coordinate of first blue rectangle.
    y += 100;           
             
    // Fill rectangles with blue, varying the alpha value from 25 to 250.
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, blue);
        g.FillRectangle(myBrush, x, y, 50, 150);
        x += 50;
    }
}

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.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

FromArgb(Int32, Color)

ソース:
Color.cs
ソース:
Color.cs
ソース:
Color.cs

指定した Color 構造体から、新しく指定したアルファ値を使用して、Color 構造体を作成します。 このメソッドでは、アルファ値に対して 32 ビット値を渡すことができますが、値は 8 ビットに制限されます。

public static System.Drawing.Color FromArgb (int alpha, System.Drawing.Color baseColor);

パラメーター

alpha
Int32

新しい Colorのアルファ値。 有効な値は 0 から 255 です。

baseColor
Color

新しい Colorの作成元となる Color

戻り値

このメソッドが作成する Color

例外

alpha が 0 未満または 255 より大きい。

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

  1. 3 つのカラー コンポーネント値 (赤、緑、青) から Color 構造体を作成します。 プライマリ カラーごとに 1 つずつ、3 つの Color 構造が作成されます。

  2. アルファ値の範囲を反復処理し、色のアルファ値を変更します。

  3. 各イテレーション中に、ブラシの色を変更された色に設定し、四角形を描画して色を表示します。

  4. プライマリ カラーごとに手順 2 と 3 を繰り返します。

アルファ値が完全に不透明になることがなく、四角形が重なっているので、色の組み合わせ効果が得られます。

public void FromArgb3(PaintEventArgs e)
{
    Graphics     g = e.Graphics;
             
    // Opaque colors (alpha value defaults to 255 -- max value).
    Color red = Color.FromArgb(255, 0, 0);
    Color green = Color.FromArgb(0, 255, 0);
    Color blue = Color.FromArgb(0, 0, 255);
             
    // Solid brush initialized to red.
    SolidBrush  myBrush = new SolidBrush(red);
    int alpha;
    
    // x coordinate of first red rectangle
    int x = 50;         
    
    // y coordinate of first red rectangle
    int y = 50;         
    
    // Fill rectangles with red, varying the alpha value from 25 to 250.
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, red);
        g.FillRectangle(myBrush, x, y, 50, 100);
        g.FillRectangle(myBrush, x, y + 250, 50, 50);
        x += 50;
    }
    // x coordinate of first green rectangle
    x = 50;             
    
    // y coordinate of first green rectangle
    y += 50;            
    
    // Fill rectangles with green, varying the alpha value from 25 to 250.
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, green);
        g.FillRectangle(myBrush, x, y, 50, 150);
        x += 50;
    }
    // x coordinate of first blue rectangle.
    x = 50; 
    
     // y coordinate of first blue rectangle
    y += 100;           

    // Fill rectangles with blue, varying the alpha value from 25 to 250.
    for (alpha = 25; alpha <= 250; alpha += 25)
    {
        myBrush.Color = Color.FromArgb(alpha, blue);
        g.FillRectangle(myBrush, x, y, 50, 150);
        x += 50;
    }
}

注釈

不透明な色を作成するには、alpha を 255 に設定します。 半透明の色を作成するには、alpha を 1 ~ 254 の任意の値に設定します。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.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

FromArgb(Int32)

ソース:
Color.cs
ソース:
Color.cs
ソース:
Color.cs

32 ビット ARGB 値から Color 構造体を作成します。

public static System.Drawing.Color FromArgb (int argb);

パラメーター

argb
Int32

32 ビットの ARGB 値を指定する値。

戻り値

このメソッドが作成する Color 構造体。

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

  • それぞれ異なる色の 3 つのブラシを作成します。 ブラシの作成に使用される各 Color 構造体は、32 ビットの ARGB 値から作成されます。

  • 虚数三角形を使用して 3 つの円を配置します。

  • 円ごとに異なるブラシを使用して、三角形の 1 つの頂点を中心とする 3 つの重なり合う円を塗りつぶします。

public void FromArgb4(PaintEventArgs e)
{
    Graphics     g = e.Graphics;
             
    // Transparent red, green, and blue brushes.
    SolidBrush trnsRedBrush = new SolidBrush(Color.FromArgb(0x78FF0000));
    SolidBrush trnsGreenBrush = new SolidBrush(Color.FromArgb(0x7800FF00));
    SolidBrush trnsBlueBrush = new SolidBrush(Color.FromArgb(0x780000FF));
             
    // Base and height of the triangle that is used to position the
    // circles. Each vertex of the triangle is at the center of one of the
    // 3 circles. The base is equal to the diameter of the circles.
    float   triBase = 100;
    float   triHeight = (float)Math.Sqrt(3*(triBase*triBase)/4);
             
    // coordinates of first circle's bounding rectangle.
    float   x1 = 40;
    float   y1 = 40;
             
    // Fill 3 over-lapping circles. Each circle is a different color.
    g.FillEllipse(trnsRedBrush, x1, y1, 2*triHeight, 2*triHeight);
    g.FillEllipse(trnsGreenBrush, x1 + triBase/2, y1 + triHeight,
        2*triHeight, 2*triHeight);
    g.FillEllipse(trnsBlueBrush, x1 + triBase, y1, 2*triHeight, 2*triHeight);
}

注釈

32 ビット ARGB 値のバイト順序は AARRGGBB です。 AA で表される最上位バイト (MSB) はアルファ コンポーネント値です。 RR、GG、BB で表される 2 番目、3 番目、4 番目のバイトはそれぞれ、赤、緑、青の色成分です。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.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