Beispiel #1
0
        public void BackgroundImageLinearGradientWithSide()
        {
            var source   = "background-image: linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet)";
            var property = ParseDeclaration(source);

            Assert.IsTrue(property.HasValue);
            Assert.IsFalse(property.IsInitial);
            var value = property.RawValue as CssListValue;

            Assert.IsNotNull(value);
            Assert.AreEqual(1, value.Items.Length);
            var gradient = value.Items[0] as CssLinearGradientValue;

            Assert.IsFalse(gradient.IsRepeating);
            Assert.AreEqual(Angle.Quarter, gradient.Angle);
            var stops = gradient.Stops.ToArray();

            Assert.AreEqual(7, stops.Length);
            Assert.AreEqual(CssColors.GetColor("red").Value, stops[0].Color);
            Assert.AreEqual(CssColors.GetColor("orange").Value, stops[1].Color);
            Assert.AreEqual(CssColors.GetColor("yellow").Value, stops[2].Color);
            Assert.AreEqual(CssColors.GetColor("green").Value, stops[3].Color);
            Assert.AreEqual(CssColors.GetColor("blue").Value, stops[4].Color);
            Assert.AreEqual(CssColors.GetColor("indigo").Value, stops[5].Color);
            Assert.AreEqual(CssColors.GetColor("violet").Value, stops[6].Color);
        }
Beispiel #2
0
        public void BackgroundImageLinearGradientNoAngle()
        {
            var source   = "background-image: linear-gradient(yellow, blue 20%, #0f0)";
            var property = ParseDeclaration(source);

            Assert.IsTrue(property.HasValue);
            Assert.IsFalse(property.IsInitial);
            var value = property.RawValue as CssListValue;

            Assert.IsNotNull(value);
            Assert.AreEqual(1, value.Items.Length);
            var gradient = value.Items[0] as CssLinearGradientValue;

            Assert.IsFalse(gradient.IsRepeating);
            Assert.AreEqual(Angle.Half, gradient.Angle);
            Assert.AreEqual(3, gradient.Stops.Count());
            Assert.AreEqual(CssColors.GetColor("yellow").Value, gradient.Stops.First().Color);
            Assert.AreEqual(CssColors.GetColor("blue").Value, gradient.Stops.Skip(1).First().Color);
            Assert.AreEqual(Color.FromRgb(0, 255, 0), gradient.Stops.Skip(2).First().Color);
        }
Beispiel #3
0
        public static (string hexColor, float opacity) GetHexColorAndFloatOpacity(string colorText, List <string> warnings)
        {
            colorText = colorText.Trim();

            if (colorText.StartsWith("url("))
            {
                throw new ArgumentException("Need to handle URL colors before getting to this point!");
            }

            if (colorText.StartsWith("rgb"))
            {
                colorText = colorText.Trim(')');
                var tokens = colorText.Split(new char[] { '(', ' ', ',' }).ToArray();
                tokens = tokens.Where(t => !string.IsNullOrWhiteSpace(t)).ToArray();
                if (tokens.Length < 4)
                {
                    warnings.AddWarning("Assuming color is black because could not parse color text [" + colorText + "]");
                    return("#000", float.NaN);
                }
                string hexColor = "#";
                if (tokens.Length > 3)
                {
                    var rToken = tokens[1].Trim();
                    if (ParseColorNumberToken(rToken) is string rHex)
                    {
                        hexColor += rHex;
                    }
                    else
                    {
                        warnings.AddWarning("Assuming color is black because could not parse red token [" + rToken + "] in color text [" + colorText + "]");
                        return("#000", float.NaN);
                    }

                    var gToken = tokens[2].Trim();
                    if (ParseColorNumberToken(gToken) is string gHex)
                    {
                        hexColor += gHex;
                    }
                    else
                    {
                        warnings.AddWarning("Assuming color is black because could not parse green token [" + gToken + "] in color text [" + colorText + "]");
                        return("#000", float.NaN);
                    }

                    var bToken = tokens[3].Trim();
                    if (ParseColorNumberToken(bToken) is string bHex)
                    {
                        hexColor += bHex;
                    }
                    else
                    {
                        warnings.AddWarning("Assuming color is black because could not parse blue token [" + bToken + "] in color text [" + colorText + "]");
                        return("#000", float.NaN);
                    }

                    if (tokens.Length > 4)
                    {
                        var aToken = tokens[4].Trim();
                        if (aToken.EndsWith("%"))
                        {
                            if (float.TryParse(aToken, out float alpha))
                            {
                                return(hexColor, alpha);
                            }
                        }
                        else if (float.TryParse(aToken, out float alpha))
                        {
                            return(hexColor, alpha / 255f);
                        }
                        warnings.AddWarning("Ignoring opacity because could not parse alpha token [" + aToken + "] in color text [" + colorText + "]");
                    }
                    return(hexColor, float.NaN);
                }
            }

            if (colorText.StartsWith("#"))
            {
                if (colorText.Length == 4 || colorText.Length == 7)
                {
                    return(colorText, float.NaN);
                }
                if (colorText.Length == 5)
                {
                    var aHex   = colorText.Substring(1, 1);
                    var aInt   = Convert.ToInt16(aHex);
                    var aFloat = aInt / 15;
                    return("#" + colorText.Substring(2), aFloat);
                }
                if (colorText.Length == 9)
                {
                    var aHex   = colorText.Substring(1, 2);
                    var aInt   = Convert.ToInt16(aHex);
                    var aFloat = aInt / 255f;
                    return("#" + colorText.Substring(3), aFloat);
                }
            }
            else if (colorText == "transparent")
            {
                return("#000", 0);
            }
            else if (CssColors.ContainsKey(colorText.ToLower()))// (CssColors.TryGetValue(colorText, out string cssHexColor))
            {
                return(CssColors[colorText.ToLower()], float.NaN);
            }
            else if (colorText == "inherit")
            {
                return("inherit", float.NaN);
            }


            warnings.AddWarning("Could not parse color text [" + colorText + "] so assuming black.");
            return("#000", float.NaN);
        }