Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace ReplaceIcon
- {
- public class ThemeIcon
- {
- public string SizePattern { get; private set; }
- public List<IconSize> Sizes { get; private set; }
- public string this[string size]
- {
- get
- {
- if (Sizes.All(s => s.Size != size))
- throw new Exception("Unsupported size");
- return string.Format(SizePattern, size);
- }
- }
- public string this[IconSize size]
- {
- get
- {
- if (!Sizes.Contains(size))
- throw new Exception("Unsupported size");
- return string.Format(SizePattern, size.Size);
- }
- }
- public ThemeIcon(string path)
- {
- Sizes = GetSizes(path);
- SizePattern = GetSizePattern(path);
- }
- private string GetSizePattern(string path)
- {
- var parts = path.Split('/');
- if (Sizes.Any(s => s.Size == parts[parts.Length - 2]))
- {
- parts[parts.Length - 2] = "{0}"; // .../CaledoniaTheme/apps/{0}/firefox.png
- }
- else if (Sizes.Any(s => s.Size == parts[parts.Length - 3]))
- {
- parts[parts.Length - 3] = "{0}"; // .../CaledoniaTheme/{0}/apps/firefox.png
- }
- string pattern = string.Join("/", parts);
- return pattern;
- }
- private List<IconSize> GetSizes(string path)
- {
- var types = new [] { new Regex("[1-9]+[0-9]+"), new Regex("[1-9]+[0-9]+x[1-9]+[0-9]+") }; // ex.: 64 64x64
- var parts = path.Split('/');
- if (types[0].IsMatch(parts[parts.Length - 2]) || types[0].IsMatch(parts[parts.Length - 3]))
- return new List<IconSize>()
- {
- new IconSize("16", "16x16"),
- new IconSize("22", "22x22"),
- new IconSize("24", "24x24"),
- new IconSize("32", "32x32"),
- new IconSize("64", "64x64"),
- new IconSize("128", "128x128"),
- new IconSize("256", "256x256"),
- new IconSize("512", "512x512"),
- new IconSize("scalable")
- };
- if (types[1].IsMatch(parts[parts.Length - 2]) || types[1].IsMatch(parts[parts.Length - 3]))
- return new List<IconSize>()
- {
- new IconSize("16x16", "16"),
- new IconSize("22x22", "22"),
- new IconSize("24x24", "24"),
- new IconSize("32x32", "32"),
- new IconSize("64x64", "64"),
- new IconSize("128x128", "128"),
- new IconSize("256x256", "256"),
- new IconSize("512x512", "512"),
- new IconSize("scalable")
- };
- throw new Exception("Unknown size type");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement