private void ConvertToTexture(GradientMode mode)
        {
            var colorKeys = inputGradientField.value.colorKeys;
            var alphaKeys = inputGradientField.value.alphaKeys;

            var colors            = new Color[colorKeys.Length];
            var colorKeyLocations = new float[colorKeys.Length];

            for (int i = 0; i < colorKeys.Length; i++)
            {
                colors[i]            = colorKeys[i].color;
                colorKeyLocations[i] = colorKeys[i].time;
            }

            var alphas            = new float[alphaKeys.Length];
            var alphaKeyLocations = new float[alphaKeys.Length];

            for (int i = 0; i < alphaKeys.Length; i++)
            {
                alphas[i]            = alphaKeys[i].alpha;
                alphaKeyLocations[i] = alphaKeys[i].time;
            }

            BetterGradient.GradientToColors(colors, colorKeyLocations, alphas, alphaKeyLocations, gradientColors, mode);
            gradientTexture.SetPixels(gradientColors);
            gradientTexture.Apply();
            outputGradientTexture.style.backgroundImage = gradientTexture;

            var error = BetterGradient.ApproximateColorArrayAsGradient(gradientColors, approxOutput, approxColorKeys, approxAlphaKeys);

            Debug.Log(error);
            outputGradientField.value = approxOutput;
        }
        protected override void ReadFromImpl(object obj)
        {
            base.ReadFromImpl(obj);
            Gradient uo = (Gradient)obj;

            mode = uo.mode;
        }
Example #3
0
        void mMarqueeTimeer_Tick(object sender, EventArgs e)
        {
            mPosition      = 100;
            mGradientStyle = GradientMode.Vertical;

            switch (mMarqueeAnim++)
            {
            case 0:
                ColorBarCenter = System.Drawing.Color.FromArgb(((int)(((byte)(3)))), ((int)(((byte)(79)))), ((int)(((byte)(134)))));
                break;

            case 1:
                ColorBarCenter = System.Drawing.Color.FromArgb(((int)(((byte)(4)))), ((int)(((byte)(92)))), ((int)(((byte)(156)))));
                break;

            case 2:
                ColorBarCenter = System.Drawing.Color.FromArgb(((int)(((byte)(3)))), ((int)(((byte)(106)))), ((int)(((byte)(179)))));
                break;

            case 3:
                ColorBarCenter = System.Drawing.Color.FromArgb(((int)(((byte)(5)))), ((int)(((byte)(119)))), ((int)(((byte)(202)))));
                break;

            case 4:
                ColorBarCenter = System.Drawing.Color.FromArgb(((int)(((byte)(5)))), ((int)(((byte)(133)))), ((int)(((byte)(225)))));
                mMarqueeAnim   = 0;
                break;
            }

            this.Invalidate();
        }
 internal GradientShaderFactory(int[] colors, float[] locations, Shader.TileMode tileMode,
                                GradientMode gradientMode)
 {
     _colors       = colors;
     _locations    = locations;
     _tileMode     = tileMode;
     _gradientMode = gradientMode;
 }
        public static void LineCaps(GradientMode gradientMode, PenLineCap lineCap)
        {
            using var app = Application.AttachOrLaunch("Gu.Wpf.Geometry.Demo.exe", WindowName);
            var window = app.MainWindow;

            _ = window.FindComboBox("LineCap").Select(lineCap.ToString());
            _ = window.FindComboBox("GradientMode").Select(gradientMode.ToString());
            ImageAssert.AreEqual($"Images\\GradientPathLineCapsWindow\\{TestImage.Current}\\{gradientMode}_{lineCap}.png", window.FindGroupBox("Path"), TestImage.OnFail);
        }
Example #6
0
        public wGradient(List <System.Drawing.Color> GradientColors, List <double> GradientParameters, wDomain GradientLocation, wDomain GradientFocus, double GradientRadius)
        {
            ColorSet.Clear();
            ParameterSet = GradientParameters;

            for (int i = 0; i < GradientColors.Count; i++)
            {
                ColorSet.Add(new wColor(GradientColors[i]));
            }

            Radius   = GradientRadius;
            Location = GradientLocation;
            Focus    = GradientFocus;
            Mode     = GradientMode.Radial;
        }
Example #7
0
        public SerializableGradient(Gradient g)
        {
            colorKeys = new SerializableGradientColorKey[g.colorKeys.Length];
            for (int i = 0; i < g.colorKeys.Length; i++)
            {
                colorKeys[i] = (SerializableGradientColorKey)g.colorKeys[i];
            }

            alphaKeys = new SerializableGradientAlphaKey[g.alphaKeys.Length];
            for (int i = 0; i < g.alphaKeys.Length; i++)
            {
                alphaKeys[i] = (SerializableGradientAlphaKey)g.alphaKeys[i];
            }
            mode = g.mode;
        }
        public void LineCaps(GradientMode gradientMode, PenLineCap lineCap)
        {
            if (Env.IsAppVeyor)
            {
                return;
            }

            using (var app = Application.Launch("Gu.Wpf.Geometry.Demo.exe", "GradientPathLineCapsWindow"))
            {
                var window = app.MainWindow;
                _ = window.FindComboBox("LineCap").Select(lineCap.ToString());
                _ = window.FindComboBox("GradientMode").Select(gradientMode.ToString());

                ImageAssert.AreEqual($"GradientPathLineCap_{gradientMode}_{lineCap}.png", window.FindGroupBox("Path"));
            }
        }
