Пример #1
0
    public void TestCaseFormatColor()
    {
        ColorPresets.Reflash();
        int count = ColorPresets.Count();

        string[] keys = ColorPresets.GetColorKeys();
        if (keys == null)
        {
            return;
        }

        string format  = "[#{0}]asdfsdfsdf111[-]";
        string format2 = "[{0}]asdfsdfsdf111[-]";

        for (int i = 0; i < count; ++i)
        {
            string key   = keys[i];
            string num   = key.Remove(0, ColorPresets.COLOR_PREFIX.Length);
            string value = ColorPresets.GetHexString(key);

            string lhs = ColorPresets.FormatText(string.Format(format, num));
            string rhs = string.Format(format2, value);
            Assert.AreEqual(lhs, rhs);
        }
    }
Пример #2
0
    private static void PrintColorPresets()
    {
        if (EditorApplication.isCompiling)
        {
            EditorUtility.DisplayDialog("提示", "编译完再试喔", "Ok");
            return;
        }

        ColorPresets.Reflash();
        int count = ColorPresets.Count();

        string[] keys = ColorPresets.GetColorKeys();
        if (keys == null)
        {
            return;
        }

        string ret = "";

        for (int i = 0; i < count; ++i)
        {
            string key   = keys[i];
            string value = ColorPresets.GetHexString(key);
            ret += string.Format("{0} {1}\n", key, value);
        }

        Debug.Log(ret);
    }
Пример #3
0
        private void LoadPresetData(object param)
        {
            //IsBusy = true;

            StatusMessage = string.Empty;

            BuildingPresets buildingPresets = null;

            try
            {
                buildingPresets = SerializationHelper.LoadFromFile <BuildingPresets>(PresetsVM.SelectedFile);
            }
            catch (Exception ex)
            {
                var message = $"Error parsing {nameof(BuildingPresets)}.";
                Trace.WriteLine($"{message}{Environment.NewLine}{ex}");
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                StatusMessage = $"{message} -> Maybe wrong selected file?";
                return;
            }

            PresetsVersion = buildingPresets.Version;

            fillAvailableTemplates(buildingPresets);
            fillAvailableIdentifiers(buildingPresets);
            fillTemplateIdentifierMapping(buildingPresets);

            AvailableColorSchemes.Clear();

            ColorPresets colorPresets = null;

            try
            {
                ColorPresetsLoader loader = new ColorPresetsLoader();
                colorPresets = loader.Load(ColorsVM.SelectedFile);
            }
            catch (Exception ex)
            {
                var message = $"Error parsing {nameof(ColorPresets)}.";
                Trace.WriteLine($"{message}{Environment.NewLine}{ex}");
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                StatusMessage = $"{message} -> Maybe wrong selected file?";
                return;
            }

            foreach (var curScheme in colorPresets.AvailableSchemes)
            {
                AvailableColorSchemes.Add(new ColorSchemeViewModel(curScheme));
            }
            //var defaultScheme = loader.LoadDefaultScheme(vmColors.SelectedFile);

            ColorPresetsVersion        = colorPresets.Version;
            ColorPresetsVersionUpdated = ColorPresetsVersion;

            SelectedColorScheme = AvailableColorSchemes.First();

            //IsBusy = false;
        }
