This repository was archived by the owner on May 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCustomColor.cs
More file actions
148 lines (139 loc) · 4.99 KB
/
CustomColor.cs
File metadata and controls
148 lines (139 loc) · 4.99 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using QQS_UI.Core;
#nullable disable
namespace QQS_UI
{
public class CustomColor
{
public RGBAColor[] Colors;
public CustomColor(string colorFileName = "colors.json")
{
if (!File.Exists(colorFileName))
{
string colors = JsonSerializer.Serialize(Global.KeyColors);
File.WriteAllText(colorFileName, colors);
Colors = new RGBAColor[96];
Array.Copy(Global.DefaultColors, Colors, 96);
}
else
{
try
{
string colorData = File.ReadAllText(colorFileName);
Colors = JsonSerializer.Deserialize<RGBAColor[]>(colorData);
}
catch
{
Console.WriteLine("加载颜色配置时出现错误, 将使用默认颜色...");
Colors = new RGBAColor[96];
Array.Copy(Global.DefaultColors, Colors, 96);
}
}
Console.WriteLine("颜色加载完成. 共 {0} 种颜色.", 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 = JsonSerializer.Deserialize<RGBAColor[]>(colorData);
}
catch
{
Colors = lastColors;
return 1;
}
return 0;
}
/// <summary>
/// 将当前实例的颜色拷贝到<see cref="Global.KeyColors"/>中.<br/>
/// Copy colors owned by current instance to <see cref="Global.KeyColors"/>.
/// </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.KeyColors = new RGBAColor[Colors.Length];
Global.NoteColors = new RGBAColor[Colors.Length];
Array.Copy(Colors, Global.KeyColors, Colors.Length);
Array.Copy(Colors, Global.NoteColors, Colors.Length);
return 0;
}
public void UseDefault()
{
Colors = new RGBAColor[96];
Array.Copy(Global.DefaultColors, Colors, 96);
Global.KeyColors = Colors;
}
public CustomColor Shuffle()
{
CustomColor shuffled = new()
{
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;
}
public CustomColor Exchange(RGBAColor[] colors)
{
CustomColor old = new()
{
Colors = new RGBAColor[Colors.Length]
};
Array.Copy(Colors, old.Colors, Colors.Length);
Colors = new RGBAColor[colors.Length];
colors.CopyTo(Colors, 0);
return old;
}
}
}