Example #9
0
            public unsafe NativeGradient(Gradient gradient, Allocator allocator)
            {
                _gradientMode = gradient.mode;
                var gradientColorKeys = gradient.colorKeys;

                _gradientColorKeys = new UnsafeArrayList <GradientColorKey>(
                    gradientColorKeys.Length,
                    allocator,
                    MemoryOptions.UninitializedMemory
                    );

                fixed(GradientColorKey *items = &gradient.colorKeys[0])
                {
                    _gradientColorKeys.CopyFrom(items, gradientColorKeys.Length);
                }
            }
Example #10
0
        public wGradient(List <System.Drawing.Color> GradientColors, wDomain GradientLocation, wDomain GradientFocus, double GradientRadius, GradientSpace Extents)
        {
            ColorSet.Clear();

            ParameterSet.Clear();
            for (int i = 0; i < GradientColors.Count; i++)
            {
                ColorSet.Add(new wColor(GradientColors[i]));
                ParameterSet.Add((1.0 / GradientColors.Count) * (double)i);
            }

            Radius   = GradientRadius;
            Location = GradientLocation;
            Focus    = GradientFocus;
            FillMode = Extents;

            Mode = GradientMode.Radial;
        }
Example #11
0
        public static Gradient CreateGradient(GradientMode mode, params KeyValuePair <float, Color>[] datas)
        {
            Gradient grad = new Gradient();

            GradientColorKey[] colorKeys = new GradientColorKey[datas.Length];
            GradientAlphaKey[] alphaKeys = new GradientAlphaKey[datas.Length];

            for (int i = 0; i < datas.Length; i++)
            {
                colorKeys[i].time  = datas[i].Key;
                colorKeys[i].color = datas[i].Value;
                alphaKeys[i].time  = datas[i].Key;
                alphaKeys[i].alpha = datas[i].Value.a;
            }
            grad.SetKeys(colorKeys, alphaKeys);
            grad.mode = mode;

            return(grad);
        }
Example #12
0
        public GradientMode GetGradientMode()
        {
            GradientMode gradientMode = new GradientMode();

            switch (mode)
            {
            case "Blend":
            {
                gradientMode = GradientMode.Blend;
                break;
            }

            case "Fixed":
            {
                gradientMode = GradientMode.Fixed;
                break;
            }
            }
            return(gradientMode);
        }
    public void Generate()
    {
        var gradients = new List <Gradient>();

        for (int k = 0; k < gradientSpecs.Length; k++)
        {
            var spec = gradientSpecs[k];

            var eightRainbow = Enumerable.Range(0, 9).Select(x => Mathf.InverseLerp(0, 9, x)).Take(8).Select(x =>
                                                                                                             Color.HSVToRGB(spec.hProgression.Evaluate(x), spec.sProgression.Evaluate(x), spec.vProgression.Evaluate(x))).ToArray();

            for (int mode = 0; mode < 2; mode++)
            {
                GradientMode gm       = (GradientMode)mode;
                var          modePlus = gm == GradientMode.Fixed ? 1 : 0;
                for (int i = 1; i < 8; i++)
                {
                    var timeRange = Enumerable.Range(0, i + 1).Select(x => Mathf.InverseLerp(0, i + modePlus, x + modePlus)).ToArray();
                    var g         = new Gradient();
                    g.mode = gm;
                    var colorKeys = new GradientColorKey[i + 1];
                    var alphaKeys = new GradientAlphaKey[i + 1];
                    for (int j = 0; j < i + 1; j++)
                    {
                        colorKeys[j].color = eightRainbow[j];
                        colorKeys[j].time  = spec.timeRemap.Evaluate(timeRange[j]);
                        alphaKeys[j].alpha = spec.aProgression.Evaluate(timeRange[j]);
                        alphaKeys[j].time  = spec.timeRemap.Evaluate(timeRange[j]);
                    }
                    g.SetKeys(colorKeys, alphaKeys);
                    gradients.Add(g);
                }
            }
        }
        generatedGradients = gradients.ToArray();
#if UNITY_EDITOR
        EditorUtility.SetDirty(this);
        AssetDatabase.SaveAssets();
#endif
    }
 private void BuildArrays()
 {
     if (this.m_Gradient != null)
     {
         GradientColorKey[] colorKeys = this.m_Gradient.colorKeys;
         this.m_RGBSwatches = new List<Swatch>(colorKeys.Length);
         for (int i = 0; i < colorKeys.Length; i++)
         {
             Color color = colorKeys[i].color;
             color.a = 1f;
             this.m_RGBSwatches.Add(new Swatch(colorKeys[i].time, color, false));
         }
         GradientAlphaKey[] alphaKeys = this.m_Gradient.alphaKeys;
         this.m_AlphaSwatches = new List<Swatch>(alphaKeys.Length);
         for (int j = 0; j < alphaKeys.Length; j++)
         {
             float alpha = alphaKeys[j].alpha;
             this.m_AlphaSwatches.Add(new Swatch(alphaKeys[j].time, new Color(alpha, alpha, alpha, 1f), true));
         }
         this.m_GradientMode = this.m_Gradient.mode;
     }
 }
        protected override void OnElementChanged(ElementChangedEventArgs <StackLayout> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null || Element == null)
            {
                return;
            }

            try
            {
                if (e.NewElement is GradientLayout layout)
                {
                    Colors = layout.Colors;
                    Mode   = layout.Mode;
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(@"ERROR:", ex.Message);
            }
        }
