-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
771 lines (710 loc) · 30.9 KB
/
Form1.cs
File metadata and controls
771 lines (710 loc) · 30.9 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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Security;
namespace Tile_Set_Editor
{
public partial class Form1 : Form
{
private tileset_binary current_tileset_binary;
private bool hexDisplay = true;
private bool dataLoaded = false;
private bool _paletteLoaded = false;
private bool paletteLoaded
{
get { return (_paletteLoaded); }
set
{
if (_paletteLoaded != value) { _paletteLoaded = value; }
if (_paletteLoaded)
{
coloursInGridToolStripMenuItem.Enabled = true;
coloursToolStripMenuItem.Enabled = true;
}
else
{
coloursInGridToolStripMenuItem.Enabled = false;
coloursToolStripMenuItem.Enabled = false;
}
}
}
private List<Byte> colour_indices = new List<Byte>();
private int layer_sel = 0;
public string outStr = "X2";
public int paletteStartAddr = 0x1000;
public int paletteEntryWidth = 4; // number of bytes for each palette entry
public int paletteSize = 0x100;
private byte[] PaletteRGBA = new byte[1024];
private PictureBox[] pictureBox = new PictureBox[256];
private int _gridColour = 0;
private int gridColour
{
get { return _gridColour; }
set
{
if (_gridColour != value) { _gridColour = value; }
if (_gridColour == 0) // Numbers only
{
coloursInGridToolStripMenuItem.Checked = false;
coloursToolStripMenuItem.Checked = false;
numbersToolStripMenuItem.Checked = true;
}
else if (_gridColour == 1) // Numbers and colours
{
coloursInGridToolStripMenuItem.Checked = true;
coloursToolStripMenuItem.Checked = false;
numbersToolStripMenuItem.Checked = false;
}
else if (_gridColour == 2) // Colours only
{
coloursInGridToolStripMenuItem.Checked = false;
coloursToolStripMenuItem.Checked = true;
numbersToolStripMenuItem.Checked = false;
}
}
}
private string palSep = ",";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Exit();
}
private void Exit()
{
Application.Exit();
}
private void ExportPalette()
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Binary File|*.bin";
saveFileDialog1.Title = "Export palette data";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName != "")
{
FileStream fs = (FileStream)saveFileDialog1.OpenFile();
fs.Write(PaletteRGBA, 0, PaletteRGBA.Length);
fs.Close();
}
}
private void binaryToolStripMenuItem_Click(object sender, EventArgs e)
{
ImportBinary();
}
private void PopulateColourIndex(byte[] data)
{
colour_indices.Clear();
for (int i = 0; i < data.Length; i++)
{
if (!colour_indices.Contains(data[i]))
{
colour_indices.Add(data[i]);
}
}
colour_indices.Sort();
PopulatePaletteList();
}
private void AnalyseData()
{
if (dataLoaded)
{
PopulateColourIndex(current_tileset_binary.Data);
current_tileset_binary.Colours = colour_indices.Count;
current_tileset_binary.LowColIndex = colour_indices.FirstOrDefault();
current_tileset_binary.HiColIndex = colour_indices.LastOrDefault();
}
}
private void ImportBinary()
{
OpenFileDialog importBinaryDialogue;
importBinaryDialogue = new OpenFileDialog();
importBinaryDialogue.Filter = "BIN files (*.bin)|*.bin|All files (*.*)|*.*";
importBinaryDialogue.FilterIndex = 1;
importBinaryDialogue.Title = "Import binary file";
importBinaryDialogue.InitialDirectory = "C:\\";
importBinaryDialogue.RestoreDirectory = true;
if (importBinaryDialogue.ShowDialog() == DialogResult.OK)
{
try
{
var data = default(byte[]);
var memstream = new MemoryStream();
var filePath = importBinaryDialogue.FileName;
var fileStream = importBinaryDialogue.OpenFile();
using (StreamReader reader = new StreamReader(fileStream))
{
//var data = reader.ReadToEnd();
reader.BaseStream.CopyTo(memstream);
data = memstream.ToArray();
current_tileset_binary = new tileset_binary(importBinaryDialogue.SafeFileName, data);
current_tileset_binary.Path = filePath;
this.Text = importBinaryDialogue.SafeFileName + " | TSE";
SetupGridView();
PopulateGridView();
UpdateStuff();
}
dataLoaded = true;
}
catch (SecurityException ex)
{
MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" + $"Details:\n\n{ex.StackTrace}");
}
}
}
private void ImportCSV()
{
byte colValue = 0;
// Clear existing PaletteRGBA[] - importing a CSV will create a new palette from the CSV itself
for (int i = 0; i < PaletteRGBA.Length; i++)
{
PaletteRGBA[i] = 0;
}
// Load CSV
OpenFileDialog importCSVDialogue;
importCSVDialogue = new OpenFileDialog();
importCSVDialogue.Filter = "CSV files (*.csv)|*.csv|All files (*.*)|*.*";
importCSVDialogue.FilterIndex = 1;
importCSVDialogue.Title = "Import CSV image";
importCSVDialogue.InitialDirectory = "C:\\";
importCSVDialogue.RestoreDirectory = true;
if (importCSVDialogue.ShowDialog() == DialogResult.OK)
{
List<byte> csvInput = new List<byte>();
using (var reader = new StreamReader(importCSVDialogue.FileName))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
// a line looks something like this:
// 000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,000000FF,
string[] values = line.Split(',');
// values will be a 16-element array (assuming image is 16 pixels wide!), each element being a pixel represented by an RGBA value.
// or it could be a single line with 4,096 elements, depending on how the CSV is saved.
for (int v = 0; v < values.Length; v++)
{
if (values[v] == "") { continue; }
// extract RGBA components from value
byte iR = (byte)Int32.Parse(values[v].ToString().Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
byte iG = (byte)Int32.Parse(values[v].ToString().Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
byte iB = (byte)Int32.Parse(values[v].ToString().Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
byte iA = (byte)Int32.Parse(values[v].ToString().Substring(6, 2), System.Globalization.NumberStyles.HexNumber);
// find a matching colour value in PaletteRGBA[]
bool found = false;
bool newEntry = false;
int colIndex = 0;
for (int i = 0; i < PaletteRGBA.Length - 3; i += 4) // step through PaletteRGBA[] in steps of 4
{
if (PaletteRGBA[i + 3] == 0) // if Alpha value is zero, we will assume this is an empty index entry
{
newEntry = true;
colIndex = i;
break;
}
else if (PaletteRGBA[i] == iR && PaletteRGBA[i + 1] == iG && PaletteRGBA[i + 2] == iB && PaletteRGBA[i + 3] == iA)
{
found = true;
colIndex = i;
break;
}
}
if (newEntry) // if none found, create one
{
PaletteRGBA[colIndex + 0] = iR;
PaletteRGBA[colIndex + 1] = iG;
PaletteRGBA[colIndex + 2] = iB;
PaletteRGBA[colIndex + 3] = iA;
colValue = (byte)(colIndex / 4);
csvInput.Add(colValue);
}
else if (found) // entry found, update colValue
{
colValue = (byte)(colIndex / 4);
csvInput.Add(colValue);
}
}
}
}
// assign the index to current_tileset_binary, then move on to the next colour value.
current_tileset_binary = new tileset_binary(importCSVDialogue.SafeFileName, csvInput.ToArray());
current_tileset_binary.Path = importCSVDialogue.FileName;
this.Text = importCSVDialogue.SafeFileName + " | TSE";
dataLoaded = true;
paletteLoaded = true;
SetupGridView();
PopulateGridView();
UpdateStuff();
PopulatePaletteDetails();
}
}
private void LoadImage()
{
// Load image
OpenFileDialog loadImageDialogue;
loadImageDialogue = new OpenFileDialog();
loadImageDialogue.Filter = "BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif|JPEG files (*.jpg)|*.jpg|PNG files (*.png)|*.png|TIFF files (*.tif)|*.tif|All files (*.*)|*.*";
loadImageDialogue.FilterIndex = 4;
loadImageDialogue.Title = "Load Image";
loadImageDialogue.InitialDirectory = "C:\\";
loadImageDialogue.RestoreDirectory = true;
if (loadImageDialogue.ShowDialog() == DialogResult.OK)
{
var filePath = loadImageDialogue.FileName;
Image image1 = Image.FromFile(filePath);
Bitmap bitmap1 = new Bitmap(image1);
List<byte> imgInput = new List<byte>();
using (MemoryStream ms = new MemoryStream())
{
image1.Save(ms, image1.RawFormat);
for (int y = 0; y < image1.Height; y++)
{
for(int x = 0; x < image1.Width; x++)
{
Color pixelColour = bitmap1.GetPixel(x, y);
// find a matching colour value in PaletteRGBA[]
bool found = false;
bool newEntry = false;
int colIndex = 0;
for (int i = 0; i < PaletteRGBA.Length - 3; i += 4) // step through PaletteRGBA[] in steps of 4
{
if (PaletteRGBA[i + 3] == 0) // if Alpha value is zero, we will assume this is an empty index entry
{
newEntry = true;
colIndex = i;
break;
}
else if (PaletteRGBA[i] == pixelColour.R && PaletteRGBA[i + 1] == pixelColour.G && PaletteRGBA[i + 2] == pixelColour.B && PaletteRGBA[i + 3] == pixelColour.A)
{
found = true;
colIndex = i;
break;
}
}
if (newEntry) // if none found, create one
{
PaletteRGBA[colIndex + 0] = pixelColour.R;
PaletteRGBA[colIndex + 1] = pixelColour.G;
PaletteRGBA[colIndex + 2] = pixelColour.B;
PaletteRGBA[colIndex + 3] = pixelColour.A;
imgInput.Add((byte)(colIndex / 4));
}
else if (found) // entry found, update colValue
{
imgInput.Add((byte)(colIndex / 4));
}
}
}
}
bitmap1.Dispose(); // free up memory
// Analyse palette
ColorPalette pal = image1.Palette;
for (int i = 0; i < pal.Entries.Length; i++)
{
Color col = pal.Entries[i];
PaletteRGBA[i*4] = col.R;
PaletteRGBA[i*4+1] = col.G;
PaletteRGBA[i*4+2] = col.B;
PaletteRGBA[i*4+3] = 255;
}
current_tileset_binary = new tileset_binary(loadImageDialogue.SafeFileName, imgInput.ToArray());
current_tileset_binary.Path = filePath;
current_tileset_binary.Width = image1.Width;
current_tileset_binary.Height = image1.Height;
current_tileset_binary.PixelFormat = image1.PixelFormat;
this.Text = loadImageDialogue.SafeFileName + " | TSE";
dataLoaded = true;
paletteLoaded = true;
image1.Dispose(); // free up memory
SetupGridView();
PopulateGridView();
UpdateStuff();
PopulatePaletteDetails();
}
}
private void ImportARGBPalette()
{
// These are text files with ;-delimited comments at the top (hopefully...
// ...there are examples were a comment is broken by a CRLF or two...
// The actual palette format is ARGB, one entry per line. This needs to be converted
// to RGBA for the GPU palette system.
// Valid lines are 8 characters long (AARRGGBB).
//
OpenFileDialog importPaletteDialogue;
importPaletteDialogue = new OpenFileDialog();
importPaletteDialogue.Filter = "TXT files (*.txt)|*.txt|All files (*.*)|*.*";
importPaletteDialogue.FilterIndex = 1;
importPaletteDialogue.Title = "Import palette file";
importPaletteDialogue.InitialDirectory = "C:\\";
importPaletteDialogue.RestoreDirectory = true;
if (importPaletteDialogue.ShowDialog() == DialogResult.OK)
{
try
{
string line;
int paletteIndex = 0;
StreamReader reader = new StreamReader(importPaletteDialogue.OpenFile());
while ((line = reader.ReadLine()) != null)
{
if (line.Length == 8) // valid comment line
{
PaletteRGBA[paletteIndex+0] = (byte)int.Parse(line.Substring(2, 2), System.Globalization.NumberStyles.HexNumber); // R
PaletteRGBA[paletteIndex+1] = (byte)int.Parse(line.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); // G
PaletteRGBA[paletteIndex+2] = (byte)int.Parse(line.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); // B
PaletteRGBA[paletteIndex+3] = (byte)int.Parse(line.Substring(0, 2), System.Globalization.NumberStyles.HexNumber); // A
if (paletteIndex < 1020)
{
paletteIndex += 4; // increment paletteIndex in steps of 4
}
}
}
paletteLoaded = true;
PopulatePaletteDetails();
label2.Text = "Palette | " + importPaletteDialogue.SafeFileName;
}
catch (SecurityException ex)
{
MessageBox.Show($"Security error.\n\nError message: {ex.Message}\n\n" + $"Details:\n\n{ex.StackTrace}");
}
}
}
public void UpdateStuff()
{
AnalyseData();
PopulatePropertyGrid();
}
private void PopulateGridView()
{
int rowCount = 0;
dataGridView1.Rows.Clear();
// iterate through current_tileset_binary.Data in 16-byte blocks to populate each row in the DataGridView
for (int i = 0; i < current_tileset_binary.Data.Length; i = i + 16)
{
if (i + 16 < current_tileset_binary.Data.Length)
{
string[] row = new string[16];
for (int x=0; x < 16; x++)
{
row[x] = current_tileset_binary.Data[i + x].ToString(outStr);
}
dataGridView1.Rows.Add(row);
dataGridView1.Rows[rowCount].HeaderCell.Value = rowCount.ToString();
rowCount += 1;
}
else
{
string[] row = new string[16];
int a = 0;
for (int x = i; x < current_tileset_binary.Data.Length; x++)
{
row[a] = current_tileset_binary.Data[x].ToString(outStr);
a += 1;
}
dataGridView1.Rows.Add(row);
dataGridView1.Rows[rowCount].HeaderCell.Value = rowCount.ToString();
rowCount += 1;
}
}
dataGridView1.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
if (gridColour == 1)
{
DisplayGridColours();
}
else if (gridColour == 2)
{
DisplayGridColours(true);
}
}
private void PopulatePaletteList()
{
listView1.Items.Clear();
foreach(Byte colour_index in colour_indices)
{
ListViewItem item = new ListViewItem();
string value = PaletteRGBA[colour_index*4].ToString(outStr) + palSep + PaletteRGBA[colour_index*4+1].ToString(outStr) + palSep + PaletteRGBA[colour_index*4+2].ToString(outStr) + palSep + PaletteRGBA[colour_index*4+3].ToString(outStr);
item.Text = colour_index.ToString(outStr);
string addr = (paletteStartAddr + paletteSize*paletteEntryWidth*layer_sel + paletteEntryWidth*colour_index).ToString(outStr);
item.SubItems.Add(addr);
item.SubItems.Add(value);
listView1.Items.Add(item);
}
}
private void SequentialiseIndices()
{
if (!dataLoaded) return;
byte pal_index = 0;
foreach (Byte colour_index in colour_indices)
{
if (colour_index != pal_index)
{
for (int i = 0; i < current_tileset_binary.Data.Length; i++)
{
if (current_tileset_binary.Data[i] == colour_index)
{
current_tileset_binary.Data[i] = pal_index;
}
}
}
pal_index++;
}
PopulateGridView();
AnalyseData();
PopulatePropertyGrid();
}
private void PopulatePaletteDetails()
{
PopulatePaletteList();
// Remove any existing PictureBoxes (in case of a palette reload)
foreach(var pb in this.Controls.OfType<PictureBox>())
{
this.Controls.Remove(pb);
}
// Create a new palette from PictureBoxes
int colIndex = 0;
int pbIndex = 0;
for (int y = 0; y < 16; y++)
{
for (int x = 0; x < 16; x++)
{
PictureBox pictureBox = new PictureBox
{
Name = "pBox" + colIndex.ToString(),
Size = new Size(16, 16),
Location = new Point(1042 + x * 16, 97 + y * 16),
Image = null,
BackColor = Color.FromArgb(PaletteRGBA[pbIndex], PaletteRGBA[pbIndex+1], PaletteRGBA[pbIndex+2])
};
this.Controls.Add(pictureBox);
colIndex++;
pbIndex += 4;
}
}
}
private void PopulatePropertyGrid()
{
propertyGrid1.SelectedObject = current_tileset_binary;
}
private void SetupGridView()
{
dataGridView1.AllowUserToOrderColumns = false;
dataGridView1.AllowUserToResizeColumns = false;
dataGridView1.AllowUserToResizeRows = false;
dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders;
dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;
dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
dataGridView1.ColumnCount = 16;
dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font(dataGridView1.Font, FontStyle.Bold);
dataGridView1.RowHeadersVisible = true;
//dataGridView1.RowHeadersWidth = 24;
for(int i = 0; i < dataGridView1.ColumnCount; i++)
{
dataGridView1.Columns[i].Width = 28;
}
dataGridView1.GridColor = Color.Gray;
dataGridView1.Name = current_tileset_binary.FileName;
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
dataGridView1.Columns[i].Name = i.ToString();
}
dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
dataGridView1.MultiSelect = false;
}
private void hexadecimalToolStripMenuItem_Click(object sender, EventArgs e)
{
hexDisplay = !hexDisplay;
hexadecimalToolStripMenuItem.Checked = hexDisplay;
if (hexDisplay)
{
outStr = "X2";
}
else
{
outStr = "";
}
if (dataLoaded)
{
PopulateGridView();
PopulatePaletteList();
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbLayerPalette.SelectedItem.ToString() == "Layer 0")
{
layer_sel = 0;
}
else if (cmbLayerPalette.SelectedItem.ToString() == "Layer 1")
{
layer_sel = 1;
}
else if (cmbLayerPalette.SelectedItem.ToString() == "Layer 2")
{
layer_sel = 2;
}
else if (cmbLayerPalette.SelectedItem.ToString() == "Layer 3")
{
layer_sel = 3;
}
PopulatePaletteList();
}
private void optimisePaletteIndicesToolStripMenuItem_Click(object sender, EventArgs e)
{
SequentialiseIndices();
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Binary File|*.bin";
saveFileDialog1.Title = "Save File As...";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
// Saves the data via a FileStream created by the OpenFile method.
FileStream fs = (FileStream)saveFileDialog1.OpenFile();
fs.Write(current_tileset_binary.Data, 0, current_tileset_binary.Data.Length);
fs.Close();
}
}
private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
{
Preferences prefs = new Preferences(this);
prefs.Show();
}
private void paintNETPaletteToolStripMenuItem_Click(object sender, EventArgs e)
{
// User wants to import a Paint.NET palette file.
ImportARGBPalette();
}
private void button1_Click(object sender, EventArgs e)
{
// Export palette data
ExportPalette();
}
private void loadToolStripMenuItem_Click(object sender, EventArgs e)
{
// Open an image
LoadImage();
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listView1.SelectedItems.Count == 0)
return;
int palIndex = this.listView1.SelectedItems[0].Index;
SetPaletteBorder(palIndex);
}
private void SetPaletteBorder(int index)
{
// reset all PictureBox borders
foreach (var pb in this.Controls.OfType<PictureBox>())
{
pb.BorderStyle = BorderStyle.None;
}
// set border on selected palette entry
((PictureBox)this.Controls.Find("pBox" + index.ToString(), true)[0]).BorderStyle = BorderStyle.Fixed3D;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (paletteLoaded)
{
object value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (value == null) { return; }
if (outStr == "")
{
SetPaletteBorder(Int32.Parse(value.ToString()));
}
else
{
SetPaletteBorder(Int32.Parse(value.ToString(), System.Globalization.NumberStyles.HexNumber));
}
SelectPaletteListEntry(value.ToString());
}
}
private void SelectPaletteListEntry(string value)
{
var entry = listView1.FindItemWithText(value);
if (entry != null)
{
listView1.SelectedIndices.Clear(); // clear current selection
entry.Selected = true;
}
}
private void ClearGridColours()
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null)
{
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.BackColor = Color.White;
cell.Style = style;
}
}
}
}
private void DisplayGridColours(bool onlyColours = false)
{
if (!paletteLoaded) { return; }
foreach (DataGridViewRow row in dataGridView1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
if (cell.Value != null)
{
DataGridViewCellStyle style = new DataGridViewCellStyle();
int val = 0;
if (outStr == "")
{
val = Int32.Parse(cell.Value.ToString());
}
else
{
val = Int32.Parse(cell.Value.ToString(), System.Globalization.NumberStyles.HexNumber);
}
style.BackColor = Color.FromArgb(PaletteRGBA[val * 4], PaletteRGBA[val * 4 + 1], PaletteRGBA[val * 4 + 2]);
if (onlyColours)
{
style.ForeColor = style.BackColor;
}
else
{
style.ForeColor = Color.FromArgb(style.BackColor.ToArgb()^0xFFFFFF); // invert foreground text colour
}
cell.Style = style;
}
}
}
}
private void coloursInGridToolStripMenuItem_Click(object sender, EventArgs e)
{
gridColour = 1;
DisplayGridColours();
}
private void cSVToolStripMenuItem_Click(object sender, EventArgs e)
{
ImportCSV();
}
private void coloursToolStripMenuItem_Click(object sender, EventArgs e)
{
gridColour = 2;
DisplayGridColours(true);
}
private void numbersToolStripMenuItem_Click(object sender, EventArgs e)
{
gridColour = 0;
ClearGridColours();
}
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
About about = new About();
about.Show();
}
}
}