diff --git a/Wox.Plugin.SystemPlugins/ColorsPlugin.cs b/Wox.Plugin.SystemPlugins/ColorsPlugin.cs new file mode 100644 index 0000000000..2cd01d4046 --- /dev/null +++ b/Wox.Plugin.SystemPlugins/ColorsPlugin.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +using System.Linq; + +namespace Wox.Plugin.SystemPlugins +{ + public sealed class ColorsPlugin : BaseSystemPlugin + { + private const string DIR_PATH = ".\\Plugins\\Colors\\"; + private const int IMG_SIZE = 32; + + private DirectoryInfo ColorsDirectory { get; set; } + + public ColorsPlugin() + { + if(!Directory.Exists(DIR_PATH)) + { + ColorsDirectory = Directory.CreateDirectory(DIR_PATH); + } + else + { + ColorsDirectory = new DirectoryInfo(DIR_PATH); + } + } + + protected override List QueryInternal(Query query) + { + var raw = query.RawQuery; + if(!IsAvailable(raw)) return new List(0); + try + { + var cached = Find(raw); + if(cached.Length == 0) + { + var path = CreateImage(raw); + return new List {new Result(raw, path)}; + } + return cached.Select(x => new Result(raw, x.FullName)).ToList(); + } + catch (Exception exception) + { + // todo: log + return new List(0); + } + } + + private bool IsAvailable(string query) + { + // todo: rgb, names + var length = query.Length - 1; // minus `#` sign + return query.StartsWith("#") && (length == 3 || length == 6); + } + + public FileInfo[] Find(string name) + { + var file = string.Format("{0}.png", name.Substring(1)); + return ColorsDirectory.GetFiles(file, SearchOption.TopDirectoryOnly); + } + + public string CreateImage(string name) + { + using (var bitmap = new Bitmap(IMG_SIZE, IMG_SIZE)) + using (var graphics = Graphics.FromImage(bitmap)) + { + var color = ColorTranslator.FromHtml(name); + graphics.Clear(color); + + var path = CreateFileName(name); + bitmap.Save(path, ImageFormat.Png); + return path; + } + } + + private string CreateFileName(string name) + { + return string.Format("{0}{1}.png", ColorsDirectory.FullName, name.Substring(1)); + } + + protected override void InitInternal(PluginInitContext context) + { + } + } +} \ No newline at end of file diff --git a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj index 44c51eae6d..0576782cd1 100644 --- a/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj +++ b/Wox.Plugin.SystemPlugins/Wox.Plugin.SystemPlugins.csproj @@ -55,6 +55,7 @@ + FileSystemSettings.xaml