private void AddDefaultPresets()
        {
            Gradient rainbow = new Gradient();

            rainbow.Add(0, ColorBgra.Red);
            rainbow.Add(1 / 6.0, ColorBgra.Orange);
            rainbow.Add(1 / 3.0, ColorBgra.Yellow);
            rainbow.Add(.5, ColorBgra.Lime);
            rainbow.Add(2 / 3.0, ColorBgra.Blue);
            rainbow.Add(5 / 6.0, ColorBgra.Indigo);
            rainbow.Add(1, ColorBgra.Violet);
            presetDropdown.AddPreset(new ConfigToken()
            {
                Gradient = rainbow
            }, "Rainbow");

            Gradient highcontrast = new Gradient();

            highcontrast.Add(0.6, ColorBgra.Black);
            highcontrast.Add(0.75, ColorBgra.White);
            presetDropdown.AddPreset(new ConfigToken()
            {
                Gradient = highcontrast
            }, "High Contrast");

            Gradient hot = new Gradient();

            hot.Add(0.2, ColorBgra.Black);
            hot.Add(0.75, ColorBgra.Red);
            hot.Add(0.95, ColorBgra.Yellow);
            hot.Add(1, ColorBgra.White);
            presetDropdown.AddPreset(new ConfigToken()
            {
                Gradient = hot
            }, "Hot");
        }
Beispiel #2
0
        public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value is string)
            {
                var      gradient = new Gradient();
                string[] entries  = ((string)value).Split(new[] { ListSeparator }, StringSplitOptions.None);
                foreach (var entryText in entries)
                {
                    var p = entryText.Split(new[] { EntrySeparator });

                    if (p.Length == 2)
                    {
                        var position = Convert.ToSingle(p[0], culture);
                        var color    = (Color)TypeDescriptor.GetConverter(typeof(Color)).ConvertFromString(context, culture, p[1]);
                        var entry    = new GradientEntry(position, color);
                        gradient.Add(entry);
                    }
                }

                return(gradient);
            }

            return(base.ConvertFrom(context, culture, value));
        }
Beispiel #3
0
        public bool SetCss(string css)
        {
            if (css == null)
            {
                return(true);
            }

            // Get rid of comments
            css = _sCssComment.Replace(css, match => new string(match.Value
                                                                .Select(x => char.IsWhiteSpace(x) ? x : ' ')
                                                                .ToArray()));

            var newColor          = color;
            var newEnableGradient = enableGradient;
            var newGradient       = new Gradient();

            if (gradient != null)
            {
                foreach (var keyPoint in gradient)
                {
                    newGradient.Add(keyPoint);
                }
            }

            var newGradientAngle = gradientAngle;

            var newBorderMode  = borderMode;
            var newBorderColor = borderColor;
            var newBorderWidth = borderWidth;

            var newCornerRadius = cornerRadius;

            var valid      = true;
            var hasOpacity = false;

            foreach (Match match in _sCssStatement.Matches(css))
            {
                var command = match.Groups["command"].Value;
                var value   = match.Groups["value"].Value;

                switch (command)
                {
                case "background":
                {
                    var colorMatch = _sCssColor.Match(value);
                    if (colorMatch.Success && colorMatch.Index == 0)
                    {
                        newColor          = ParseColor(colorMatch, hasOpacity ? newColor.a : 1f);
                        newEnableGradient = false;
                        break;
                    }

                    var gradientMatch = _sCssLinearGradient.Match(value);
                    if (gradientMatch.Success && gradientMatch.Index == 0)
                    {
                        newColor          = Color.white;
                        newGradientAngle  = float.Parse(gradientMatch.Groups["angle"].Value);
                        newEnableGradient = true;

                        newGradient.Clear();

                        foreach (Capture capture in gradientMatch.Groups["point"].Captures)
                        {
                            var pointMatch = _sCssGradientPoint.Match(capture.Value);

                            newGradient.Add(new GradientKeyPoint
                                {
                                    color    = ParseColor(pointMatch.Groups["color"].Value),
                                    progress = pointMatch.Groups["percent"].Success
                                        ? float.Parse(pointMatch.Groups["percent"].Value) / 100f : 0f
                                });
                        }

                        break;
                    }

                    valid = false;
                    break;
                }

                case "opacity":
                {
                    float opacity;
                    if (float.TryParse(value, out opacity))
                    {
                        newColor.a = opacity;
                        hasOpacity = true;
                        break;
                    }

                    valid = false;
                    break;
                }

                case "border":
                {
                    var borderMatch = _sCssBorder.Match(value);
                    if (borderMatch.Success && borderMatch.Index == 0)
                    {
                        newBorderMode  = BorderMode.All;
                        newBorderColor = ParseColor(borderMatch.Groups["color"].Value);
                        newBorderWidth = ParsePixels(borderMatch.Groups["width"].Value);

                        valid = true;
                        break;
                    }

                    valid = false;
                    break;
                }

                case "border-radius":
                {
                    var pixelsMatch = _sCssPixels.Match(value);
                    if (pixelsMatch.Success && pixelsMatch.Index == 0)
                    {
                        newCornerRadius = ParsePixels(pixelsMatch.Value);
                        valid           = true;
                        break;
                    }

                    valid = false;
                    break;
                }
                }
            }

            if (valid)
            {
                color          = newColor;
                enableGradient = newEnableGradient;
                gradientAngle  = newGradientAngle;
                borderMode     = newBorderMode;
                borderColor    = newBorderColor;
                borderWidth    = newBorderWidth;
                cornerRadius   = newCornerRadius;
                gradient       = newGradient;
            }

            return(valid);
        }