Example #16
0
 private void BuildArrays()
 {
     if (this.m_Gradient != null)
     {
         GradientColorKey[] colorKeys = this.m_Gradient.colorKeys;
         this.m_RGBSwatches = new List <GradientEditor.Swatch>(colorKeys.Length);
         for (int i = 0; i < colorKeys.Length; i++)
         {
             Color color = colorKeys[i].color;
             color.a = 1f;
             this.m_RGBSwatches.Add(new GradientEditor.Swatch(colorKeys[i].time, color, false));
         }
         GradientAlphaKey[] alphaKeys = this.m_Gradient.alphaKeys;
         this.m_AlphaSwatches = new List <GradientEditor.Swatch>(alphaKeys.Length);
         for (int j = 0; j < alphaKeys.Length; j++)
         {
             float alpha = alphaKeys[j].alpha;
             this.m_AlphaSwatches.Add(new GradientEditor.Swatch(alphaKeys[j].time, new Color(alpha, alpha, alpha, 1f), true));
         }
         this.m_GradientMode = this.m_Gradient.mode;
     }
 }
    void SetGradient()
    {
        GradientMode gm = GradientMode.None;

        if (IsKeywordEnabled("HEIGHT_GRADIENT"))
        {
            gm = GradientMode.Height;
        }
        if (IsKeywordEnabled("RADIAL_GRADIENT"))
        {
            gm = GradientMode.Radial;
        }

        EditorGUI.BeginChangeCheck();
        gm = (GradientMode)EditorGUILayout.EnumPopup("Gradient Mode", gm);
        if (EditorGUI.EndChangeCheck())
        {
            editor.RegisterPropertyChangeUndo("Gradient Mode");
            SetKeyword("HEIGHT_GRADIENT", gm == GradientMode.Height);
            SetKeyword("RADIAL_GRADIENT", gm == GradientMode.Radial);
            SetKeyword("NO_GRADIENT", gm == GradientMode.None);
        }
    }
    //--------------------------------------------------------------------------------------------------
    // Gradient Serialization

    static Gradient Deserialize(string serializedGradient)
    {
        // format:
        // gradient:colorKey0,colorTime0,colorKey1,colorTime1,...;alphaKey0,alphaTime0,alphaKey1,alphaTime1...;gradientMode

        var split     = serializedGradient.Substring("gradient:".Length).Split(';');
        var colorKeys = new List <GradientColorKey>();
        var alphaKeys = new List <GradientAlphaKey>();

        // parse color keys:
        var colorKeysStr = split[0].Split(',');

        for (int i = 0; i < colorKeysStr.Length; i += 2)
        {
            colorKeys.Add(new GradientColorKey(HexToColor(colorKeysStr[i]), float.Parse(colorKeysStr[i + 1], CultureInfo.InvariantCulture)));
        }

        // parse alpha keys:
        var alphaKeysStr = split[1].Split(',');

        for (int i = 0; i < alphaKeysStr.Length; i += 2)
        {
            alphaKeys.Add(new GradientAlphaKey(float.Parse(alphaKeysStr[i], CultureInfo.InvariantCulture), float.Parse(alphaKeysStr[i + 1], CultureInfo.InvariantCulture)));
        }

        // parse gradient mode:
        GradientMode mode = (GradientMode)Enum.Parse(typeof(GradientMode), split[2]);

        var gradient = new Gradient()
        {
            colorKeys = colorKeys.ToArray(),
            alphaKeys = alphaKeys.ToArray(),
            mode      = mode
        };

        return(gradient);
    }
Example #19
0
        private JSONObject GetGradientColor(Gradient gcolor)
        {
            JSONObject res      = new JSONObject(JSONObject.Type.OBJECT);
            JSONObject alphaKey = new JSONObject(JSONObject.Type.ARRAY);
            JSONObject colorKey = new JSONObject(JSONObject.Type.ARRAY);

            res.AddField("alpha", alphaKey);
            res.AddField("color", colorKey);
            GradientMode gradientMode = gcolor.mode;

            if (gradientMode == GradientMode.Blend)
            {
                res.AddField("mode", 1);
            }
            if (gradientMode == GradientMode.Fixed)
            {
                res.AddField("mode", 2);
            }
            GradientAlphaKey[] alphaKeys = gcolor.alphaKeys;
            for (var i = 0; i < alphaKeys.Length; i++)
            {
                GradientAlphaKey alphaKeyframe = alphaKeys[i];
                alphaKey.Add(alphaKeyframe.time);
                alphaKey.Add(alphaKeyframe.alpha * 255);
            }
            GradientColorKey[] colorKeys = gcolor.colorKeys;
            for (var i = 0; i < colorKeys.Length; i++)
            {
                GradientColorKey colorKeyframe = colorKeys[i];
                colorKey.Add(colorKeyframe.time);
                colorKey.Add(colorKeyframe.color.r * 255);
                colorKey.Add(colorKeyframe.color.g * 255);
                colorKey.Add(colorKeyframe.color.b * 255);
                colorKey.Add(colorKeyframe.color.a * 255);
            }
            return(res);
        }
