Ejemplo n.º 1
0
 private static Dictionary <string, PaintShopSource> GetNameSourcePairs(JToken t, Func <string, byte[]> extraData, PaintShopSourceParams sourceParams)
 {
     return(t?.ToObject <Dictionary <string, JToken> >().Select(x => new {
         x.Key,
         Source = GetSource(x.Value, extraData, sourceParams)
     }).Where(x => x.Source != null).ToDictionary(
                x => x.Key,
                x => x.Source) ?? new Dictionary <string, PaintShopSource>());
 }
Ejemplo n.º 2
0
        private static PaintShopSource GetSource([CanBeNull] JToken j, Func <string, byte[]> extraData, [CanBeNull] PaintShopSourceParams baseParams)
        {
            if (j == null)
            {
                return(null);
            }

            if (j.Type == JTokenType.Object)
            {
                var o = (JObject)j;
                baseParams = GetSourceParams(o);

                if (o["red"] != null)
                {
                    return(new PaintShopSource(
                               GetSource(o["red"], extraData, baseParams),
                               GetSource(o["green"], extraData, baseParams),
                               GetSource(o["blue"], extraData, baseParams),
                               GetSource(o["alpha"], extraData, baseParams)).SetFrom(baseParams));
                }

                return(GetSource(o["path"], extraData, baseParams));
            }

            if (j.Type == JTokenType.Boolean)
            {
                var b = (bool)j;
                return(b ? PaintShopSource.InputSource.SetFrom(baseParams) : null);
            }

            if (j.Type != JTokenType.String)
            {
                return(null);
            }

            var    s = j.ToString();
            string c = null;

            var index = s.IndexOf(':');

            if (index > 1)
            {
                c = s.Substring(index + 1);
                s = s.Substring(0, index);
            }

            if (s == "@self" || s == "#self")
            {
                return(PaintShopSource.InputSource.SetFrom(baseParams).MapChannels(c));
            }

            if (s.StartsWith("#"))
            {
                return(new PaintShopSource(s.ToColor()?.ToColor() ?? System.Drawing.Color.White)
                       .SetFrom(baseParams).MapChannels(c));
            }

            if (s.StartsWith("./") || s.StartsWith(".\\"))
            {
                return(new PaintShopSource(extraData(s.Substring(2)))
                       .SetFrom(baseParams).MapChannels(c));
            }

            return(new PaintShopSource(s)
                   .SetFrom(baseParams).MapChannels(c));
        }