-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPalette.cs
More file actions
161 lines (123 loc) · 3.49 KB
/
Palette.cs
File metadata and controls
161 lines (123 loc) · 3.49 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
using GR.IO;
using GR.Memory;
using System;
namespace RetroDevStudio
{
public class Palette
{
public string Name = "";
private int NumColorsUsed = 16;
public System.Drawing.Color[] Colors = new System.Drawing.Color[256];
public System.Drawing.Brush[] ColorBrushes = new System.Drawing.Brush[256];
public uint[] ColorValues = new uint[256];
public Palette()
{
}
public Palette( int NumColors )
{
if ( NumColors < 16 )
{
Debug.Log( "< 16 colors" );
NumColors = 16;
}
else if ( ( NumColors > 16 )
&& ( NumColors < 256 ) )
{
Debug.Log( "< 256 colors" );
NumColors = 256;
}
if ( NumColors == 257 )
{
NumColors = 256;
}
if ( NumColors == 17 )
{
NumColors = 16;
}
NumColorsUsed = NumColors;
}
public Palette( Palette OtherPal )
{
Name = OtherPal.Name;
NumColorsUsed = OtherPal.NumColorsUsed;
for ( int i = 0; i < NumColorsUsed; ++i )
{
ColorValues[i] = OtherPal.ColorValues[i];
}
CreateBrushes();
}
public int NumColors
{
get
{
return NumColorsUsed;
}
}
internal void CreateBrushes()
{
for ( int i = 0; i < NumColorsUsed; ++i )
{
Colors[i] = GR.Color.Helper.FromARGB( ColorValues[i] );
ColorBrushes[i] = new System.Drawing.SolidBrush( Colors[i] );
}
}
internal ByteBuffer GetExportData( int StartIndex, int CountColors, bool Swizzled )
{
if ( ( StartIndex < 0 )
|| ( StartIndex + CountColors > NumColors ) )
{
Debug.Log( "Palette.GetExportData, trying to access out of bounds data" );
return new ByteBuffer();
}
var result = new ByteBuffer();
result.Reserve( CountColors * 3 );
for ( int i = 0; i < CountColors; ++i )
{
result.AppendU8( (byte)( ( ColorValues[i] & 0x00ff0000 ) >> 16 ) );
}
for ( int i = 0; i < CountColors; ++i )
{
result.AppendU8( (byte)( ( ColorValues[i] & 0x0000ff00 ) >> 8 ) );
}
for ( int i = 0; i < CountColors; ++i )
{
result.AppendU8( (byte)( ( ColorValues[i] & 0x000000ff ) ) );
}
if ( Swizzled )
{
for ( int i = 0; i < result.Length; ++i )
{
result.SetU8At( i, SwizzleByte( result.ByteAt( i ) ) );
}
}
return result;
}
private byte SwizzleByte( byte Value )
{
return (byte)( ( Value >> 4 ) | ( Value << 4 ) );
}
public static Palette Read( IReader Reader )
{
int numColors = Reader.ReadInt32();
var pal = new Palette( numColors );
for ( int i = 0; i < pal.NumColors; ++i )
{
pal.ColorValues[i] = Reader.ReadUInt32();
}
pal.CreateBrushes();
pal.Name = Reader.ReadString();
return pal;
}
public ByteBuffer ToBuffer()
{
var chunkPalette = new GR.IO.FileChunk( RetroDevStudio.FileChunkConstants.PALETTE );
chunkPalette.AppendI32( NumColors );
for ( int i = 0; i < NumColors; ++i )
{
chunkPalette.AppendU32( ColorValues[i] );
}
chunkPalette.AppendString( Name );
return chunkPalette.ToBuffer();
}
}
}