Пример #4
0
        public static void SaveColorPresets(ColorPresets presets)
        {
            try
            {
                string fileName;
                if (!TryGetFileName(out fileName))
                {
                    throw new Exception("Unable to get file name.");
                }

                // Write Data
                using (FileStream fileStream = File.Open(fileName, FileMode.Create, FileAccess.Write))
                {
                    using (StreamWriter sw = new StreamWriter(fileStream))
                    {
                        sw.WriteLine("Version 2");
                        for (int i = 0; i < presets.Count; ++i)
                        {
                            Color c = presets[i];
                            sw.WriteLine(c.r + ":" + c.g + ":" + c.b + ":" + c.a);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(
                    "Problem while saving ReColor Color Presets:\n" +
                    e.GetType().Name + " " + e.Message + "\n" + e.StackTrace);
            }
        }
Пример #5
0
    public void TestCaseColorToHexString()
    {
        Color32 rgb  = new Color32(134, 234, 123, 252);
        string  str  = ColorPresets.ColorToHexString(rgb);
        Color32 rgb2 = ColorPresets.HexStringToColor(str);

        Assert.AreEqual(rgb, rgb2);
    }
Пример #6
0
        private static ColorPresets CreateDefaultColorPresets()
        {
            ColorPresets presets = new ColorPresets();

            for (int i = 0; i < presets.Count; ++i)
            {
                presets[i] = Color.white;
            }
            return(presets);
        }
        public ColorPresets Load(string pathToColorPresetsFile)
        {
            ColorPresets result = null;

            try
            {
                result = SerializationHelper.LoadFromFile <ColorPresets>(pathToColorPresetsFile);
            }
            catch (Exception ex)
            {
                Trace.WriteLine($"Error loading the colors.{Environment.NewLine}{ex}");
                throw;
            }

            return(result);
        }
Пример #8
0
        public ColorPresets Load(string pathToColorPresetsFile)
        {
            ColorPresets result = null;

            try
            {
                result = SerializationHelper.LoadFromFile <ColorPresets>(pathToColorPresetsFile);
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error loading the colors.");
                throw;
            }

            return(result);
        }
Пример #9
0
    public void GenerateColorPreset(string _colorPath, string _codePath)
    {
        if (string.IsNullOrEmpty(_colorPath) || string.IsNullOrEmpty(_codePath))
        {
            return;
        }

        Debug.Log("_colorPath = " + _colorPath);
        Debug.Log("_codePath = " + _codePath);

        // 获取颜色样式
        System.Object[] instanceArray               = UnityEditorInternal.InternalEditorUtility.LoadSerializedFileAndForget(_colorPath);
        System.Type     typeColorPresetLibrary      = System.Type.GetType("UnityEditor.ColorPresetLibrary,UnityEditor");
        System.Reflection.MethodInfo _CountFunc     = typeColorPresetLibrary.GetMethod("Count");
        System.Reflection.MethodInfo _GetNameFunc   = typeColorPresetLibrary.GetMethod("GetName");
        System.Reflection.MethodInfo _GetPresetFunc = typeColorPresetLibrary.GetMethod("GetPreset");

        string genCode = "";

        System.Object colorLibIntance = instanceArray[0];
        int           count           = (int)_CountFunc.Invoke(colorLibIntance, null);

        for (int i = 0; i < count; ++i)
        {
            string name = (string)_GetNameFunc.Invoke(colorLibIntance, new System.Object[1] {
                i
            });
            Color col = (Color)_GetPresetFunc.Invoke(colorLibIntance, new System.Object[1] {
                i
            });
            string hexStr = ColorPresets.ColorToHexString(col);
            genCode += string.Format(FORMAT_COLORITEM, ColorPresets.COLOR_PREFIX, name, hexStr);
        }

        // 插入到目标代码颜色码区域
        string content    = System.IO.File.ReadAllText(_codePath);
        int    beginIndex = content.IndexOf(BEGIN_GENERATE_CODE);
        int    endIndex   = content.IndexOf(END_GENERATE_CODE);

        string upContent   = content.Substring(0, beginIndex + BEGIN_GENERATE_CODE.Length);
        string backContent = content.Substring(endIndex);

        string newStr = upContent + "\n" + genCode + backContent;

        Debug.Log(newStr);
        File.WriteAllText(_codePath, newStr);
    }
Пример #10
0
        private void Save(object param)
        {
            try
            {
                StatusMessage = string.Empty;

                var colorPresets = new ColorPresets
                {
                    Version = ColorPresetsVersionUpdated
                };

                colorPresets.AvailableSchemes.AddRange(AvailableColorSchemes.Select(x => new ColorScheme
                {
                    Name   = x.Name,
                    Colors = x.Colors.Select(color => new PredefinedColor
                    {
                        TargetTemplate    = color.TargetTemplate,
                        TargetIdentifiers = color.TargetIdentifiers.ToList(),
                        Color             = color.SelectedColor.Value
                    }).ToList()
                }));

                var backupFilePath = ColorsVM.SelectedFile + ".bak";
                logger.Trace($"create backup of \"{ColorsVM.SelectedFile}\" at \"{backupFilePath}\"");
                File.Copy(ColorsVM.SelectedFile, backupFilePath, true);
                StatusMessage = $"Backup created \"{backupFilePath}\".";

                logger.Info($"save color presets file: \"{ColorsVM.SelectedFile}\" ({colorPresets.Version})");
                SerializationHelper.SaveToFile <ColorPresets>(colorPresets, ColorsVM.SelectedFile);

                MessageBox.Show("New colors.json was saved.", "Saved", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                var message = "Error saving colors.json.";
                logger.Error(ex, message);
                MessageBox.Show(message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);

                StatusMessage = $"{message} -> Could not save to \"{ColorsVM.SelectedFile}\"";
                return;
            }
        }
Пример #11
0
        public static ColorPresets LoadColorPresets()
        {
            ColorPresets presets = CreateDefaultColorPresets();

            try
            {
                string fileName;
                if (TryGetFileName(out fileName) &&
                    File.Exists(fileName))
                {
                    // Load Data
                    using (StreamReader sr = new StreamReader(fileName))
                    {
                        string version = sr.ReadLine();
                        if (version.Equals("Version 1"))
                        {
                            for (int i = 0; i < presets.Count; ++i)
                            {
                                try
                                {
                                    string[] s = sr.ReadLine().Split(new Char[] { ':' });
                                    Color    c = Color.white;
                                    c.r        = float.Parse(s[0]);
                                    c.g        = float.Parse(s[1]);
                                    c.b        = float.Parse(s[2]);
                                    presets[i] = c;
                                }
                                catch
                                {
                                    presets[i] = Color.white;
                                }
                            }
                        }
                        if (version.Equals("Version 2"))
                        {
                            for (int i = 0; i < presets.Count; ++i)
                            {
                                try
                                {
                                    string[] s = sr.ReadLine().Split(new Char[] { ':' });
                                    Color    c = Color.white;
                                    c.r        = float.Parse(s[0]);
                                    c.g        = float.Parse(s[1]);
                                    c.b        = float.Parse(s[2]);
                                    c.a        = float.Parse(s[3]);
                                    presets[i] = c;
                                }
                                catch
                                {
                                    presets[i] = Color.white;
                                }
                            }
                        }
                        else
                        {
                            presets = new ColorPresets();
                            {
                                for (int i = 0; i < presets.Count; ++i)
                                {
                                    presets[i] = Color.white;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Log.Warning(
                    "Problem while loading ReColor Color Presets:\n" +
                    e.GetType().Name + " " + e.Message + "\n" + e.StackTrace);
                presets = CreateDefaultColorPresets();
            }
            presets.IsModified = false;
            return(presets);
        }
Пример #12
0
 public String GetNextColor()
 {
     return(ColorPresets.GetValue(Random.Next(ColorPresets.Length)).ToString());
 }