Example #20
0
        void BuildArrays()
        {
            if (m_Gradient == null)
            {
                return;
            }
            GradientColorKey[] colorKeys = m_Gradient.colorKeys;
            m_RGBSwatches = new List <Swatch>(colorKeys.Length);
            for (int i = 0; i < colorKeys.Length; i++)
            {
                Color color = colorKeys[i].color;
                color.a = 1f;
                m_RGBSwatches.Add(new Swatch(colorKeys[i].time, color, false));
            }

            GradientAlphaKey[] alphaKeys = m_Gradient.alphaKeys;
            m_AlphaSwatches = new List <Swatch>(alphaKeys.Length);
            for (int i = 0; i < alphaKeys.Length; i++)
            {
                float a = alphaKeys[i].alpha;
                m_AlphaSwatches.Add(new Swatch(alphaKeys[i].time, new Color(a, a, a, 1), true));
            }
            m_GradientMode = m_Gradient.mode;
        }
Example #21
0
        public static void DrawRadius(Graphics g, System.Drawing.Rectangle rect, RadiusMode radiusMode, int radius, Color c1, Color c2, GradientMode linearGradientMode, Color borderColor, int borderWidth)
        {
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            using (System.Drawing.Drawing2D.GraphicsPath graphPath = GetPath(rect, radiusMode, radius))
            {
                if (rect.Width == 0)
                {
                    rect.Width += 1;
                }
                if (rect.Height == 0)
                {
                    rect.Height += 1;
                }

                System.Drawing.Drawing2D.LinearGradientBrush brush;
                if (linearGradientMode == GradientMode.None)
                {
                    brush = new System.Drawing.Drawing2D.LinearGradientBrush(rect, c1, c1, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
                }
                else
                {
                    brush = new System.Drawing.Drawing2D.LinearGradientBrush(rect, c1, c2.IsEmpty ? c1 : c2, ((System.Drawing.Drawing2D.LinearGradientMode)linearGradientMode));
                }

                if (radiusMode != RadiusMode.None && radius > 0)
                {
                    g.FillPath(brush, graphPath);

                    using (System.Drawing.Pen borderPen = new System.Drawing.Pen(borderColor, borderWidth))
                    {
                        g.DrawPath(borderPen, graphPath);
                        borderPen.Dispose();
                    }
                }
                else
                {
                    g.FillRectangle(brush, rect);

                    if (borderWidth > 0 && !borderColor.IsEmpty)
                    {
                        ControlPaint.DrawBorder(g, rect, borderColor, ButtonBorderStyle.Solid);
                    }
                }

                brush.Dispose();
            }
        }
Example #22
0
 public static void DrawRadius(Graphics graphics, Rectangle rect, RadiusMode radiusMode, int radius, Color backColor, Color backColorGradient, GradientMode gradient, object color, int width)
 {
     throw new NotImplementedException();
 }
 public void OnGUI(Rect position)
 {
     if (s_Styles == null)
     {
         s_Styles = new Styles();
     }
     float num = 24f;
     float num2 = 16f;
     float num3 = 26f;
     float num4 = ((position.height - (2f * num2)) - num3) - num;
     position.height = num;
     this.m_GradientMode = (GradientMode) EditorGUI.EnumPopup(position, s_Styles.modeText, this.m_GradientMode);
     if (this.m_GradientMode != this.m_Gradient.mode)
     {
         this.AssignBack();
     }
     position.y += num;
     position.height = num2;
     this.ShowSwatchArray(position, this.m_AlphaSwatches, true);
     position.y += num2;
     if (Event.current.type == EventType.Repaint)
     {
         position.height = num4;
         DrawGradientWithBackground(position, GradientPreviewCache.GetGradientPreview(this.m_Gradient));
     }
     position.y += num4;
     position.height = num2;
     this.ShowSwatchArray(position, this.m_RGBSwatches, false);
     if (this.m_SelectedSwatch != null)
     {
         position.y += num2;
         position.height = num3;
         position.y += 10f;
         float num5 = 45f;
         float num6 = 60f;
         float num7 = 20f;
         float num8 = 50f;
         float num9 = ((num6 + num7) + num6) + num5;
         Rect rect = position;
         rect.height = 18f;
         rect.x += 17f;
         rect.width -= num9;
         EditorGUIUtility.labelWidth = num8;
         if (this.m_SelectedSwatch.m_IsAlpha)
         {
             EditorGUIUtility.fieldWidth = 30f;
             EditorGUI.BeginChangeCheck();
             float num10 = ((float) EditorGUI.IntSlider(rect, s_Styles.alphaText, (int) (this.m_SelectedSwatch.m_Value.r * 255f), 0, 0xff)) / 255f;
             if (EditorGUI.EndChangeCheck())
             {
                 num10 = Mathf.Clamp01(num10);
                 this.m_SelectedSwatch.m_Value.r = this.m_SelectedSwatch.m_Value.g = this.m_SelectedSwatch.m_Value.b = num10;
                 this.AssignBack();
                 HandleUtility.Repaint();
             }
         }
         else
         {
             EditorGUI.BeginChangeCheck();
             this.m_SelectedSwatch.m_Value = EditorGUI.ColorField(rect, s_Styles.colorText, this.m_SelectedSwatch.m_Value, true, false);
             if (EditorGUI.EndChangeCheck())
             {
                 this.AssignBack();
                 HandleUtility.Repaint();
             }
         }
         rect.x += rect.width + num7;
         rect.width = num5 + num6;
         EditorGUIUtility.labelWidth = num6;
         string kFloatFieldFormatString = EditorGUI.kFloatFieldFormatString;
         EditorGUI.kFloatFieldFormatString = "f1";
         EditorGUI.BeginChangeCheck();
         float num12 = EditorGUI.FloatField(rect, s_Styles.locationText, this.m_SelectedSwatch.m_Time * 100f) / 100f;
         if (EditorGUI.EndChangeCheck())
         {
             this.m_SelectedSwatch.m_Time = Mathf.Clamp(num12, 0f, 1f);
             this.AssignBack();
         }
         EditorGUI.kFloatFieldFormatString = kFloatFieldFormatString;
         rect.x += rect.width;
         rect.width = 20f;
         GUI.Label(rect, s_Styles.percentText);
     }
 }
Example #24
0
 public override void ParseObject(AssetsReader reader)
 {
     colorKeys = reader.ReadArrayOf((r) => new GradientColorKey(r));
     alphaKeys = reader.ReadArrayOf((r) => new GradientAlphaKey(r));
     mode      = reader.ReadEnum <GradientMode>();
 }
Example #25
0
 public GradientColor(GradientMode mode, Color fromColor, Color toColor)
 {
     this.GradientMode = mode;
     this.FromColor    = fromColor;
     this.ToColor      = toColor;
 }
Example #26
0
 /// <summary>
 /// Paints a rounded rectangle with a gradient.
 /// </summary>
 /// <param name="rect">The rectangle to paint.</param>
 /// <param name="radius">The radius of the corner.</param>
 /// <param name="startColor">The start color of the gradiant.</param>
 /// <param name="endColor">The end color of the gradiant.</param>
 /// <param name="gradientMode">The gradiant mode.</param>
 public void GradientFillRoundRectangle(Rectangle rect, int radius, Color startColor, Color endColor, GradientMode gradientMode)
 {
     GradientFillShape(rect, radius, startColor, endColor, gradientMode, GraphicShape.Rounded, RoundedCorners.All);
 }
 public void FromGradient(Gradient source)
 {
     mode      = source.mode;
     colorKeys = source.colorKeys.Select(key => new ColorKey(key)).ToArray();
     alphaKeys = source.alphaKeys.Select(key => new AlphaKey(key)).ToArray();
 }
Example #28
0
 public static int GradientModeToHoudiniColorRampInterpolation(GradientMode gradientMode)
 {
     return((gradientMode == GradientMode.Blend) ? 1 : 0);
 }
Example #29
0
        public void OnGUI(Rect position)
        {
            if (GradientEditor.s_Styles == null)
            {
                GradientEditor.s_Styles = new GradientEditor.Styles();
            }
            float num  = 24f;
            float num2 = 16f;
            float num3 = 26f;
            float num4 = position.height - 2f * num2 - num3 - num;

            position.height     = num;
            this.m_GradientMode = (GradientMode)EditorGUI.EnumPopup(position, GradientEditor.s_Styles.modeText, this.m_GradientMode);
            if (this.m_GradientMode != this.m_Gradient.mode)
            {
                this.AssignBack();
            }
            position.y     += num;
            position.height = num2;
            this.ShowSwatchArray(position, this.m_AlphaSwatches, true);
            position.y += num2;
            if (Event.current.type == EventType.Repaint)
            {
                position.height = num4;
                GradientEditor.DrawGradientWithBackground(position, this.m_Gradient);
            }
            position.y     += num4;
            position.height = num2;
            this.ShowSwatchArray(position, this.m_RGBSwatches, false);
            if (this.m_SelectedSwatch != null)
            {
                position.y     += num2;
                position.height = num3;
                position.y     += 10f;
                float num5       = 45f;
                float num6       = 60f;
                float num7       = 20f;
                float labelWidth = 50f;
                float num8       = num6 + num7 + num6 + num5;
                Rect  position2  = position;
                position2.height            = 18f;
                position2.x                += 17f;
                position2.width            -= num8;
                EditorGUIUtility.labelWidth = labelWidth;
                if (this.m_SelectedSwatch.m_IsAlpha)
                {
                    EditorGUIUtility.fieldWidth = 30f;
                    EditorGUI.BeginChangeCheck();
                    float num9 = (float)EditorGUI.IntSlider(position2, GradientEditor.s_Styles.alphaText, (int)(this.m_SelectedSwatch.m_Value.r * 255f), 0, 255) / 255f;
                    if (EditorGUI.EndChangeCheck())
                    {
                        num9 = Mathf.Clamp01(num9);
                        this.m_SelectedSwatch.m_Value.r = (this.m_SelectedSwatch.m_Value.g = (this.m_SelectedSwatch.m_Value.b = num9));
                        this.AssignBack();
                        HandleUtility.Repaint();
                    }
                }
                else
                {
                    EditorGUI.BeginChangeCheck();
                    this.m_SelectedSwatch.m_Value = EditorGUI.ColorField(position2, GradientEditor.s_Styles.colorText, this.m_SelectedSwatch.m_Value, true, false, this.m_HDR, ColorPicker.defaultHDRConfig);
                    if (EditorGUI.EndChangeCheck())
                    {
                        this.AssignBack();
                        HandleUtility.Repaint();
                    }
                }
                position2.x                += position2.width + num7;
                position2.width             = num5 + num6;
                EditorGUIUtility.labelWidth = num6;
                string kFloatFieldFormatString = EditorGUI.kFloatFieldFormatString;
                EditorGUI.kFloatFieldFormatString = "f1";
                EditorGUI.BeginChangeCheck();
                float value = EditorGUI.FloatField(position2, GradientEditor.s_Styles.locationText, this.m_SelectedSwatch.m_Time * 100f) / 100f;
                if (EditorGUI.EndChangeCheck())
                {
                    this.m_SelectedSwatch.m_Time = Mathf.Clamp(value, 0f, 1f);
                    this.AssignBack();
                }
                EditorGUI.kFloatFieldFormatString = kFloatFieldFormatString;
                position2.x    += position2.width;
                position2.width = 20f;
                GUI.Label(position2, GradientEditor.s_Styles.percentText);
            }
        }
        /// <summary>
        /// Generates a texture that encodes the Gradient specified by colors, colorKeyLocations, alpha and alphaKeyLocations.
        /// The generated texture is textureWidth x 1 pixels in size.
        /// Supports more than 8 each of Alpha Keys and Color Keys
        /// </summary>
        /// <param name="colors"></param>
        /// <param name="colorKeyLocations"></param>
        /// <param name="alphas"></param>
        /// <param name="alphaKeyLocations"></param>
        /// <param name="textureWidth"></param>
        /// <returns></returns>
        public static bool GradientToColors(Color[] colors, float[] colorKeyLocations, float[] alphas, float[] alphaKeyLocations, Color[] gradientColors, GradientMode mode)
        {
            Assert.AreEqual(colors.Length, colorKeyLocations.Length);
            Assert.AreEqual(alphas.Length, alphaKeyLocations.Length);
            // Invalid array lengths
            if (colors.Length != colorKeyLocations.Length || alphas.Length != alphaKeyLocations.Length)
            {
                return(false);
            }
            // Too few keys
            if (colors.Length < 2 && alphas.Length < 2)
            {
                return(false);
            }

            float prevColorKey = 0f, nextColorKey = colorKeyLocations[0];
            Color prevColor = colors[0], nextColor = colors[0];
            float prevAlphaKey = 0f, nextAlphaKey = alphaKeyLocations[0];
            float prevAlpha = alphas[0], nextAlpha = alphas[0];
            var   textureWidth    = gradientColors.Length;
            var   invTextureWidth = 1f / (textureWidth - 1);

            for (int i = 0; i < textureWidth; i++)
            {
                float f = i * invTextureWidth;
                if (f >= nextAlphaKey)
                {
                    if (nextAlphaKey < 1f)
                    {
                        prevAlphaKey = nextAlphaKey;
                        prevAlpha    = nextAlpha;
                        int k = 0;
                        while (k < alphaKeyLocations.Length && f >= alphaKeyLocations[k])
                        {
                            k++;
                            nextAlphaKey = k < alphaKeyLocations.Length ? alphaKeyLocations[k] : 1f;
                            nextAlpha    = alphas[k < alphas.Length ? k : k - 1];
                        }
                    }
                }
                if (f >= nextColorKey)
                {
                    if (nextColorKey < 1f)
                    {
                        prevColorKey = nextColorKey;
                        prevColor    = nextColor;
                        int k = 0;
                        while (k < colorKeyLocations.Length && f >= colorKeyLocations[k])
                        {
                            k++;
                            nextColorKey = k < colorKeyLocations.Length ? colorKeyLocations[k] : 1f;
                            nextColor    = colors[k < colors.Length ? k : k - 1];
                        }
                    }
                }
                var   colorFraction = (f - prevColorKey) / (nextColorKey - prevColorKey);
                var   alphaFraction = (f - prevAlphaKey) / (nextAlphaKey - prevAlphaKey);
                var   lerpedAlpha   = Mathf.Lerp(prevAlpha, nextAlpha, alphaFraction);
                Color lerpedColor;
                switch (mode)
                {
                case GradientMode.LChuv:
                    lerpedColor = ColorLChuv.Lerp(prevColor, nextColor, colorFraction);
                    break;

                default:
                    lerpedColor = Color.Lerp(prevColor, nextColor, colorFraction);
                    break;
                }
                gradientColors[i] = new Color(lerpedColor.r, lerpedColor.g, lerpedColor.b, lerpedAlpha);
            }
            return(true);
        }
Example #31
0
 public SerializableGradientMode(GradientMode gradientMode)
 {
     mode = gradientMode.ToString();
 }
Example #32
0
        public void GradientFillShape(Rectangle rect, int radius, Color startColor, Color endColor, GradientMode gradientMode, GraphicShape shape, RoundedCorners corners)
        {
            if (rect.Width < 1 || rect.Height < 1) return;
            PathFunc func = GetShapeFunc(shape);
            SmoothingMode mode = SmoothingMode;
            try
            {
                SmoothingMode = SmoothingMode.None;
                int x = rect.X, y = rect.Y, w = rect.Width, h = rect.Height;
                int reflectionHeight = h / 2;

                PointF p1 = new PointF(x, y);
                PointF p2 = new PointF(x, y + reflectionHeight);

                if (gradientMode == GradientMode.Top)
                {
                    PathMode pathMode = endColor.A == 255 ? PathMode.Bottom : PathMode.All;
                    using (GraphicsPath path = func(rect, radius, reflectionHeight, pathMode, corners))
                    {
                        using (SolidBrushPlus brush = new SolidBrushPlus(endColor))
                        {
                            this.FillPath(brush, path);
                        }
                    }
                    using (GraphicsPath path = func(rect, radius, reflectionHeight, PathMode.Top, corners))
                    {
                        using (LinearGradientBrush brush = new LinearGradientBrush(p1, p2, startColor, endColor))
                        {
                            this.FillPath(brush, path);
                        }
                    }
                }
                else
                {
                    PathMode pathMode = endColor.A == 255 ? PathMode.Top : PathMode.All;
                    using (GraphicsPath path = func(rect, radius, reflectionHeight, PathMode.All, corners))
                    {
                        using (SolidBrushPlus brush = new SolidBrushPlus(endColor))
                        {
                            this.FillPath(brush, path);
                        }
                    }
                    using (GraphicsPath path = func(rect, radius, reflectionHeight, PathMode.Bottom, corners))
                    {
                        p1.Y = y + reflectionHeight - 1;
                        p2.Y = y + h;
                        using (LinearGradientBrush brush = new LinearGradientBrush(p1, p2, endColor, startColor))
                        {
                            this.FillPath(brush, path);
                        }
                    }
                }
            }
            finally
            {
                SmoothingMode = mode;
            }
        }
Example #33
0
        public void OnGUI(Rect position)
        {
            if (s_Styles == null)
            {
                s_Styles = new Styles();
            }

            float modeHeight            = 24f;
            float swatchHeight          = 16f;
            float editSectionHeight     = 26f;
            float gradientTextureHeight = position.height - 2 * swatchHeight - editSectionHeight - modeHeight;

            position.height = modeHeight;
            m_GradientMode  = (GradientMode)EditorGUI.IntPopup(position, s_Styles.modeText, (int)m_GradientMode, s_Styles.modeTexts, s_Styles.modeValues);
            if (m_GradientMode != m_Gradient.mode)
            {
                AssignBack();
            }

            position.y     += modeHeight;
            position.height = swatchHeight;

            // Alpha swatches (no idea why they're top, but that's what Adobe & Apple seem to agree on)
            ShowSwatchArray(position, m_AlphaSwatches, true);

            // Gradient texture
            position.y += swatchHeight;
            if (Event.current.type == EventType.Repaint)
            {
                position.height = gradientTextureHeight;
                DrawGradientWithBackground(position, m_Gradient, m_ColorSpace == ColorSpace.Linear);
            }
            position.y     += gradientTextureHeight;
            position.height = swatchHeight;

            // Color swatches (bottom)
            ShowSwatchArray(position, m_RGBSwatches, false);

            if (m_SelectedSwatch != null)
            {
                position.y     += swatchHeight;
                position.height = editSectionHeight;
                position.y     += 10;

                float locationWidth         = 45;
                float locationTextWidth     = 60;
                float space                 = 20;
                float alphaOrColorTextWidth = 50;
                float totalLocationWidth    = locationTextWidth + space + locationTextWidth + locationWidth;

                // Alpha or Color field
                Rect rect = position;
                rect.height = 18;
                rect.x     += 17;
                rect.width -= totalLocationWidth;
                EditorGUIUtility.labelWidth = alphaOrColorTextWidth;
                if (m_SelectedSwatch.m_IsAlpha)
                {
                    EditorGUIUtility.fieldWidth = 30;
                    EditorGUI.BeginChangeCheck();
                    float sliderValue = EditorGUI.IntSlider(rect, s_Styles.alphaText, (int)(m_SelectedSwatch.m_Value.r * 255), 0, 255) / 255f;
                    if (EditorGUI.EndChangeCheck())
                    {
                        sliderValue = Mathf.Clamp01(sliderValue);
                        m_SelectedSwatch.m_Value.r = m_SelectedSwatch.m_Value.g = m_SelectedSwatch.m_Value.b = sliderValue;
                        AssignBack();
                        HandleUtility.Repaint();
                    }
                }
                else
                {
                    EditorGUI.BeginChangeCheck();
                    m_SelectedSwatch.m_Value = EditorGUI.ColorField(rect, s_Styles.colorText, m_SelectedSwatch.m_Value, true, false, m_HDR);
                    if (EditorGUI.EndChangeCheck())
                    {
                        AssignBack();
                        HandleUtility.Repaint();
                    }
                }

                // Location of key
                rect.x    += rect.width + space;
                rect.width = locationWidth + locationTextWidth;

                EditorGUIUtility.labelWidth = locationTextWidth;
                string orgFormatString = EditorGUI.kFloatFieldFormatString;
                EditorGUI.kFloatFieldFormatString = "f1";

                EditorGUI.BeginChangeCheck();
                float newLocation = EditorGUI.FloatField(rect, s_Styles.locationText, m_SelectedSwatch.m_Time * 100.0f) / 100.0f;
                if (EditorGUI.EndChangeCheck())
                {
                    m_SelectedSwatch.m_Time = Mathf.Clamp(newLocation, 0f, 1f);
                    AssignBack();
                }

                EditorGUI.kFloatFieldFormatString = orgFormatString;

                rect.x    += rect.width;
                rect.width = 20;
                GUI.Label(rect, s_Styles.percentText);
            }
        }
        public void OnGUI(Rect position)
        {
            if (s_Styles == null)
            {
                s_Styles = new Styles();
            }
            float num  = 24f;
            float num2 = 16f;
            float num3 = 26f;
            float num4 = ((position.height - (2f * num2)) - num3) - num;

            position.height     = num;
            this.m_GradientMode = (GradientMode)EditorGUI.EnumPopup(position, s_Styles.modeText, this.m_GradientMode);
            if (this.m_GradientMode != this.m_Gradient.mode)
            {
                this.AssignBack();
            }
            position.y     += num;
            position.height = num2;
            this.ShowSwatchArray(position, this.m_AlphaSwatches, true);
            position.y += num2;
            if (Event.current.type == EventType.Repaint)
            {
                position.height = num4;
                DrawGradientWithBackground(position, GradientPreviewCache.GetGradientPreview(this.m_Gradient));
            }
            position.y     += num4;
            position.height = num2;
            this.ShowSwatchArray(position, this.m_RGBSwatches, false);
            if (this.m_SelectedSwatch != null)
            {
                position.y     += num2;
                position.height = num3;
                position.y     += 10f;
                float num5 = 45f;
                float num6 = 60f;
                float num7 = 20f;
                float num8 = 50f;
                float num9 = ((num6 + num7) + num6) + num5;
                Rect  rect = position;
                rect.height = 18f;
                rect.x     += 17f;
                rect.width -= num9;
                EditorGUIUtility.labelWidth = num8;
                if (this.m_SelectedSwatch.m_IsAlpha)
                {
                    EditorGUIUtility.fieldWidth = 30f;
                    EditorGUI.BeginChangeCheck();
                    float num10 = ((float)EditorGUI.IntSlider(rect, s_Styles.alphaText, (int)(this.m_SelectedSwatch.m_Value.r * 255f), 0, 0xff)) / 255f;
                    if (EditorGUI.EndChangeCheck())
                    {
                        num10 = Mathf.Clamp01(num10);
                        this.m_SelectedSwatch.m_Value.r = this.m_SelectedSwatch.m_Value.g = this.m_SelectedSwatch.m_Value.b = num10;
                        this.AssignBack();
                        HandleUtility.Repaint();
                    }
                }
                else
                {
                    EditorGUI.BeginChangeCheck();
                    this.m_SelectedSwatch.m_Value = EditorGUI.ColorField(rect, s_Styles.colorText, this.m_SelectedSwatch.m_Value, true, false);
                    if (EditorGUI.EndChangeCheck())
                    {
                        this.AssignBack();
                        HandleUtility.Repaint();
                    }
                }
                rect.x    += rect.width + num7;
                rect.width = num5 + num6;
                EditorGUIUtility.labelWidth = num6;
                string kFloatFieldFormatString = EditorGUI.kFloatFieldFormatString;
                EditorGUI.kFloatFieldFormatString = "f1";
                EditorGUI.BeginChangeCheck();
                float num12 = EditorGUI.FloatField(rect, s_Styles.locationText, this.m_SelectedSwatch.m_Time * 100f) / 100f;
                if (EditorGUI.EndChangeCheck())
                {
                    this.m_SelectedSwatch.m_Time = Mathf.Clamp(num12, 0f, 1f);
                    this.AssignBack();
                }
                EditorGUI.kFloatFieldFormatString = kFloatFieldFormatString;
                rect.x    += rect.width;
                rect.width = 20f;
                GUI.Label(rect, s_Styles.percentText);
            }
        }