カラーピッカーのHueサークル画像を作成するスクリプト
Hue circle texture generate by script

ペイントツールとかでは、こういうカラーピッカーが一般的かと思います。

↑ PaintTool SAI より

この外側の輪っかがHue(色相)サークルです。

このHueサークルの元にするための画像をコードから生成してみます。


Texture2D tex = new Texture2D(256,256);

                    

for (int y = 0; y < tex.height; y++)

{

    for (int x = 0; x < tex.width; x++)

    {

        Vector2 vec = new Vector2(x, y) - (new Vector2(tex.width, tex.height)*0.5f);

        float angle = Mathf.Atan2(vec.x, vec.y) * Mathf.Rad2Deg;

        if (angle < 0) angle += 360;

        angle = angle / 360f;


        Color c = Color.HSVToRGB(angle,1,1);

        tex.SetPixel(x, y, c);

    }

}

出力されたテクスチャ


この画像を円環状にマスクするとかして使うといい感じになりそうです。


今回はついでに円環状に切り取るところまでやります。

円環状のHueサークルを出力する

メソッド化しました。

public Texture2D CreateHueCircle(int size = 256, float widthPerc = 0.2f)

{

    Texture2D tex = new Texture2D(size,size);

    float sizeHalf = size * 0.5f;


    for (int y = 0; y < tex.height; y++)

    {

        for (int x = 0; x < tex.width; x++)

        {

            //テクスチャの中心位置

            Vector2 center = new Vector2(tex.width, tex.height)*0.5f;


            //中心→(x,y)へのベクトル

            Vector2 vec = new Vector2(x, y) - center;


            //そのベクトルから角度を求める

            float angle = Mathf.Atan2(vec.x, vec.y) * Mathf.Rad2Deg;

            if (angle < 0) angle += 360;

            angle = angle / 360f;


            //背景カラーは完全透明

            Color c = Color.clear;


            //サークル部のみHue色をつける

            if(sizeHalf*(1-widthPerc) < vec.magnitude && vec.magnitude < sizeHalf) c = Color.HSVToRGB(angle, 1, 1);


            //テクスチャに書き込む

            tex.SetPixel(x, y, c);

        }

    }


    return tex;

}

出力されたテクスチャ

アンチエイリアスがないのでギザギザしてて実用性は微妙ですが・・・

ちょっと工夫すればなんか使えるかもしれません