1 | /*******************************************************************************
|
---|
2 | * You may amend and distribute as you like, but don't remove this header!
|
---|
3 | *
|
---|
4 | * EPPlus provides server-side generation of Excel 2007/2010 spreadsheets.
|
---|
5 | * See http://www.codeplex.com/EPPlus for details.
|
---|
6 | *
|
---|
7 | * Copyright (C) 2011 Jan Källman
|
---|
8 | *
|
---|
9 | * This library is free software; you can redistribute it and/or
|
---|
10 | * modify it under the terms of the GNU Lesser General Public
|
---|
11 | * License as published by the Free Software Foundation; either
|
---|
12 | * version 2.1 of the License, or (at your option) any later version.
|
---|
13 |
|
---|
14 | * This library is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | * See the GNU Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * The GNU Lesser General Public License can be viewed at http://www.opensource.org/licenses/lgpl-license.php
|
---|
20 | * If you unfamiliar with this license or have questions about it, here is an http://www.gnu.org/licenses/gpl-faq.html
|
---|
21 | *
|
---|
22 | * All code and executables are provided "as is" with no warranty either express or implied.
|
---|
23 | * The author accepts no liability for any damage or loss of business that this product may cause.
|
---|
24 | *
|
---|
25 | * Code change notes:
|
---|
26 | *
|
---|
27 | * Author Change Date
|
---|
28 | * ******************************************************************************
|
---|
29 | * Jan Källman Initial Release 2009-10-01
|
---|
30 | * Jan Källman License changed GPL-->LGPL 2011-12-16
|
---|
31 | *******************************************************************************/
|
---|
32 | using System;
|
---|
33 | using System.Collections.Generic;
|
---|
34 | using System.Text;
|
---|
35 | using OfficeOpenXml.Style.XmlAccess;
|
---|
36 | using System.Drawing;
|
---|
37 |
|
---|
38 | namespace OfficeOpenXml.Style
|
---|
39 | {
|
---|
40 | /// <summary>
|
---|
41 | /// Color for cellstyling
|
---|
42 | /// </summary>
|
---|
43 | public sealed class ExcelColor : StyleBase
|
---|
44 | {
|
---|
45 | eStyleClass _cls;
|
---|
46 | StyleBase _parent;
|
---|
47 | internal ExcelColor(ExcelStyles styles, OfficeOpenXml.XmlHelper.ChangedEventHandler ChangedEvent, int worksheetID, string address, eStyleClass cls, StyleBase parent) :
|
---|
48 | base(styles, ChangedEvent, worksheetID, address)
|
---|
49 |
|
---|
50 | {
|
---|
51 | _parent = parent;
|
---|
52 | _cls = cls;
|
---|
53 | }
|
---|
54 | /// <summary>
|
---|
55 | /// The theme color
|
---|
56 | /// </summary>
|
---|
57 | public string Theme
|
---|
58 | {
|
---|
59 | get
|
---|
60 | {
|
---|
61 | return GetSource().Theme;
|
---|
62 | }
|
---|
63 | }
|
---|
64 | /// <summary>
|
---|
65 | /// The tint value
|
---|
66 | /// </summary>
|
---|
67 | public decimal Tint
|
---|
68 | {
|
---|
69 | get
|
---|
70 | {
|
---|
71 | return GetSource().Tint;
|
---|
72 | }
|
---|
73 | set
|
---|
74 | {
|
---|
75 | if (value > 1 || value < -1)
|
---|
76 | {
|
---|
77 | throw (new ArgumentOutOfRangeException("Value must be between -1 and 1"));
|
---|
78 | }
|
---|
79 | _ChangedEvent(this, new StyleChangeEventArgs(_cls, eStyleProperty.Tint, value, _positionID, _address));
|
---|
80 | }
|
---|
81 | }
|
---|
82 | /// <summary>
|
---|
83 | /// The RGB value
|
---|
84 | /// </summary>
|
---|
85 | public string Rgb
|
---|
86 | {
|
---|
87 | get
|
---|
88 | {
|
---|
89 | return GetSource().Rgb;
|
---|
90 | }
|
---|
91 | internal set
|
---|
92 | {
|
---|
93 | _ChangedEvent(this, new StyleChangeEventArgs(_cls, eStyleProperty.Color, value, _positionID, _address));
|
---|
94 | }
|
---|
95 | }
|
---|
96 | /// <summary>
|
---|
97 | /// The indexed color number.
|
---|
98 | /// </summary>
|
---|
99 | public int Indexed
|
---|
100 | {
|
---|
101 | get
|
---|
102 | {
|
---|
103 | return GetSource().Indexed;
|
---|
104 | }
|
---|
105 | set
|
---|
106 | {
|
---|
107 | _ChangedEvent(this, new StyleChangeEventArgs(_cls, eStyleProperty.IndexedColor, value, _positionID, _address));
|
---|
108 | }
|
---|
109 | }
|
---|
110 | /// <summary>
|
---|
111 | /// Set the color of the object
|
---|
112 | /// </summary>
|
---|
113 | /// <param name="color">The color</param>
|
---|
114 | public void SetColor(Color color)
|
---|
115 | {
|
---|
116 | Rgb = color.ToArgb().ToString("X");
|
---|
117 | }
|
---|
118 |
|
---|
119 |
|
---|
120 | internal override string Id
|
---|
121 | {
|
---|
122 | get
|
---|
123 | {
|
---|
124 | return Theme + Tint + Rgb + Indexed;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | private ExcelColorXml GetSource()
|
---|
128 | {
|
---|
129 | Index = _parent.Index < 0 ? 0 : _parent.Index;
|
---|
130 | switch(_cls)
|
---|
131 | {
|
---|
132 | case eStyleClass.FillBackgroundColor:
|
---|
133 | return _styles.Fills[Index].BackgroundColor;
|
---|
134 | case eStyleClass.FillPatternColor:
|
---|
135 | return _styles.Fills[Index].PatternColor;
|
---|
136 | case eStyleClass.Font:
|
---|
137 | return _styles.Fonts[Index].Color;
|
---|
138 | case eStyleClass.BorderLeft:
|
---|
139 | return _styles.Borders[Index].Left.Color;
|
---|
140 | case eStyleClass.BorderTop:
|
---|
141 | return _styles.Borders[Index].Top.Color;
|
---|
142 | case eStyleClass.BorderRight:
|
---|
143 | return _styles.Borders[Index].Right.Color;
|
---|
144 | case eStyleClass.BorderBottom:
|
---|
145 | return _styles.Borders[Index].Bottom.Color;
|
---|
146 | case eStyleClass.BorderDiagonal:
|
---|
147 | return _styles.Borders[Index].Diagonal.Color;
|
---|
148 | default:
|
---|
149 | throw(new Exception("Invalid style-class for Color"));
|
---|
150 | }
|
---|
151 | }
|
---|
152 | internal override void SetIndex(int index)
|
---|
153 | {
|
---|
154 | _parent.Index = index;
|
---|
155 | }
|
---|
156 | /// <summary>
|
---|
157 | /// Return the RGB value for the color object that uses the Indexed or Tint property
|
---|
158 | /// </summary>
|
---|
159 | /// <param name="theColor">The color object</param>
|
---|
160 | /// <returns>The RGB color starting with a #</returns>
|
---|
161 | public string LookupColor(ExcelColor theColor)
|
---|
162 | {
|
---|
163 | //Thanks to neaves for contributing this method.
|
---|
164 | int iTint = 0;
|
---|
165 | string translatedRGB = "";
|
---|
166 |
|
---|
167 | // reference extracted from ECMA-376, Part 4, Section 3.8.26
|
---|
168 | string[] rgbLookup =
|
---|
169 | {
|
---|
170 | "#FF000000", // 0
|
---|
171 | "#FFFFFFFF",
|
---|
172 | "#FFFF0000",
|
---|
173 | "#FF00FF00",
|
---|
174 | "#FF0000FF",
|
---|
175 | "#FFFFFF00",
|
---|
176 | "#FFFF00FF",
|
---|
177 | "#FF00FFFF",
|
---|
178 | "#FF000000", // 8
|
---|
179 | "#FFFFFFFF",
|
---|
180 | "#FFFF0000",
|
---|
181 | "#FF00FF00",
|
---|
182 | "#FF0000FF",
|
---|
183 | "#FFFFFF00",
|
---|
184 | "#FFFF00FF",
|
---|
185 | "#FF00FFFF",
|
---|
186 | "#FF800000",
|
---|
187 | "#FF008000",
|
---|
188 | "#FF000080",
|
---|
189 | "#FF808000",
|
---|
190 | "#FF800080",
|
---|
191 | "#FF008080",
|
---|
192 | "#FFC0C0C0",
|
---|
193 | "#FF808080",
|
---|
194 | "#FF9999FF",
|
---|
195 | "#FF993366",
|
---|
196 | "#FFFFFFCC",
|
---|
197 | "#FFCCFFFF",
|
---|
198 | "#FF660066",
|
---|
199 | "#FFFF8080",
|
---|
200 | "#FF0066CC",
|
---|
201 | "#FFCCCCFF",
|
---|
202 | "#FF000080",
|
---|
203 | "#FFFF00FF",
|
---|
204 | "#FFFFFF00",
|
---|
205 | "#FF00FFFF",
|
---|
206 | "#FF800080",
|
---|
207 | "#FF800000",
|
---|
208 | "#FF008080",
|
---|
209 | "#FF0000FF",
|
---|
210 | "#FF00CCFF",
|
---|
211 | "#FFCCFFFF",
|
---|
212 | "#FFCCFFCC",
|
---|
213 | "#FFFFFF99",
|
---|
214 | "#FF99CCFF",
|
---|
215 | "#FFFF99CC",
|
---|
216 | "#FFCC99FF",
|
---|
217 | "#FFFFCC99",
|
---|
218 | "#FF3366FF",
|
---|
219 | "#FF33CCCC",
|
---|
220 | "#FF99CC00",
|
---|
221 | "#FFFFCC00",
|
---|
222 | "#FFFF9900",
|
---|
223 | "#FFFF6600",
|
---|
224 | "#FF666699",
|
---|
225 | "#FF969696",
|
---|
226 | "#FF003366",
|
---|
227 | "#FF339966",
|
---|
228 | "#FF003300",
|
---|
229 | "#FF333300",
|
---|
230 | "#FF993300",
|
---|
231 | "#FF993366",
|
---|
232 | "#FF333399",
|
---|
233 | "#FF333333", // 63
|
---|
234 | };
|
---|
235 |
|
---|
236 | if ((0 <= theColor.Indexed) && (rgbLookup.Length > theColor.Indexed))
|
---|
237 | {
|
---|
238 | // coloring by pre-set color codes
|
---|
239 | translatedRGB = rgbLookup[theColor.Indexed];
|
---|
240 | }
|
---|
241 | else if (null != theColor.Rgb && 0 < theColor.Rgb.Length)
|
---|
242 | {
|
---|
243 | // coloring by RGB value ("FFRRGGBB")
|
---|
244 | translatedRGB = "#" + theColor.Rgb;
|
---|
245 | }
|
---|
246 | else
|
---|
247 | {
|
---|
248 | // coloring by shades of grey (-1 -> 0)
|
---|
249 | iTint = ((int)(theColor.Tint * 160) + 0x80);
|
---|
250 | translatedRGB = ((int)(decimal.Round(theColor.Tint * -512))).ToString("X");
|
---|
251 | translatedRGB = "#FF" + translatedRGB + translatedRGB + translatedRGB;
|
---|
252 | }
|
---|
253 |
|
---|
254 | return translatedRGB;
|
---|
255 | }
|
---|
256 | }
|
---|
257 | }
|
---|