-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPalette.cs
More file actions
256 lines (233 loc) · 8.63 KB
/
Copy pathPalette.cs
File metadata and controls
256 lines (233 loc) · 8.63 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
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System;
using System.Collections.Generic;
internal class Palette
{
public const uint PaletteSize = 256;
public byte[][] Colors = new byte[PaletteSize][];
public Palette() { }
public Palette(BinaryReader binreader, bool LEVformat = false)
{
for (uint i = 0; i < PaletteSize; ++i)
{
var color = Colors[i] = new byte[4];
color[0] = binreader.ReadByte();
color[1] = binreader.ReadByte();
color[2] = binreader.ReadByte();
if (!LEVformat)
color[3] = binreader.ReadByte();
}
}
public static Color Convert(byte[] src)
{
return Color.FromArgb(byte.MaxValue, src[0], src[1], src[2]);
}
public static byte[] Convert(Color src, bool respectAlpha = false)
{
return new byte[4] { src.R, src.G, src.B, respectAlpha ? src.A : byte.MaxValue };
}
internal void CopyFrom(Palette other)
{
if (other != null)
{
for (int i = 0; i < PaletteSize; ++i)
Colors[i] = other.Colors[i].Clone() as byte[];
}
}
internal void Apply(Bitmap bitmap, Color? firstColor = null)
{
var palette = bitmap.Palette;
var entries = palette.Entries;
for (uint i = 0; i < PaletteSize; ++i)
entries[i] = Convert(Colors[i]);
if (firstColor.HasValue)
entries[0] = firstColor.Value;
bitmap.Palette = palette;
}
internal Palette(ColorPalette original)
{
var entries = original.Entries;
for (uint i = 0; i < PaletteSize; ++i)
Colors[i] = Convert(entries[i]);
}
public void WriteLEVStyle(BinaryWriter binwriter)
{
foreach (byte[] color in Colors)
{
binwriter.Write(color[0]);
binwriter.Write(color[1]);
binwriter.Write(color[2]);
}
}
public byte[] this[int key]
{
get
{
return Colors[key];
}
set
{
Colors[key] = value;
}
}
public override bool Equals(object obj)
{
if (obj == null || GetType() != obj.GetType()) return false;
else
{
Palette other = obj as Palette;
for (int i = 0; i < PaletteSize; ++i)
if (!Colors[i].SequenceEqual(other.Colors[i]))
return false;
return true;
}
}
internal byte FindNearestColor(byte[] requestedColor)
{
byte nearestEntry = 15;
int smallestSquaredDifference = 0x40000;
for (int i = 16; i < 256; i++)
{
int squaredDifference = 0;
for (int j = 0; j < 3; j++)
{
int difference = (int)Colors[i][j] - (int)requestedColor[j];
squaredDifference += difference * difference;
}
if (squaredDifference < smallestSquaredDifference)
{
nearestEntry = (byte)i;
smallestSquaredDifference = squaredDifference;
}
}
return nearestEntry;
}
}
namespace MLLE
{
class PaletteImage : PictureBox
{
public const int PaletteLengthOnEitherDimension = 16;
int ColorSize, BorderSize, ColorTotalSize;
public static readonly int[] AllPaletteColors = Enumerable.Range(0, (int)Palette.PaletteSize).ToArray();
public bool[] ColorDisabled = new bool[Palette.PaletteSize];
public int NumberOfColorsToSelectAtATime = 1;
private Palette _Palette = new Palette();
public Palette Palette {
get {
return _Palette;
}
set
{
_Palette.CopyFrom(value);
Update(AllPaletteColors);
}
}
private Bitmap ImageImage;
bool ReadOnly;
public PaletteImage(int colorSize, int borderSize, bool readOnly, bool registerStandardEvents)
{
Width = Height = (int)((ColorTotalSize = (ColorSize = colorSize) + (BorderSize = borderSize) * 2) * PaletteLengthOnEitherDimension);
ImageImage = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
ColorsSelected = new bool[Palette.PaletteSize];
ReadOnly = readOnly;
if (registerStandardEvents)
{
MouseDown += Clicked;
if (!ReadOnly)
MouseDoubleClick += DoubleClicked;
MouseMove += Moved;
}
}
public bool CurrentlySelectingColors;
public void Moved(object sender, MouseEventArgs e)
{
if (!(sender as Control).ClientRectangle.Contains(e.Location))
return;
if (e.Button != MouseButtons.None)
{
if (ColorsSelected[getSelectedColor(e)] != CurrentlySelectingColors)
Clicked(sender, e);
}
}
public int getSelectedColor(MouseEventArgs e)
{
return (e.X / ColorTotalSize) + (e.Y / ColorTotalSize * PaletteLengthOnEitherDimension);
}
public void Clicked(object sender, MouseEventArgs e)
{
int colorSelected = getSelectedColor(e);
if (!ColorDisabled[colorSelected])
SetSelected(
Enumerable.Range(
colorSelected & ~(NumberOfColorsToSelectAtATime - 1),
NumberOfColorsToSelectAtATime
).ToArray(),
CurrentlySelectingColors = !ColorsSelected[colorSelected]
);
}
public int[] GetSelectedIndices()
{
return ColorsSelected.Select((item, index) => new { Item = item, Index = index })
.Where(pair => pair.Item && !ColorDisabled[pair.Index]) //can't hurt to check ColorDisabled here too, probably
.Select(result => result.Index)
.ToArray();
}
public int NumberOfSelectedColors { get
{
return ColorsSelected.Count(item => item);
}
}
private void DoubleClicked(object sender, MouseEventArgs e)
{
int colorSelected = getSelectedColor(e);
if (ColorDisabled[colorSelected])
return;
if (!ColorsSelected[colorSelected])
Clicked(sender, e);
ColorDialog colorDialog1 = new ColorDialog();
colorDialog1.Color = Palette.Convert(Palette[colorSelected]);
colorDialog1.FullOpen = true;
DialogResult result = colorDialog1.ShowDialog();
if (result == DialogResult.OK)
{
var color = Palette.Convert(colorDialog1.Color);
var selections = GetSelectedIndices();
foreach (var selectedIndex in selections)
Palette[selectedIndex] = color;
if (selections.Length == 1) //only this one
ColorsSelected[colorSelected] = false;
Update(selections);
}
}
public bool[] ColorsSelected { get; }
public void Update(int[] colorsToUpdate)
{
var backColorAsArray = Palette.Convert(BackColor);
using (Graphics g = Graphics.FromImage(ImageImage))
{
using (Brush transparent = new SolidBrush(BackColor))
foreach (var colorToUpdate in colorsToUpdate) {
var color = !ColorDisabled[colorToUpdate] ? Palette[colorToUpdate] : backColorAsArray;
int left = (colorToUpdate % PaletteLengthOnEitherDimension) * ColorTotalSize;
int top = (colorToUpdate / PaletteLengthOnEitherDimension) * ColorTotalSize;
g.FillRectangle(ColorsSelected[colorToUpdate] ? Brushes.Black : transparent, new Rectangle(left, top, ColorTotalSize, ColorTotalSize)); //border
using (Brush brush = new SolidBrush(Color.FromArgb(color[0], color[1], color[2])))
g.FillRectangle(brush, new Rectangle(left + BorderSize, top + BorderSize, ColorSize, ColorSize)); //fill
}
Image = ImageImage;
}
}
public void SetSelected(int[] colorsToSelect, bool selecting)
{
foreach (var colorToSelect in colorsToSelect)
ColorsSelected[colorToSelect] = !ColorDisabled[colorToSelect] ? selecting : false;
Update(colorsToSelect);
}
}
}