zhao
2021-07-02 081df17b8cc4a6e7e4f4e1e1887f24810e3ec2f9
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
 
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) Under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for Additional information regarding copyright ownership.
   The ASF licenses this file to You Under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed Under the License is distributed on an "AS Is" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations Under the License.
==================================================================== */
 
 
namespace HH.WMS.Utils.NPOI.HSSF.Record
{
 
    using HH.WMS.Utils.NPOI.Util;
    using System;
    using System.Text;
    using System.IO;
    using System.Collections.Generic;
 
    /**
     * PaletteRecord - Supports custom palettes.
     * @author Andrew C. Oliver (acoliver at apache dot org)
     * @author Brian Sanders (bsanders at risklabs dot com) - custom palette editing
     * @version 2.0-pre
     */
 
    public class PaletteRecord
       : StandardRecord
    {
        public const short sid = 0x92;
        /** The standard size of an XLS palette */
        public static byte STANDARD_PALETTE_SIZE = (byte)56;
        /** The byte index of the first color */
        public static short FIRST_COLOR_INDEX = (short)0x8;
 
        
        private List<PColor> field_2_colors;
 
        public PaletteRecord()
        {
            PColor[] defaultPalette = CreateDefaultPalette();
            field_2_colors = new List<PColor>(defaultPalette.Length);
            for (int i = 0; i < defaultPalette.Length; i++)
            {
                field_2_colors.Add(defaultPalette[i]);
            }
        }
 
        /**
         * Constructs a PaletteRecord record and Sets its fields appropriately.
         * @param in the RecordInputstream to Read the record from
         */
 
        public PaletteRecord(RecordInputStream in1)
        {
            short field_1_numcolors = in1.ReadShort();
            field_2_colors = new List<PColor>(field_1_numcolors);
            for (int k = 0; k < field_1_numcolors; k++)
            {
                field_2_colors.Add(new PColor(in1));
            }
        }
 
        public override String ToString()
        {
            StringBuilder buffer = new StringBuilder();
 
            buffer.Append("[PALETTE]\n");
            buffer.Append("  numcolors     = ").Append(field_2_colors.Count)
                  .Append('\n');
            for (int k = 0; k < field_2_colors.Count; k++)
            {
                PColor c = (PColor)field_2_colors[k];
                buffer.Append("* colornum      = ").Append(k)
                      .Append('\n');
                buffer.Append(c.ToString());
                buffer.Append("/*colornum      = ").Append(k)
                      .Append('\n');
            }
            buffer.Append("[/PALETTE]\n");
            return buffer.ToString();
        }
 
        public override void Serialize(ILittleEndianOutput out1)
        {
            out1.WriteShort(field_2_colors.Count);
            for (int i = 0; i < field_2_colors.Count; i++)
            {
                field_2_colors[i].Serialize(out1);
            }
        }
 
        protected override int DataSize
        {
            get
            {
                return 2 + field_2_colors.Count * PColor.ENCODED_SIZE;
            }
        }
 
        public override short Sid
        {
            get { return sid; }
        }
 
        /**
         * Returns the color value at a given index
         *
         * @return the RGB triplet for the color, or null if the specified index
         * does not exist
         */
        public byte[] GetColor(short byteIndex)
        {
            int i = byteIndex - FIRST_COLOR_INDEX;
            if (i < 0 || i >= field_2_colors.Count)
            {
                return null;
            }
            PColor color = (PColor)field_2_colors[i];
            return new byte[] { color._red, color._green, color._blue };
        }
 
        /**
         * Sets the color value at a given index
         *
         * If the given index Is greater than the current last color index,
         * then black Is Inserted at every index required to make the palette continuous.
         *
         * @param byteIndex the index to Set; if this index Is less than 0x8 or greater than
         * 0x40, then no modification Is made
         */
        public void SetColor(short byteIndex, byte red, byte green, byte blue)
        {
            int i = byteIndex - FIRST_COLOR_INDEX;
            if (i < 0 || i >= STANDARD_PALETTE_SIZE)
            {
                return;
            }
            while (field_2_colors.Count <= i)
            {
                field_2_colors.Add(new PColor((byte)0, (byte)0, (byte)0));
            }
            PColor custColor = new PColor(red, green, blue);
            field_2_colors[i] = custColor;
        }
 
        /**
         * Creates the default palette as PaletteRecord binary data
         *
         * @see org.apache.poi.hssf.model.Workbook#createPalette
         */
        private static PColor[] CreateDefaultPalette()
        {
            return new PColor[] {
                pc(0, 0, 0),
                pc(255, 255, 255),
                pc(255, 0, 0),
                pc(0, 255, 0),
                pc(0, 0, 255),
                pc(255, 255, 0),
                pc(255, 0, 255),
                pc(0, 255, 255),
                pc(128, 0, 0),
                pc(0, 128, 0),
                pc(0, 0, 128),
                pc(128, 128, 0),
                pc(128, 0, 128),
                pc(0, 128, 128),
                pc(192, 192, 192),
                pc(128, 128, 128),
                pc(153, 153, 255),
                pc(153, 51, 102),
                pc(255, 255, 204),
                pc(204, 255, 255),
                pc(102, 0, 102),
                pc(255, 128, 128),
                pc(0, 102, 204),
                pc(204, 204, 255),
                pc(0, 0, 128),
                pc(255, 0, 255),
                pc(255, 255, 0),
                pc(0, 255, 255),
                pc(128, 0, 128),
                pc(128, 0, 0),
                pc(0, 128, 128),
                pc(0, 0, 255),
                pc(0, 204, 255),
                pc(204, 255, 255),
                pc(204, 255, 204),
                pc(255, 255, 153),
                pc(153, 204, 255),
                pc(255, 153, 204),
                pc(204, 153, 255),
                pc(255, 204, 153),
                pc(51, 102, 255),
                pc(51, 204, 204),
                pc(153, 204, 0),
                pc(255, 204, 0),
                pc(255, 153, 0),
                pc(255, 102, 0),
                pc(102, 102, 153),
                pc(150, 150, 150),
                pc(0, 51, 102),
                pc(51, 153, 102),
                pc(0, 51, 0),
                pc(51, 51, 0),
                pc(153, 51, 0),
                pc(153, 51, 102),
                pc(51, 51, 153),
                pc(51, 51, 51),
            };
        }
 
        private static PColor pc(int r, int g, int b)
        {
            return new PColor(r, g, b);
        }
    }
 
    /**
     * PColor - element in the list of colors - consider it a "struct"
     */
    class PColor
    {
        public static short ENCODED_SIZE = 4;
        public byte _red;
        public byte _green;
        public byte _blue;
        public PColor(int red, int green, int blue)
        {
            this._red = (byte)red;
            this._green = (byte)green;
            this._blue = (byte)blue;
        }
 
          public PColor(RecordInputStream in1) {
            _red = (byte)in1.ReadByte();
            _green = (byte)in1.ReadByte();
            _blue = (byte)in1.ReadByte();
            in1.ReadByte(); // unused
          }
 
        public void Serialize(ILittleEndianOutput out1) {
            out1.WriteByte(_red);
            out1.WriteByte(_green);
            out1.WriteByte(_blue);
            out1.WriteByte(0);
        }
 
        public override String ToString()
        {
            StringBuilder buffer = new StringBuilder();
            buffer.Append("  red           = ").Append(_red & 0xff).Append('\n');
            buffer.Append("  green         = ").Append(_green & 0xff).Append('\n');
            buffer.Append("  blue          = ").Append(_blue & 0xff).Append('\n');
            return buffer.ToString();
        }
    }
}