GetColorStyle() public static method

public static GetColorStyle ( string name ) : ColorScheme
name string
return ColorScheme
Example #1
0
        public static ColorScheme Import(string fileName, Stream stream)
        {
            var result = new ColorScheme();

            result.Name        = Path.GetFileNameWithoutExtension(fileName);
            result.Description = "Imported color scheme";
            result.Originator  = "Imported from " + fileName;

            var colors = new Dictionary <string, VSSettingColor> ();

            using (var reader = XmlReader.Create(stream)) {
                while (reader.Read())
                {
                    if (reader.LocalName == "Item")
                    {
                        var color = VSSettingColor.Create(reader);
                        if (colors.ContainsKey(color.Name))
                        {
                            Console.WriteLine("Warning: {0} is defined twice in vssettings.", color.Name);
                            continue;
                        }
                        colors[color.Name] = color;
                    }
                }
            }


            HashSet <string> importedAmbientColors = new HashSet <string> ();

            // convert ambient colors
            foreach (var ambient in ambientColors.Values)
            {
                if (!string.IsNullOrEmpty(ambient.Attribute.VSSetting))
                {
                    var import = AmbientColor.Import(colors, ambient.Attribute.VSSetting);
                    if (import != null)
                    {
                        importedAmbientColors.Add(import.Name);
                        ambient.Info.SetValue(result, import, null);
                        continue;
                    }
                }
            }

            // convert text colors
            foreach (var vsc in colors.Values)
            {
                bool found = false;
                foreach (var color in textColors)
                {
                    if (color.Value.Attribute.VSSetting == null)
                    {
                        continue;
                    }
                    var split = color.Value.Attribute.VSSetting.Split('?');
                    foreach (var s in split)
                    {
                        if (s == vsc.Name)
                        {
                            /*	if (vsc.Foreground == "0x02000000" && vsc.Background == "0x02000000") {
                             *              color.Value.Info.SetValue (result, result.PlainText, null);
                             *              found = true;
                             *              continue;
                             *      }*/
                            var textColor = ChunkStyle.Import(color.Value.Attribute.Name, vsc);
                            if (textColor != null)
                            {
                                color.Value.Info.SetValue(result, textColor, null);
                                found = true;
                            }
                        }
                    }
                }
                if (!found && !importedAmbientColors.Contains(vsc.Name))
                {
                    Console.WriteLine(vsc.Name + " not imported!");
                }
            }

            result.IndentationGuide = new AmbientColor();
            result.IndentationGuide.Colors.Add(Tuple.Create("color", AlphaBlend(result.PlainText.Foreground, result.PlainText.Background, 0.3)));

            result.TooltipText = result.PlainText.Clone();
            var h = (HslColor)result.TooltipText.Background;

            h.L += 0.01;
            result.TooltipText.Background = h;

            result.TooltipPagerTop = new AmbientColor();
            result.TooltipPagerTop.Colors.Add(Tuple.Create("color", result.TooltipText.Background));

            result.TooltipPagerBottom = new AmbientColor();
            result.TooltipPagerBottom.Colors.Add(Tuple.Create("color", result.TooltipText.Background));

            result.TooltipPagerTriangle = new AmbientColor();
            result.TooltipPagerTriangle.Colors.Add(Tuple.Create("color", AlphaBlend(result.PlainText.Foreground, result.PlainText.Background, 0.8)));

            result.TooltipBorder = new AmbientColor();
            result.TooltipBorder.Colors.Add(Tuple.Create("color", AlphaBlend(result.PlainText.Foreground, result.PlainText.Background, 0.5)));

            var defaultStyle = SyntaxModeService.GetColorStyle(HslColor.Brightness(result.PlainText.Background) < 0.5 ? "Monokai" : "Default");

            foreach (var color in textColors.Values)
            {
                if (color.Info.GetValue(result, null) == null)
                {
                    color.Info.SetValue(result, color.Info.GetValue(defaultStyle, null), null);
                }
            }
            foreach (var color in ambientColors.Values)
            {
                if (color.Info.GetValue(result, null) == null)
                {
                    color.Info.SetValue(result, color.Info.GetValue(defaultStyle, null), null);
                }
            }
            if (result.PlainText.TransparentForeground)
            {
                result.PlainText.Foreground = new Cairo.Color(0, 0, 0);
            }
            return(result);
        }
