-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathXSSFColor.cs
461 lines (425 loc) · 12.6 KB
/
XSSFColor.cs
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
/* ====================================================================
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.
==================================================================== */
using NPOI.OpenXmlFormats;
using System;
using NPOI.HSSF.Util;
using System.Text;
using NPOI.Util;
using NPOI.SS.UserModel;
using NPOI.OpenXmlFormats.Spreadsheet;
namespace NPOI.XSSF.UserModel
{
/**
* Represents a color in SpreadsheetML
*/
public class XSSFColor : ExtendedColor
{
private CT_Color ctColor;
/**
* Create an instance of XSSFColor from the supplied XML bean
*/
public XSSFColor(CT_Color color)
{
this.ctColor = color;
}
/**
* Create an new instance of XSSFColor
*/
public XSSFColor()
{
this.ctColor = new CT_Color();
}
public XSSFColor(System.Drawing.Color clr)
: this()
{
ctColor.SetRgb((byte)clr.R, (byte)clr.G, (byte)clr.B);
}
public XSSFColor(byte[] rgb)
: this()
{
ctColor.SetRgb(rgb);
}
public XSSFColor(IndexedColors indexedColor)
: this()
{
ctColor.indexed = (uint)indexedColor.Index;
}
/// <summary>
///A bool value indicating the ctColor is automatic and system ctColor dependent.
/// </summary>
public override bool IsAuto
{
get
{
return ctColor.auto;
}
set
{
ctColor.auto = value;
ctColor.autoSpecified = true;
}
}
public override bool IsIndexed
{
get
{
return ctColor.IsSetIndexed();
}
}
public override bool IsRGB
{
get
{
return ctColor.IsSetRgb();
}
}
public override bool IsThemed
{
get
{
return ctColor.IsSetTheme();
}
}
/**
* A bool value indicating if the ctColor has a alpha or not
*/
public bool HasAlpha
{
get
{
if (!ctColor.IsSetRgb()) return false;
return ctColor.rgb.Length == 4;
}
}
/**
* A bool value indicating if the ctColor has a tint or not
*/
public bool HasTint
{
get
{
if (!ctColor.IsSetTint())
{
return false;
}
return ctColor.tint != 0;
}
}
public override short Index
{
get
{
return ctColor.indexedSpecified ? (short)ctColor.indexed : (short)0;
}
}
/**
* Indexed ctColor value. Only used for backwards compatibility. References a ctColor in indexedColors.
*/
public short Indexed
{
get
{
return Index;
}
set
{
ctColor.indexed = (uint)value;
ctColor.indexedSpecified = true;
}
}
[Obsolete("use property RGB")]
public byte[] GetRgb()
{
return this.RGB;
}
protected override byte[] StoredRBG
{
get
{
return ctColor.rgb;
}
}
/**
* Standard Red Green Blue ctColor value (RGB).
* If there was an A (Alpha) value, it will be stripped.
*/
public override byte[] RGB
{
get
{
byte[] rgb = GetRGBOrARGB();
if (rgb == null) return null;
if (rgb.Length == 4)
{
// Need to trim off the alpha
byte[] tmp = new byte[3];
Array.Copy(rgb, 1, tmp, 0, 3);
return tmp;
}
else
{
return rgb;
}
}
set
{
ctColor.SetRgb((value));
}
}
/**
* Standard Alpha Red Green Blue ctColor value (ARGB).
*/
public override byte[] ARGB
{
get
{
byte[] rgb = GetRGBOrARGB();
if (rgb == null) return null;
if (rgb.Length == 3)
{
// Pad with the default Alpha
byte[] tmp = new byte[4];
unchecked
{
tmp[0] = (byte)-1;
}
Array.Copy(rgb, 0, tmp, 1, 3);
return tmp;
}
else
{
return rgb;
}
}
}
/**
* Standard Alpha Red Green Blue ctColor value (ARGB).
*/
[Obsolete("use property ARGB")]
public byte[] GetARgb()
{
return ARGB;
}
/**
* Standard Red Green Blue ctColor value (RGB) with applied tint.
* Alpha values are ignored.
*/
public byte[] GetRgbWithTint()
{
byte[] rgb = ctColor.GetRgb();
if (rgb != null)
{
if (rgb.Length == 4)
{
byte[] tmp = new byte[3];
Array.Copy(rgb, 1, tmp, 0, 3);
rgb = tmp;
}
for (int i = 0; i < rgb.Length; i++)
{
rgb[i] = ApplyTint(rgb[i] & 0xFF, ctColor.tint);
}
}
return rgb;
}
private static byte ApplyTint(int lum, double tint)
{
if (tint > 0)
{
return (byte)(lum * (1.0 - tint) + (255 - 255 * (1.0 - tint)));
}
else if (tint < 0)
{
return (byte)(lum * (1 + tint));
}
else
{
return (byte)lum;
}
}
/**
* Standard Alpha Red Green Blue ctColor value (ARGB).
*/
public void SetRgb(byte[] rgb)
{
// Correct it and save
ctColor.SetRgb((rgb));
}
/**
* Index into the clrScheme collection, referencing a particular sysClr or
* srgbClr value expressed in the Theme part.
*/
public override int Theme
{
get
{
return ctColor.themeSpecified ? (int)ctColor.theme : (int)0;
}
set
{
ctColor.theme = (uint)value;
}
}
/**
* Specifies the tint value applied to the ctColor.
*
* <p>
* If tint is supplied, then it is applied to the RGB value of the ctColor to determine the final
* ctColor applied.
* </p>
* <p>
* The tint value is stored as a double from -1.0 .. 1.0, where -1.0 means 100% darken and
* 1.0 means 100% lighten. Also, 0.0 means no Change.
* </p>
* <p>
* In loading the RGB value, it is Converted to HLS where HLS values are (0..HLSMAX), where
* HLSMAX is currently 255.
* </p>
* Here are some examples of how to apply tint to ctColor:
* <blockquote>
* <pre>
* If (tint < 0)
* Lum' = Lum * (1.0 + tint)
*
* For example: Lum = 200; tint = -0.5; Darken 50%
* Lum' = 200 * (0.5) => 100
* For example: Lum = 200; tint = -1.0; Darken 100% (make black)
* Lum' = 200 * (1.0-1.0) => 0
* If (tint > 0)
* Lum' = Lum * (1.0-tint) + (HLSMAX - HLSMAX * (1.0-tint))
* For example: Lum = 100; tint = 0.75; Lighten 75%
*
* Lum' = 100 * (1-.75) + (HLSMAX - HLSMAX*(1-.75))
* = 100 * .25 + (255 - 255 * .25)
* = 25 + (255 - 63) = 25 + 192 = 217
* For example: Lum = 100; tint = 1.0; Lighten 100% (make white)
* Lum' = 100 * (1-1) + (HLSMAX - HLSMAX*(1-1))
* = 100 * 0 + (255 - 255 * 0)
* = 0 + (255 - 0) = 255
* </pre>
* </blockquote>
*
* @return the tint value
*/
public override double Tint
{
get
{
return ctColor.tint;
}
set
{
ctColor.tint = value;
ctColor.tintSpecified = true;
}
}
/**
* Returns the underlying XML bean
*
* @return the underlying XML bean
*/
internal CT_Color GetCTColor()
{
return ctColor;
}
/// <summary>
/// Checked type cast <tt>color</tt> to an XSSFColor.
/// </summary>
/// <param name="color">the color to type cast</param>
/// <returns>the type casted color</returns>
/// <exception cref="ArgumentException">if color is null or is not an instance of XSSFColor</exception>
public static XSSFColor ToXSSFColor(IColor color)
{
// FIXME: this method would be more useful if it could convert any Color to an XSSFColor
// Currently the only benefit of this method is to throw an IllegalArgumentException
// instead of a ClassCastException.
if (color != null && !(color is XSSFColor)) {
throw new ArgumentException("Only XSSFColor objects are supported");
}
return (XSSFColor)color;
}
// Helper methods for {@link #equals(Object)}
private bool SameIndexed(XSSFColor other)
{
if (IsIndexed == other.IsIndexed)
{
if (IsIndexed)
{
return Indexed == other.Indexed;
}
return true;
}
return false;
}
private bool SameARGB(XSSFColor other)
{
if (IsRGB == other.IsRGB)
{
if (IsRGB)
{
return Arrays.Equals(ARGB, other.ARGB);
}
return true;
}
return false;
}
private bool SameTheme(XSSFColor other)
{
if (IsThemed == other.IsThemed)
{
if (IsThemed)
{
return Theme == other.Theme;
}
return true;
}
return false;
}
private bool SameTint(XSSFColor other)
{
if (HasTint == other.HasTint)
{
if (HasTint)
{
return Tint == other.Tint;
}
return true;
}
return false;
}
private bool SameAuto(XSSFColor other)
{
return IsAuto == other.IsAuto;
}
public override int GetHashCode()
{
return ctColor.ToString().GetHashCode();
}
public override bool Equals(Object o)
{
if (o == null || !(o is XSSFColor))
return false;
XSSFColor other = (XSSFColor)o;
// Compare each field in ctColor.
// Cannot compare ctColor's XML string representation because equivalent
// colors may have different relation namespace URI's
return SameARGB(other)
&& SameTheme(other)
&& SameIndexed(other)
&& SameTint(other)
&& SameAuto(other);
}
}
}