forked from az64/mm-rando
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tunics.cs
262 lines (249 loc) · 8.57 KB
/
Tunics.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.Drawing;
namespace MMRando
{
public partial class ROMFuncs
{
static int[] sizes = new int[] { 14, 128, 33, 512, 16 };
static bool[] grad = new bool[] { false, false, false, true, false };
static bool[] zora = new bool[] { false, false, true, true, false };
static bool[] fd = new bool[] { false, false, false, false, true };
private static float Interpolate(float y0, float y1, float x0, float x1, float x)
{
if (x0 == x1)
{
return y0;
}
else
{
return y0 * ((x1 - x) / (x1 - x0)) + y1 * ((x - x0) / (x1 - x0));
};
}
private static ushort ToRGBA5551(Color c)
{
byte r = (byte)((c.R >> 3) & 0x1F);
byte g = (byte)((c.G >> 3) & 0x1F);
byte b = (byte)((c.B >> 3) & 0x1F);
byte a;
if (c.A > 0x7F)
{
a = 1;
}
else
{
a = 0;
};
return (ushort)((r << 11) | (g << 6) | (b << 1) | a);
}
private static Color FromRGBA5551(ushort c)
{
float r = (c & 0xF800) >> 11;
float g = (c & 0x7C0) >> 6;
float b = (c & 0x3E) >> 1;
int a = (c & 1) * 255;
return Color.FromArgb(a, (int)(r * 255.0f / 31.0f), (int)(g * 255.0f / 31.0f), (int)(b * 255.0f / 31.0f));
}
private static Color GetAverageColour(Color[] c, int count)
{
float r = 0f;
float g = 0f;
float b = 0f;
float d = 1.0f / (float)count;
for (int i = 0; i < count; i++)
{
r += (float)c[i].R * d;
g += (float)c[i].G * d;
b += (float)c[i].B * d;
};
return Color.FromArgb((int)Math.Round(r), (int)Math.Round(g), (int)Math.Round(b));
}
private static Color[] ShiftHue(Color[] c, Color target, int count, bool zora, bool grad, bool fd)
{
Color[] s = new Color[count];
Color a;
if (zora && !grad)
{
a = GetAverageColour(c, 16);
}
else
{
a = GetAverageColour(c, count);
};
float rot = target.GetHue() - a.GetHue();
float avgb = a.GetBrightness();
float avgs = a.GetSaturation();
for (int i = 0; i < count; i++)
{
if ((i == 12) && (count == 14))
{
s[i] = c[i];
continue;
};
float h = c[i].GetHue();
float b = c[i].GetBrightness();
float sat = c[i].GetSaturation();
b -= avgb;
sat -= avgs;
b += target.GetBrightness();
sat += target.GetSaturation();
h += rot;
if (fd)
{
sat = target.GetSaturation();
h = target.GetHue();
};
if (zora && grad)
{
if (i > 351)
{
float x0 = c[352].GetBrightness();
float x1 = c[511].GetBrightness();
float x = c[i].GetBrightness();
h = target.GetHue();
sat = target.GetSaturation();
b = Interpolate(target.GetBrightness(), c[511].GetBrightness(), x0, x1, x);
};
};
h %= 360f;
if (h < 0f)
{
h += 360f;
};
if (b < 0.0f)
{
b = 0.0f;
};
if (b > 1.0f)
{
b = 1.0f;
};
if (sat < 0.0f)
{
sat = 0.0f;
};
if (sat > 1.0f)
{
sat = 1.0f;
};
s[i] = FromAHSB(c[i].A, h, sat, b);
//this code is a mess
if (zora && grad)
{
if (i < 96)
{
s[i] = c[i];
}
else if (i < 352)
{
float x0 = c[95].GetBrightness();
float x1 = c[352].GetBrightness();
float x = c[i].GetBrightness();
int rr = (int)Interpolate(c[95].R, target.R, x0, x1, x);
int gg = (int)Interpolate(c[95].G, target.G, x0, x1, x);
int bb = (int)Interpolate(c[95].B, target.B, x0, x1, x);
if (rr < 0)
{
rr = 0;
};
if (rr > 255)
{
rr = 255;
};
if (gg < 0)
{
gg = 0;
};
if (gg > 255)
{
gg = 255;
};
if (bb < 0)
{
bb = 0;
};
if (bb > 255)
{
bb = 255;
};
s[i] = Color.FromArgb(rr, gg, bb);
};
}
else if (zora)
{
if (i > 15)
{
float x0 = c[14].GetBrightness();
float x1 = c[31].GetBrightness();
float x = c[i].GetBrightness();
int rr = (int)Interpolate(s[14].R, c[31].R, x0, x1, x);
int gg = (int)Interpolate(s[14].G, c[31].G, x0, x1, x);
int bb = (int)Interpolate(s[14].B, c[31].B, x0, x1, x);
if (rr < 0)
{
rr = 0;
};
if (rr > 255)
{
rr = 255;
};
if (gg < 0)
{
gg = 0;
};
if (gg > 255)
{
gg = 255;
};
if (bb < 0)
{
bb = 0;
};
if (bb > 255)
{
bb = 255;
};
s[i] = Color.FromArgb(rr, gg, bb);
};
};
};
return s;
}
private static Color[] ReadColours(int file, int addr, int count)
{
Color[] c = new Color[count];
for (int i = 0; i < count; i++)
{
int ca = addr + (i * 2);
ushort rgba = (ushort)((MMFileList[file].Data[ca] << 8) | MMFileList[file].Data[ca + 1]);
c[i] = FromRGBA5551(rgba);
};
return c;
}
private static void WriteColours(int file, int addr, int count, Color[] c)
{
for (int i = 0; i < count; i++)
{
int ca = addr + (i * 2);
ushort rgba = ToRGBA5551(c[i]);
MMFileList[file].Data[ca] = (byte)(rgba >> 8);
MMFileList[file].Data[ca + 1] = (byte)(rgba & 0xFF);
};
}
public static void UpdateFormTunics(List<int[]> addresses, Color target)
{
for (int i = 0; i < addresses.Count; i++)
{
for (int j = 0; j < addresses[i].Length; j++)
{
int f = AddrToFile((uint)addresses[i][j]);
CheckCompressed(f);
int a = addresses[i][j] - MMFileList[f].Addr;
Color[] c = ReadColours(f, a, sizes[i]);
c = ShiftHue(c, target, sizes[i], zora[i], grad[i], fd[i]);
WriteColours(f, a, sizes[i], c);
};
};
}
}
}