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 System.Xml;
|
---|
36 | using System.Globalization;
|
---|
37 | namespace OfficeOpenXml.Style.XmlAccess
|
---|
38 | {
|
---|
39 | /// <summary>
|
---|
40 | /// Xml access class for color
|
---|
41 | /// </summary>
|
---|
42 | public sealed class ExcelColorXml : StyleXmlHelper
|
---|
43 | {
|
---|
44 | internal ExcelColorXml(XmlNamespaceManager nameSpaceManager)
|
---|
45 | : base(nameSpaceManager)
|
---|
46 | {
|
---|
47 | _auto = false;
|
---|
48 | _theme = "";
|
---|
49 | _tint = 0;
|
---|
50 | _rgb = "";
|
---|
51 | _indexed = int.MinValue;
|
---|
52 | }
|
---|
53 | internal ExcelColorXml(XmlNamespaceManager nsm, XmlNode topNode) :
|
---|
54 | base(nsm, topNode)
|
---|
55 | {
|
---|
56 | if(topNode==null)
|
---|
57 | {
|
---|
58 | _exists=false;
|
---|
59 | }
|
---|
60 | else
|
---|
61 | {
|
---|
62 | _exists = true;
|
---|
63 | _auto = GetXmlNodeBool("@auto");
|
---|
64 | _theme = GetXmlNodeString("@theme");
|
---|
65 | _tint = GetXmlNodeDecimal("@tint");
|
---|
66 | _rgb = GetXmlNodeString("@rgb");
|
---|
67 | _indexed = GetXmlNodeInt("@indexed");
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | internal override string Id
|
---|
72 | {
|
---|
73 | get
|
---|
74 | {
|
---|
75 | return _auto.ToString() + "|" + _theme + "|" + _tint + "|" + _rgb + "|" + _indexed;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | bool _auto;
|
---|
79 | public bool Auto
|
---|
80 | {
|
---|
81 | get
|
---|
82 | {
|
---|
83 | return _auto;
|
---|
84 | }
|
---|
85 | set
|
---|
86 | {
|
---|
87 | if (value)
|
---|
88 | {
|
---|
89 |
|
---|
90 | }
|
---|
91 | _auto = value;
|
---|
92 | _exists = true;
|
---|
93 | Clear();
|
---|
94 | }
|
---|
95 | }
|
---|
96 | string _theme;
|
---|
97 | /// <summary>
|
---|
98 | /// Theme color value
|
---|
99 | /// </summary>
|
---|
100 | public string Theme
|
---|
101 | {
|
---|
102 | get
|
---|
103 | {
|
---|
104 | return _theme;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | decimal _tint;
|
---|
108 | /// <summary>
|
---|
109 | /// Tint
|
---|
110 | /// </summary>
|
---|
111 | public decimal Tint
|
---|
112 | {
|
---|
113 | get
|
---|
114 | {
|
---|
115 | return _tint;
|
---|
116 | }
|
---|
117 | set
|
---|
118 | {
|
---|
119 | _tint = value;
|
---|
120 | _exists = true;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | string _rgb;
|
---|
124 | /// <summary>
|
---|
125 | /// RGB value
|
---|
126 | /// </summary>
|
---|
127 | public string Rgb
|
---|
128 | {
|
---|
129 | get
|
---|
130 | {
|
---|
131 | return _rgb;
|
---|
132 | }
|
---|
133 | set
|
---|
134 | {
|
---|
135 | _rgb = value;
|
---|
136 | _exists=true;
|
---|
137 | _indexed = int.MinValue;
|
---|
138 | _auto = false;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | int _indexed;
|
---|
142 | /// <summary>
|
---|
143 | /// Indexed color value
|
---|
144 | /// </summary>
|
---|
145 | public int Indexed
|
---|
146 | {
|
---|
147 | get
|
---|
148 | {
|
---|
149 | return _indexed;
|
---|
150 | }
|
---|
151 | set
|
---|
152 | {
|
---|
153 | if (value < 0 || value > 65)
|
---|
154 | {
|
---|
155 | throw (new ArgumentOutOfRangeException("Index out of range"));
|
---|
156 | }
|
---|
157 | Clear();
|
---|
158 | _indexed = value;
|
---|
159 | _exists = true;
|
---|
160 | }
|
---|
161 | }
|
---|
162 | internal void Clear()
|
---|
163 | {
|
---|
164 | _theme = "";
|
---|
165 | _tint = decimal.MaxValue;
|
---|
166 | _indexed = int.MinValue;
|
---|
167 | _rgb = "";
|
---|
168 | _auto = false;
|
---|
169 | }
|
---|
170 | public void SetColor(System.Drawing.Color color)
|
---|
171 | {
|
---|
172 | Clear();
|
---|
173 | _rgb = color.ToArgb().ToString("X");
|
---|
174 | }
|
---|
175 |
|
---|
176 | internal ExcelColorXml Copy()
|
---|
177 | {
|
---|
178 | return new ExcelColorXml(NameSpaceManager) {_indexed=Indexed, _tint=Tint, _rgb=Rgb, _theme=Theme, _auto=Auto, _exists=Exists };
|
---|
179 | }
|
---|
180 |
|
---|
181 | internal override XmlNode CreateXmlNode(XmlNode topNode)
|
---|
182 | {
|
---|
183 | TopNode = topNode;
|
---|
184 | if(_rgb!="")
|
---|
185 | {
|
---|
186 | SetXmlNodeString("@rgb", _rgb);
|
---|
187 | }
|
---|
188 | else if (_indexed >= 0)
|
---|
189 | {
|
---|
190 | SetXmlNodeString("@indexed", _indexed.ToString());
|
---|
191 | }
|
---|
192 | else if (_auto)
|
---|
193 | {
|
---|
194 | SetXmlNodeBool("@auto", _auto);
|
---|
195 | }
|
---|
196 | else
|
---|
197 | {
|
---|
198 | SetXmlNodeString("@theme", _theme.ToString());
|
---|
199 | }
|
---|
200 | if (_tint != decimal.MaxValue)
|
---|
201 | {
|
---|
202 | SetXmlNodeString("@tint", _tint.ToString(CultureInfo.InvariantCulture));
|
---|
203 | }
|
---|
204 | return TopNode;
|
---|
205 | }
|
---|
206 |
|
---|
207 | bool _exists;
|
---|
208 | internal bool Exists
|
---|
209 | {
|
---|
210 | get
|
---|
211 | {
|
---|
212 | return _exists;
|
---|
213 | }
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|