Example #1
0
 /// <summary>
 /// This constructor is here so we can allow partial matches to the property names.
 /// </summary>
 /// <param name="values"></param>
 public Text(IDictionary values) : this()
 {
     foreach (string key in values.Keys)
     {
         var pattern = "^" + Regex.Escape(key);
         if ("bg".Equals(key, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch("BackgroundColor", pattern, RegexOptions.IgnoreCase))
         {
             BackgroundColor = RgbColor.ConvertFrom(values[key]);
         }
         else if ("fg".Equals(key, StringComparison.OrdinalIgnoreCase) || Regex.IsMatch("ForegroundColor", pattern, RegexOptions.IgnoreCase))
         {
             ForegroundColor = RgbColor.ConvertFrom(values[key]);
         }
         else if (Regex.IsMatch("text", pattern, RegexOptions.IgnoreCase) || Regex.IsMatch("Content", pattern, RegexOptions.IgnoreCase) || Regex.IsMatch("Object", pattern, RegexOptions.IgnoreCase))
         {
             Object = values[key];
         }
         else if (Regex.IsMatch("clear", pattern, RegexOptions.IgnoreCase))
         {
             Clear = (bool)values[key];
         }
         else if (Regex.IsMatch("entities", pattern, RegexOptions.IgnoreCase))
         {
             Entities = (bool)values[key];
         }
         else if (Regex.IsMatch("separator", pattern, RegexOptions.IgnoreCase))
         {
             Separator = values[key].ToString();
         }
         else if (Regex.IsMatch("persist", pattern, RegexOptions.IgnoreCase))
         {
             PersistentColor = (bool)values[key];
         }
         else
         {
             throw new ArgumentException("Unknown key '" + key + "' in " + values.GetType().Name + ". Allowed values are BackgroundColor (or bg), ForegroundColor (or fg), and Object (also called Content or Text), or Separator, Clear, and Entities");
         }
     }
 }
Example #2
0
 public void SetColor(RgbColor color)
 {
     ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
 }
Example #3
0
 public ColorReference(RgbColor color)
 {
     ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
 }
Example #4
0
        public static string GetString(RgbColor foreground, RgbColor background, object @object, string separator = " ", bool clear = false, bool entities = true)
        {
            var output = new StringBuilder();

            // There's a bug in Conhost where an advanced 48;2 RGB code followed by a console code doesn't render the RGB value
            // So we try to put the ConsoleColor first, if it's there ...
            if (null != foreground)
            {
                if (foreground.Mode == ColorMode.ConsoleColor)
                {
                    output.Append(foreground.ToVtEscapeSequence(false));
                    if (null != background)
                    {
                        output.Append(background.ToVtEscapeSequence(true));
                    }
                }
                else
                {
                    if (null != background)
                    {
                        output.Append(background.ToVtEscapeSequence(true));
                    }
                    output.Append(foreground.ToVtEscapeSequence(false));
                }
            }
            else if (null != background)
            {
                output.Append(background.ToVtEscapeSequence(true));
            }

            if (null != @object)
            {
                //var scriptBlock = @object as ScriptBlock;
                //var text = (string)LanguagePrimitives.ConvertTo(scriptBlock != null ? scriptBlock.Invoke() : @object, typeof(string));
                var text = ConvertToString(@object, separator);

                if (!string.IsNullOrEmpty(text))
                {
                    output.Append(text);
                }
            }

            if (clear)
            {
                if (null != background)
                {
                    // clear background
                    output.Append("\u001B[49m");
                }
                if (null != foreground)
                {
                    // clear foreground
                    output.Append("\u001B[39m");
                }
            }

            if (entities)
            {
                return(PoshCode.Pansies.Entities.Decode(output.ToString()));
            }
            else
            {
                return(output.ToString());
            }
        }
Example #5
0
 public static RgbColor[][] GetRgbGradient(RgbColor startColor, RgbColor endColor, int height = 1, int width = 120, bool reverseHue = false)
 {
     return((RgbColor[][])Get2DGradient <RgbColor, HslColor>(startColor, endColor, height, width, reverseHue));
 }