/
DFPalette.cs
339 lines (296 loc) · 10.6 KB
/
DFPalette.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: Gavin Clayton (interkarma@dfworkshop.net)
// Contributors:
//
// Notes:
//
#region Using Statements
using System;
using System.IO;
using DaggerfallConnect.Utility;
#endregion
namespace DaggerfallConnect
{
/// <summary>
/// Describes a 256-colour Daggerfall palette. Supports loading .PAL and .COL files.
/// Palette is initialised to all 0xff0000 (red) to make it obvious when palette isn't loaded.
/// </summary>
public class DFPalette
{
#region Fields
/// <summary>
/// Length of header in bytes for supporting .COL files.
/// </summary>
private int headerLength = 0;
/// <summary>
/// Array of 256x RGB values. Includes 8-byte header for supporting .COL files.
/// </summary>
private readonly byte[] paletteBuffer = new byte[776];
#endregion
#region Properties
/// <summary>
/// Gets palette memory buffer.
/// </summary>
public byte[] PaletteBuffer
{
get { return paletteBuffer; }
}
/// <summary>
/// Gets palette header length;
/// </summary>
public int HeaderLength
{
get { return headerLength; }
}
#endregion
#region Constructors
/// <summary>
/// Default constructor. Palette is initialised with red.
/// </summary>
public DFPalette()
{
Fill(0xff, 0, 0);
}
/// <summary>
/// Load constructor (supports both .PAL and .COL files).
/// </summary>
/// <param name="FilePath">Absolute path to palette file.</param>
public DFPalette(string FilePath)
{
if (!Load(FilePath))
Fill(0xff, 0, 0);
}
/// <summary>
/// Copy constructor.
/// </summary>
/// <param name="other">Palette to copy from.</param>
public DFPalette(DFPalette other)
{
if (other != null && other.PaletteBuffer != null && other.paletteBuffer.Length != 0)
{
headerLength = other.HeaderLength;
paletteBuffer = new byte[other.PaletteBuffer.Length];
Array.Copy(other.PaletteBuffer, paletteBuffer, other.PaletteBuffer.Length);
}
}
/// <summary>
/// Loads a Daggerfall palette file (supports both .PAL and .COL files).
/// </summary>
/// <param name="FilePath">Absolute path to palette file.</param>
/// <returns>True if successful, otherwise false.</returns>
public bool Load(string FilePath)
{
FileProxy fileProxy = new FileProxy(FilePath, FileUsage.UseMemory, true);
switch (fileProxy.Length)
{
case 768:
headerLength = 0;
break;
case 776:
headerLength = 8;
break;
default:
return false;
}
// Read palette
BinaryReader reader = fileProxy.GetReader();
if (fileProxy.Length != reader.Read(paletteBuffer, 0, fileProxy.Length))
return false;
// Multiply MAP.PAL
if (Path.GetFileName(FilePath) == "MAP.PAL")
Multiply(4);
return true;
}
/// <summary>
/// Read palette information from a binary reader.
/// Palette must be a 768-byte PalFile structure (256x 24-bit RGB values).
/// </summary>
/// <param name="Reader">Source reader positioned at start of palette data.</param>
/// <returns>True if successful, otherwise false.</returns>
public bool Read(ref BinaryReader Reader)
{
// Read palette bytes
if (768 != Reader.Read(paletteBuffer, 8, 768))
return false;
// Set header length
headerLength = 8;
return true;
}
/// <summary>
/// Fills entire palette with specified RGB value.
/// </summary>
/// <param name="R">Red component.</param>
/// <param name="G">Green component.</param>
/// <param name="B">Blue component.</param>
public void Fill(byte R, byte G, byte B)
{
int offset = headerLength;
for (int i = 0; i < 256; i++)
{
paletteBuffer[offset++] = R;
paletteBuffer[offset++] = G;
paletteBuffer[offset++] = B;
}
}
/// <summary>
/// Fills entire palette with grayscale values.
/// </summary>
public void MakeGrayscale()
{
int offset = headerLength;
for (int i = 0; i < 256; i++)
{
paletteBuffer[offset++] = (byte)i;
paletteBuffer[offset++] = (byte)i;
paletteBuffer[offset++] = (byte)i;
}
}
/// <summary>
/// Fills entire palette with random values.
/// </summary>
public void MakeRandom()
{
Random rnd = new Random();
int offset = headerLength;
for (int i = 0; i < 256; i++)
{
paletteBuffer[offset++] = (byte)(rnd.Next() % 255);
paletteBuffer[offset++] = (byte)(rnd.Next() % 255);
paletteBuffer[offset++] = (byte)(rnd.Next() % 255);
}
}
public void Multiply(int scale)
{
int offset = headerLength;
for (int i = 0; i < 256; i++)
{
paletteBuffer[offset] = (byte)(paletteBuffer[offset++] * scale);
paletteBuffer[offset] = (byte)(paletteBuffer[offset++] * scale);
paletteBuffer[offset] = (byte)(paletteBuffer[offset++] * scale);
}
}
/// <summary>
/// Fills palette with AutoMap colours. The AutoMap colour index is equal to BuildingTypes value + 1.
/// </summary>
public void MakeAutomap()
{
// Fill to most common colour
Fill(69, 60, 40);
// Guild halls
Set(12, 69, 125, 195);
// Temples
Set(15, 69, 125, 195);
// Taverns
Set(16, 85, 117, 48);
// Stores
Set(1, 190, 85, 24); // Alchemist
Set(3, 190, 85, 24); // Amorer
Set(4, 190, 85, 24); // Bank
Set(6, 190, 85, 24); // Bookseller
Set(7, 190, 85, 24); // Clothing Store
Set(8, 190, 85, 24); // Furniture Store
Set(9, 190, 85, 24); // Gem Store
Set(10, 190, 85, 24); // General Store
Set(11, 190, 85, 24); // Library
Set(13, 190, 85, 24); // Pawn Shop
Set(14, 190, 85, 24); // Weapon Smith
// Special 1-4
//Set(0x75, 255, 0, 0);
//Set(0xe0, 255, 0, 0);
//Set(0xfa, 255, 0, 0);
// Ground flats
Set(0xfb, 0, 0, 0);
}
/// <summary>
/// Gets colour at specified index.
/// </summary>
/// <param name="Index">Index into colour array.</param>
/// <returns>DFColor object.</returns>
public DFColor Get(int Index)
{
int offset = headerLength + Index * 3;
DFColor col = new DFColor(paletteBuffer[offset], paletteBuffer[offset + 1], paletteBuffer[offset + 2]);
return col;
}
/// <summary>
/// Gets red colour value at index.
/// </summary>
/// <param name="Index">Index into colour array.</param>
/// <returns>Red value byte.</returns>
public byte GetRed(int Index)
{
int offset = headerLength + Index * 3;
return paletteBuffer[offset];
}
/// <summary>
/// Gets green colour value at index.
/// </summary>
/// <param name="Index">Index into colour array.</param>
/// <returns>Green value byte.</returns>
public byte GetGreen(int Index)
{
int offset = headerLength + Index * 3;
return paletteBuffer[offset + 1];
}
/// <summary>
/// Gets blue colour value at index.
/// </summary>
/// <param name="Index">Index into colour array.</param>
/// <returns>Blue value byte.</returns>
public byte GetBlue(int Index)
{
int offset = headerLength + Index * 3;
return paletteBuffer[offset + 2];
}
/// <summary>
/// Sets index to specified RGB values.
/// </summary>
/// <param name="Index">Index into colour array.</param>
/// <param name="R">Red component.</param>
/// <param name="G">Green component.</param>
/// <param name="B">Blue component.</param>
public void Set(int Index, byte R, byte G, byte B)
{
int offset = headerLength + Index * 3;
paletteBuffer[offset] = R;
paletteBuffer[offset + 1] = G;
paletteBuffer[offset + 2] = B;
}
/// <summary>
/// Sets 768-byte palette buffer directly.
/// </summary>
/// <param name="data">Data to set.</param>
public void Set(byte[] data)
{
if (data.Length != 768)
throw new Exception("DFPalette: Invalid buffer length. Must be 768 bytes for direct set.");
headerLength = 0;
Array.Copy(data, paletteBuffer, 768);
}
/// <summary>
/// Finds index with specified RGB values.
/// </summary>
/// <param name="R">Red component.</param>
/// <param name="G">Green component.</param>
/// <param name="B">Blue component.</param>
/// <returns>Index of found RGB value.</returns>
public int Find(byte R, byte G, byte B)
{
int offset = headerLength;
for (int i = 0; i < 256; i++)
{
// Check for match
if (paletteBuffer[offset] == R && paletteBuffer[offset + 1] == G && paletteBuffer[offset + 2] == B)
return i;
// Increment offset
offset += 3;
}
return -1;
}
#endregion
}
}