Example #2
0
        public static ColorScheme LoadFrom(Stream stream)
        {
            var result = new ColorScheme();
            var reader = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(stream, new System.Xml.XmlDictionaryReaderQuotas());

            var root = XElement.Load(reader);

            // The fields we'd like to extract
            result.Name = root.XPathSelectElement("name").Value;

            if (result.Name != "Default")
            {
                result.CopyValues(SyntaxModeService.DefaultColorStyle);
            }

            var version = Version.Parse(root.XPathSelectElement("version").Value);

            if (version.Major != 1)
            {
                return(null);
            }
            var el = root.XPathSelectElement("description");

            if (el != null)
            {
                result.Description = el.Value;
            }
            el = root.XPathSelectElement("originator");
            if (el != null)
            {
                result.Originator = el.Value;
            }
            el = root.XPathSelectElement("baseScheme");
            if (el != null)
            {
                result.BaseScheme = el.Value;
            }

            if (result.BaseScheme != null)
            {
                var baseScheme = SyntaxModeService.GetColorStyle(result.BaseScheme);
                if (baseScheme != null)
                {
                    result.CopyValues(baseScheme);
                }
            }

            var palette = new Dictionary <string, Cairo.Color> ();

            foreach (var color in root.XPathSelectElements("palette/*"))
            {
                var name = color.XPathSelectElement("name").Value;
                if (palette.ContainsKey(name))
                {
                    throw new InvalidDataException("Duplicate palette color definition for: " + name);
                }
                palette.Add(
                    name,
                    ParseColor(color.XPathSelectElement("value").Value)
                    );
            }

            foreach (var colorElement in root.XPathSelectElements("//colors/*"))
            {
                var color = AmbientColor.Create(colorElement, palette);
                PropertyDecsription info;
                if (!ambientColors.TryGetValue(color.Name, out info))
                {
                    Console.WriteLine("Ambient color:" + color.Name + " not found.");
                    continue;
                }
                info.Info.SetValue(result, color, null);
            }

            foreach (var textColorElement in root.XPathSelectElements("//text/*"))
            {
                var color = ChunkStyle.Create(textColorElement, palette);
                PropertyDecsription info;
                if (!textColors.TryGetValue(color.Name, out info))
                {
                    Console.WriteLine("Text color:" + color.Name + " not found.");
                    continue;
                }
                info.Info.SetValue(result, color, null);
            }

            // Check scheme
            bool valid = true;

            foreach (var color in textColors.Values)
            {
                if (color.Info.GetValue(result, null) == null)
                {
                    Console.WriteLine(color.Attribute.Name + " == null");
                    valid = false;
                }
            }
            foreach (var color in ambientColors.Values)
            {
                if (color.Info.GetValue(result, null) == null)
                {
                    Console.WriteLine(color.Attribute.Name + " == null");
                    valid = false;
                }
            }
            if (!valid)
            {
                throw new InvalidDataException("Scheme " + result.Name + " is not valid.");
            }
            return(result);
        }
Example #3
0
        public void Save(string fileName)
        {
            using (var writer = new StreamWriter(fileName)) {
                writer.WriteLine("{");
                writer.WriteLine("\t\"name\":\"{0}\",", Name);
                writer.WriteLine("\t\"version\":\"1.0\",");
                if (!string.IsNullOrEmpty(Description))
                {
                    writer.WriteLine("\t\"description\":\"{0}\",", Description);
                }
                if (!string.IsNullOrEmpty(Originator))
                {
                    writer.WriteLine("\t\"originator\":\"{0}\",", Originator);
                }
                if (!string.IsNullOrEmpty(BaseScheme))
                {
                    writer.WriteLine("\t\"baseScheme\":\"{0}\",", BaseScheme);
                }

                var baseStyle = SyntaxModeService.GetColorStyle(BaseScheme ?? "Default");

                writer.WriteLine("\t\"colors\":[");
                bool first = true;
                foreach (var ambient in ambientColors)
                {
                    var thisValue = ambient.Value.Info.GetValue(this, null) as AmbientColor;
                    if (thisValue == null)
                    {
                        continue;
                    }
                    var baseValue = ambient.Value.Info.GetValue(baseStyle, null) as AmbientColor;
                    if (thisValue.Equals(baseValue))
                    {
                        continue;
                    }

                    var colorString = new StringBuilder();
                    foreach (var color in thisValue.Colors)
                    {
                        if (colorString.Length > 0)
                        {
                            colorString.Append(", ");
                        }
                        colorString.Append(string.Format("\"{0}\":\"{1}\"", color.Item1, ColorToMarkup(color.Item2)));
                    }
                    if (colorString.Length == 0)
                    {
                        Console.WriteLine("Invalid ambient color :" + thisValue);
                        continue;
                    }
                    if (!first)
                    {
                        writer.WriteLine(",");
                    }
                    else
                    {
                        first = false;
                    }
                    writer.Write("\t\t{");
                    writer.Write("\"name\": \"{0}\", {1}", ambient.Value.Attribute.Name, colorString);
                    writer.Write(" }");
                }

                writer.WriteLine("\t],");
                first = true;
                writer.WriteLine("\t\"text\":[");
                foreach (var textColor in textColors)
                {
                    var thisValue = textColor.Value.Info.GetValue(this, null) as ChunkStyle;
                    if (thisValue == null)
                    {
                        continue;
                    }
                    var baseValue = textColor.Value.Info.GetValue(baseStyle, null) as ChunkStyle;
                    if (thisValue.Equals(baseValue))
                    {
                        continue;
                    }
                    var colorString = new StringBuilder();
                    if (!thisValue.TransparentForeground)
                    {
                        colorString.Append(string.Format("\"fore\":\"{0}\"", ColorToMarkup(thisValue.Foreground)));
                    }
                    if (!thisValue.TransparentBackground)
                    {
                        if (colorString.Length > 0)
                        {
                            colorString.Append(", ");
                        }
                        colorString.Append(string.Format("\"back\":\"{0}\"", ColorToMarkup(thisValue.Background)));
                    }
                    if (thisValue.FontWeight != FontWeight.Normal)
                    {
                        if (colorString.Length > 0)
                        {
                            colorString.Append(", ");
                        }
                        colorString.Append(string.Format("\"weight\":\"{0}\"", thisValue.FontWeight));
                    }
                    if (thisValue.FontStyle != FontStyle.Normal)
                    {
                        if (colorString.Length > 0)
                        {
                            colorString.Append(", ");
                        }
                        colorString.Append(string.Format("\"style\":\"{0}\"", thisValue.FontStyle));
                    }
                    if (!first)
                    {
                        writer.WriteLine(",");
                    }
                    else
                    {
                        first = false;
                    }
                    writer.Write("\t\t{");
                    if (colorString.Length == 0)
                    {
                        writer.Write("\"name\": \"{0}\"", textColor.Value.Attribute.Name);
                    }
                    else
                    {
                        writer.Write("\"name\": \"{0}\", {1}", textColor.Value.Attribute.Name, colorString);
                    }
                    writer.Write(" }");
                }
                writer.WriteLine();
                writer.WriteLine("\t]");

                writer.WriteLine("}");
            }
        }