This repository has been archived by the owner on Oct 24, 2021. It is now read-only.
forked from null7323/QMidiCore-Quaver-Stream-Renderer
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomColor.cs
134 lines (126 loc) · 4.48 KB
/
CustomColor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QQS_UI.Core;
namespace QQS_UI
{
public class CustomColor
{
public RGBAColor[] Colors;
public CustomColor(string colorFileName = "colors.json")
{
if (!File.Exists(colorFileName))
{
string colors = JsonConvert.SerializeObject(Global.Colors);
File.WriteAllText(colorFileName, colors);
Colors = new RGBAColor[96];
Array.Copy(Global.DefaultColors, Colors, 96);
}
else
{
try
{
string colorData = File.ReadAllText(colorFileName);
Colors = JsonConvert.DeserializeObject<RGBAColor[]>(colorData);
}
catch
{
Console.WriteLine("Error loading palette. Using default coloursS...");
Colors = new RGBAColor[96];
Array.Copy(Global.DefaultColors, Colors, 96);
}
}
Console.WriteLine("Colours loaded. Loaded {0} colours.", Colors.Length);
}
private CustomColor()
{
}
/// <summary>
/// 将指定的文件的颜色加载到当前实例中.
/// </summary>
/// <param name="colorFileName">颜色文件路径.</param>
/// <returns>如果文件不存在, 返回-1; 如果加载时出现问题, 返回1; 如果没有错误, 返回0.</returns>
public int Load(string colorFileName)
{
if (!File.Exists(colorFileName))
{
return -1;
}
string colorData = File.ReadAllText(colorFileName);
RGBAColor[] lastColors = Colors;
try
{
Colors = JsonConvert.DeserializeObject<RGBAColor[]>(colorData);
}
catch
{
Colors = lastColors;
return 1;
}
return 0;
}
/// <summary>
/// 将当前实例的颜色拷贝到<see cref="Global.Colors"/>中.<br/>
/// Copy colors owned by current instance to <see cref="Global.Colors"/>.
/// </summary>
/// <remarks>
/// 请注意: 这不是一个线程安全操作.<br/>
/// This is not a thread-safe operation.
/// </remarks>
/// <returns>
/// 如果当前颜色为<see langword="null"/>, 返回-1;<br/>
/// 如果当前颜色不为<see langword="null"/>, 但是长度为0, 返回1;<br/>
/// 如果操作无异常, 返回0.<br/>
/// If colors owned by <see langword="this"/> is null then -1 will be returned;<br/>
/// If the color array owned by <see langword="this"/> is not null but its length equals 0, then 1 is returned;<br/>
/// If the operation is successful, returns 0.
/// </returns>
public int SetGlobal()
{
if (Colors == null)
{
return -1;
}
if (Colors.Length == 0)
{
return 1;
}
Global.Colors = new RGBAColor[Colors.Length];
Array.Copy(Colors, Global.Colors, Colors.Length);
return 0;
}
public void UseDefault()
{
Colors = new RGBAColor[96];
Array.Copy(Global.DefaultColors, Colors, 96);
Global.Colors = Colors;
}
public CustomColor Shuffle()
{
CustomColor shuffled = new CustomColor
{
Colors = new RGBAColor[Colors.Length]
};
Array.Copy(Colors, shuffled.Colors, Colors.Length);
Random rand = new Random(DateTime.Now.Millisecond);
for (int i = 0; i < shuffled.Colors.Length; i++)
{
int x, y;
RGBAColor col;
x = rand.Next(0, shuffled.Colors.Length);
do
{
y = rand.Next(0, shuffled.Colors.Length);
} while (y == x);
col = shuffled.Colors[x];
shuffled.Colors[x] = shuffled.Colors[y];
shuffled.Colors[y] = col;
}
return shuffled;
}
}
}