zhao
2021-06-11 98186752629a7bd38965418af84db382d90b9c07
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
/* ====================================================================
   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.UserModel
{
    using System;
    using System.Text;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.HSSF.Util;
    using HH.WMS.Utils.NPOI.Util;
 
 
    /// <summary>
    /// Represents a workbook color palette.
    /// Internally, the XLS format refers to colors using an offset into the palette
    /// record.  Thus, the first color in the palette has the index 0x8, the second
    /// has the index 0x9, etc. through 0x40
    /// @author Brian Sanders (bsanders at risklabs dot com)
    /// </summary>
    public class HSSFPalette
    {
        private PaletteRecord palette;
 
        public HSSFPalette(PaletteRecord palette)
        {
            this.palette = palette;
        }
 
        /// <summary>
        /// Retrieves the color at a given index
        /// </summary>
        /// <param name="index">the palette index, between 0x8 to 0x40 inclusive.</param>
        /// <returns>the color, or null if the index Is not populated</returns>
        public HSSFColor GetColor(short index)
        {
            //Handle the special AUTOMATIC case
            if (index == HSSFColor.AUTOMATIC.index)
                return HSSFColor.AUTOMATIC.GetInstance();
            else
            {
                byte[] b = palette.GetColor(index);
                if (b != null)
                {
                    return new CustomColor(index, b);
                }
            }
            return null;
        }
 
        /// <summary>
        /// Finds the first occurance of a given color
        /// </summary>
        /// <param name="red">the RGB red component, between 0 and 255 inclusive</param>
        /// <param name="green">the RGB green component, between 0 and 255 inclusive</param>
        /// <param name="blue">the RGB blue component, between 0 and 255 inclusive</param>
        /// <returns>the color, or null if the color does not exist in this palette</returns>
        public HSSFColor FindColor(byte red, byte green, byte blue)
        {
            byte[] b = palette.GetColor(PaletteRecord.FIRST_COLOR_INDEX);
            for (short i = (short)PaletteRecord.FIRST_COLOR_INDEX; b != null;
                b = palette.GetColor(++i))
            {
                if (b[0] == red && b[1] == green && b[2] == blue)
                {
                    return new CustomColor(i, b);
                }
            }
            return null;
        }
 
        /// <summary>
        /// Finds the closest matching color in the custom palette.  The
        /// method for Finding the distance between the colors Is fairly
        /// primative.
        /// </summary>
        /// <param name="red">The red component of the color to match.</param>
        /// <param name="green">The green component of the color to match.</param>
        /// <param name="blue">The blue component of the color to match.</param>
        /// <returns>The closest color or null if there are no custom
        /// colors currently defined.</returns>
        public HSSFColor FindSimilarColor(byte red, byte green, byte blue)
        {
            HSSFColor result = null;
            int minColorDistance = int.MaxValue;
            byte[] b = palette.GetColor(PaletteRecord.FIRST_COLOR_INDEX);
            for (short i = (short)PaletteRecord.FIRST_COLOR_INDEX; b != null;
                b = palette.GetColor(++i))
            {
                int colorDistance = Math.Abs(red - b[0]) +
                    Math.Abs(green - b[1]) + Math.Abs(blue - b[2]);
                if (colorDistance < minColorDistance)
                {
                    minColorDistance = colorDistance;
                    result = GetColor(i);
                }
            }
            return result;
        }
 
        /// <summary>
        /// Sets the color at the given offset
        /// </summary>
        /// <param name="index">the palette index, between 0x8 to 0x40 inclusive</param>
        /// <param name="red">the RGB red component, between 0 and 255 inclusive</param>
        /// <param name="green">the RGB green component, between 0 and 255 inclusive</param>
        /// <param name="blue">the RGB blue component, between 0 and 255 inclusive</param>
        public void SetColorAtIndex(short index, byte red, byte green, byte blue)
        {
            palette.SetColor(index, red, green, blue);
        }
 
        /// <summary>
        /// Adds a new color into an empty color slot.
        /// </summary>
        /// <param name="red">The red component</param>
        /// <param name="green">The green component</param>
        /// <param name="blue">The blue component</param>
        /// <returns>The new custom color.</returns>
        public HSSFColor AddColor(byte red, byte green, byte blue)
        {
            byte[] b = palette.GetColor(PaletteRecord.FIRST_COLOR_INDEX);
            short i;
            for (i = (short)PaletteRecord.FIRST_COLOR_INDEX; i < PaletteRecord.STANDARD_PALETTE_SIZE + PaletteRecord.FIRST_COLOR_INDEX; b = palette.GetColor(++i))
            {
                if (b == null)
                {
                    SetColorAtIndex(i, red, green, blue);
                    return GetColor(i);
                }
            }
            throw new Exception("Could not Find free color index");
        }
 
        /// <summary>
        /// user custom color
        /// </summary>
        private class CustomColor : HSSFColor
        {
            private short byteOffset;
            private byte red;
            private byte green;
            private byte blue;
 
            /// <summary>
            /// Initializes a new instance of the <see cref="CustomColor"/> class.
            /// </summary>
            /// <param name="byteOffset">The byte offset.</param>
            /// <param name="colors">The colors.</param>
            public CustomColor(short byteOffset, byte[] colors): this(byteOffset, colors[0], colors[1], colors[2])
            {
               
            }
 
            /// <summary>
            /// Initializes a new instance of the <see cref="CustomColor"/> class.
            /// </summary>
            /// <param name="byteOffset">The byte offset.</param>
            /// <param name="red">The red.</param>
            /// <param name="green">The green.</param>
            /// <param name="blue">The blue.</param>
            public CustomColor(short byteOffset, byte red, byte green, byte blue)
            {
                this.byteOffset = byteOffset;
                this.red = red;
                this.green = green;
                this.blue = blue;
            }
 
            /// <summary>
            /// Gets index to the standard palette
            /// </summary>
            /// <value></value>
            public override short GetIndex()
            {
                return byteOffset;
            }
 
            /// <summary>
            /// Gets triplet representation like that in Excel
            /// </summary>
            /// <value></value>
            public override short[] GetTriplet()
            {
                    return new short[]
                    {
                        (short) (red   & 0xff),
                        (short) (green & 0xff),
                        (short) (blue  & 0xff)
                    };
            }
 
            /// <summary>
            /// Gets a hex string exactly like a gnumeric triplet
            /// </summary>
            /// <value></value>
            public override String GetHexString()
            {
                    StringBuilder sb = new StringBuilder();
                    sb.Append(GetGnumericPart(red));
                    sb.Append(':');
                    sb.Append(GetGnumericPart(green));
                    sb.Append(':');
                    sb.Append(GetGnumericPart(blue));
                    return sb.ToString();
            }
 
            /// <summary>
            /// Gets the gnumeric part.
            /// </summary>
            /// <param name="color">The color.</param>
            /// <returns></returns>
            private String GetGnumericPart(byte color)
            {
                String s;
                if (color == 0)
                {
                    s = "0";
                }
                else
                {
                    int c = color & 0xff; //as Unsigned
                    c = (c << 8) | c; //pad to 16-bit
                    s = StringUtil.ToHexString(c).ToUpper();
                    while (s.Length < 4)
                    {
                        s = "0" + s;
                    }
                }
                return s;
            }
        }
    }
}