Beispiel #4
0
        public void CopyFromLerped(RectStyle a, RectStyle b, float t)
        {
            color = Color.Lerp(a.color, b.color, t);

            if ((a.gradient?.keyPointCount ?? 0) != (b.gradient?.keyPointCount ?? 0))
            {
                // TODO
            }
            else if (a.gradient != null && b.gradient != null && a.gradient.keyPointCount > 0)
            {
                gradientAngle = Mathf.LerpAngle(a.gradientAngle, b.gradientAngle, t);

                if (gradient == null)
                {
                    gradient = new Gradient();
                }
                else
                {
                    gradient.Clear();
                }

                for (var i = 0; i < a.gradient.keyPointCount; ++i)
                {
                    gradient.Add(new GradientKeyPoint
                    {
                        color    = Color.Lerp(a.gradient[i].color, b.gradient[i].color, t),
                        progress = Mathf.Lerp(a.gradient[i].progress, b.gradient[i].progress, t)
                    });
                }
            }

            enableGradient = a.enableGradient || b.enableGradient;
            borderMode     = a.borderMode | b.borderMode;
            borderColor    = a.HasBorder != b.HasBorder
                ? a.HasBorder
                    ? a.borderColor
                    : b.borderColor
                : Color.Lerp(a.borderColor, b.borderColor, t);
            borderWidth = Mathf.Lerp(a.HasBorder ? a.borderWidth : 0f, b.HasBorder ? b.borderWidth : 0f, t);

            cornerRadius = Mathf.Lerp(a.cornerRadius, b.cornerRadius, t);

            var boxShadowColorA = a.enableBoxShadow ? a.boxShadowColor : b.boxShadowColor;
            var boxShadowColorB = b.enableBoxShadow ? b.boxShadowColor : a.boxShadowColor;

            if (!a.enableBoxShadow)
            {
                boxShadowColorA.a = 0f;
            }
            if (!b.enableBoxShadow)
            {
                boxShadowColorB.a = 0f;
            }

            enableBoxShadow     = a.enableBoxShadow || b.enableBoxShadow;
            boxShadowColor      = Color.Lerp(boxShadowColorA, boxShadowColorB, t);
            boxShadowBlurRadius = Mathf.Lerp(
                a.enableBoxShadow ? a.boxShadowBlurRadius : b.boxShadowBlurRadius,
                b.enableBoxShadow ? b.boxShadowBlurRadius : a.boxShadowBlurRadius, t);
            boxShadowOffset = Vector2.Lerp(
                a.enableBoxShadow ? a.boxShadowOffset : b.boxShadowOffset,
                b.enableBoxShadow ? b.boxShadowOffset : a.boxShadowOffset, t);
            boxShadowInner = Mathf.Lerp(
                a.enableBoxShadow ? a.boxShadowInner : b.boxShadowInner,
                b.enableBoxShadow ? b.boxShadowInner : a.boxShadowInner, t);
        }