public static Texture GenerateFieldGradient(Color baseColor, Field field, int resolution = 8, bool vertical = true)
        {
            int width  = vertical ? 1 : resolution;
            int height = vertical ? resolution : 1;

            Texture2D texture = new Texture2D(width, height);

            texture.hideFlags = HideFlags.DontSave;
            texture.wrapMode  = TextureWrapMode.Clamp;

            //TODO: Use SetPixels instead of SetPixel, for better performance.
            baseColor.a = 1f;
            for (int i = 0; i < resolution; i++)
            {
                Color color = new Color();
                float perc  = i / (float)(resolution - 1);
                switch (field)
                {
                case Field.Red:
                    color = ColorConvert.ChangeRGB(baseColor, newR: perc);
                    break;

                case Field.Green:
                    color = ColorConvert.ChangeRGB(baseColor, newG: perc);
                    break;

                case Field.Blue:
                    color = ColorConvert.ChangeRGB(baseColor, newB: perc);
                    break;

                case Field.Alpha:
                    color = ColorConvert.ChangeRGB(baseColor, newA: perc);
                    break;

                case Field.Hue:
                    color = ColorConvert.ChangeHSV(baseColor, newH: perc);
                    break;

                case Field.HSV_Saturation:
                    color = ColorConvert.ChangeHSV(baseColor, newS: perc);
                    break;

                case Field.HSV_Value:
                    color = ColorConvert.ChangeHSV(baseColor, newV: perc);
                    break;

                case Field.HSL_Saturation:
                    color = ColorConvert.ChangeHSL(baseColor, newS: perc);
                    break;

                case Field.HSL_Lightness:
                    color = ColorConvert.ChangeHSL(baseColor, newL: perc);
                    break;
                }
                if (vertical)
                {
                    texture.SetPixel(0, i, color);
                }
                else
                {
                    texture.SetPixel(i, 0, color);
                }
            }

            texture.Apply();
            return(texture);
        }
Beispiel #2
0
        public void SetField(Field field, float value)
        {
            Color newRgb = rgb;

            //Avoids true internal blacks, whites and grays.
            //This prevents those colors from "locking" saturation and hue.
            //The change won't be enough to modify HTML/255/359 values.
            if (value < 0.0010f)
            {
                value = 0.0010f;
            }
            if (value > 0.9990f)
            {
                value = 0.9990f;
            }

            switch (field)
            {
            case Field.Red:
                //This prevents internal grays through RGB. Check note above.
                if (value.EqualsAll(RGB.g, RGB.b))
                {
                    value += 0.0001f;
                }
                newRgb = ColorConvert.ChangeRGB(RGB, newR: value);
                break;

            case Field.Green:
                //This prevents internal grays through RGB. Check note above.
                if (value.EqualsAll(RGB.r, RGB.b))
                {
                    value += 0.0001f;
                }
                newRgb = ColorConvert.ChangeRGB(RGB, newG: value);
                break;

            case Field.Blue:
                //This prevents internal grays through RGB. Check note above.
                if (value.EqualsAll(RGB.g, RGB.r))
                {
                    value += 0.0001f;
                }
                newRgb = ColorConvert.ChangeRGB(RGB, newB: value);
                break;

            case Field.Alpha:
                newRgb = ColorConvert.ChangeRGB(RGB, newA: value);
                break;

            case Field.Hue:
                newRgb = ColorConvert.ChangeHSV(RGB, newH: value);
                break;

            case Field.HSV_Saturation:
                newRgb = ColorConvert.ChangeHSV(RGB, newS: value);
                break;

            case Field.HSV_Value:
                newRgb = ColorConvert.ChangeHSV(RGB, newV: value);
                break;

            case Field.HSL_Saturation:
                newRgb = ColorConvert.ChangeHSL(RGB, newS: value);
                break;

            case Field.HSL_Lightness:
                newRgb = ColorConvert.ChangeHSL(RGB, newL: value);
                break;
            }

            SetRGB(newRgb, checkValues: false);
        }