LibreOffice Module oox (master) 1
colorchoicecontext.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
24#include <oox/token/namespaces.hxx>
25#include <oox/token/tokens.hxx>
26#include <unordered_map>
27#include <frozen/bits/defines.h>
28#include <frozen/bits/elsa_std.h>
29#include <frozen/unordered_map.h>
30
31namespace oox::drawingml {
32
33namespace
34{
35
36constexpr frozen::unordered_map<sal_Int32, model::SystemColorType, 30> constSystemColorMap
37{
38 { XML_scrollBar, model::SystemColorType::ScrollBar },
39 { XML_background, model::SystemColorType::Background },
40 { XML_activeCaption, model::SystemColorType::ActiveCaption },
41 { XML_inactiveCaption, model::SystemColorType::InactiveCaption },
42 { XML_menu, model::SystemColorType::Menu },
43 { XML_window, model::SystemColorType::Window },
44 { XML_windowFrame, model::SystemColorType::WindowFrame },
45 { XML_menuText, model::SystemColorType::MenuText },
46 { XML_windowText, model::SystemColorType::WindowText },
47 { XML_captionText, model::SystemColorType::CaptionText },
48 { XML_activeBorder, model::SystemColorType::ActiveBorder },
49 { XML_inactiveBorder, model::SystemColorType::InactiveBorder },
50 { XML_appWorkspace, model::SystemColorType::AppWorkspace },
51 { XML_highlight, model::SystemColorType::Highlight },
52 { XML_highlightText, model::SystemColorType::HighlightText },
54 { XML_btnShadow, model::SystemColorType::ButtonShadow },
55 { XML_grayText, model::SystemColorType::GrayText },
57 { XML_inactiveCaptionText, model::SystemColorType::InactiveCaptionText },
58 { XML_btnHighlight, model::SystemColorType::ButtonHighlight },
59 { XML_3dDkShadow, model::SystemColorType::DarkShadow3D },
60 { XML_3dLight, model::SystemColorType::Light3D },
61 { XML_infoText, model::SystemColorType::InfoText },
63 { XML_hotLight, model::SystemColorType::HotLight },
64 { XML_gradientActiveCaption, model::SystemColorType::GradientActiveCaption },
65 { XML_gradientInactiveCaption, model::SystemColorType::GradientInactiveCaption },
66 { XML_menuHighlight, model::SystemColorType::MenuHighlight },
67 { XML_menuBar, model::SystemColorType::MenuBar }
68};
69
70constexpr frozen::unordered_map<sal_Int32, model::TransformationType, 28> constTransformTypeMap
71{
100};
101
102}
103
104ColorValueContext::ColorValueContext(ContextHandler2Helper const & rParent, Color& rColor, model::ComplexColor* pComplexColor)
105 : ContextHandler2(rParent)
106 , mrColor(rColor)
107 , mpComplexColor(pComplexColor)
108{
109}
110
112{
113 switch( getCurrentElement() )
114 {
115 case A_TOKEN(scrgbClr):
116 {
118 rAttribs.getInteger( XML_r, 0 ),
119 rAttribs.getInteger( XML_g, 0 ),
120 rAttribs.getInteger( XML_b, 0 ) );
121 if (mpComplexColor)
122 {
124 rAttribs.getInteger(XML_r, 0),
125 rAttribs.getInteger(XML_g, 0),
126 rAttribs.getInteger(XML_b, 0));
127 }
128 }
129 break;
130
131 case A_TOKEN(srgbClr):
132 {
133 mrColor.setSrgbClr(rAttribs.getIntegerHex(XML_val, 0));
134 if (mpComplexColor)
135 {
136 mpComplexColor->setRGB(rAttribs.getIntegerHex(XML_val, 0));
137 }
138 }
139 break;
140
141 case A_TOKEN(hslClr):
142 {
144 rAttribs.getInteger( XML_hue, 0 ),
145 rAttribs.getInteger( XML_sat, 0 ),
146 rAttribs.getInteger( XML_lum, 0 ) );
147
148 if (mpComplexColor)
149 {
151 rAttribs.getInteger(XML_hue, 0),
152 rAttribs.getInteger(XML_sat, 0),
153 rAttribs.getInteger(XML_lum, 0));
154 }
155 }
156 break;
157
158 case A_TOKEN(sysClr):
159 {
160 sal_Int32 nToken = rAttribs.getToken(XML_val, XML_TOKEN_INVALID);
161 sal_Int32 nLastColor = rAttribs.getIntegerHex(XML_lastClr, -1);
162
163 mrColor.setSysClr(nToken, nLastColor);
164
165 if (mpComplexColor)
166 {
167 auto aIterator = constSystemColorMap.find(nToken);
168 if (aIterator != constSystemColorMap.end())
169 {
170 auto const& aPair = *aIterator;
171 model::SystemColorType eType = aPair.second;
172 mpComplexColor->setSystemColor(eType, nLastColor);
173 }
174 }
175 }
176 break;
177
178 case A_TOKEN(schemeClr):
179 {
180 auto nToken = rAttribs.getToken(XML_val, XML_TOKEN_INVALID);
182 std::optional<OUString> sSchemeName = rAttribs.getString(XML_val);
183 if (sSchemeName.has_value())
184 {
185 mrColor.setSchemeName(*sSchemeName);
186
187 if (mpComplexColor)
188 {
189 if (nToken == XML_phClr)
190 {
192 }
193 else
194 {
197 }
198 }
199 }
200 }
201 break;
202
203 case A_TOKEN(prstClr):
204 {
205 sal_Int32 nToken = rAttribs.getToken(XML_val, XML_TOKEN_INVALID);
207 if (mpComplexColor)
208 {
209 // TODO - just converted to RGB for now
211 mpComplexColor->mnComponent1 = nRgbValue.GetRed();
212 mpComplexColor->mnComponent2 = nRgbValue.GetGreen();
213 mpComplexColor->mnComponent3 = nRgbValue.GetBlue();
215 }
216 }
217 break;
218 }
219}
220
222 sal_Int32 nElement, const AttributeList& rAttribs )
223{
224 switch( nElement )
225 {
226 case A_TOKEN( alpha ):
227 case A_TOKEN( alphaMod ):
228 case A_TOKEN( alphaOff ):
229 case A_TOKEN( blue ):
230 case A_TOKEN( blueMod ):
231 case A_TOKEN( blueOff ):
232 case A_TOKEN( hue ):
233 case A_TOKEN( hueMod ):
234 case A_TOKEN( hueOff ):
235 case A_TOKEN( lum ):
236 case A_TOKEN( lumMod ):
237 case A_TOKEN( lumOff ):
238 case A_TOKEN( green ):
239 case A_TOKEN( greenMod ):
240 case A_TOKEN( greenOff ):
241 case A_TOKEN( red ):
242 case A_TOKEN( redMod ):
243 case A_TOKEN( redOff ):
244 case A_TOKEN( sat ):
245 case A_TOKEN( satMod ):
246 case A_TOKEN( satOff ):
247 case A_TOKEN( shade ):
248 case A_TOKEN( tint ):
249 {
250 OUString aValue = rAttribs.getStringDefaulted( XML_val);
251 sal_Int32 nVal = 0;
252 if (aValue.endsWith("%"))
253 {
254 nVal = aValue.toDouble() * PER_PERCENT;
255 }
256 else
257 nVal = rAttribs.getInteger(XML_val, 0);
258 mrColor.addTransformation( nElement, nVal );
259 }
260 break;
261 case A_TOKEN( comp ):
262 case A_TOKEN( gamma ):
263 case A_TOKEN( gray ):
264 case A_TOKEN( inv ):
265 case A_TOKEN( invGamma ):
266 mrColor.addTransformation( nElement );
267 break;
268 }
269
270 if (mpComplexColor)
271 {
272 auto aIterator = constTransformTypeMap.find(getBaseToken(nElement));
273 if (aIterator != constTransformTypeMap.end())
274 {
275 auto const& aPair = *aIterator;
276 model::TransformationType eType = aPair.second;
277
278 OUString aValueString = rAttribs.getStringDefaulted(XML_val);
279 sal_Int32 nValue = 0;
280 if (aValueString.endsWith("%"))
281 nValue = aValueString.toDouble() * PER_PERCENT;
282 else
283 nValue = rAttribs.getInteger(XML_val, 0);
284
285 mpComplexColor->maTransformations.push_back({eType, sal_Int16(nValue / 10)});
286 }
287 }
288
289 return nullptr;
290}
291
292ColorContext::ColorContext(ContextHandler2Helper const & rParent, Color& rColor, model::ComplexColor* pComplexColor)
293 : ContextHandler2(rParent)
294 , mrColor(rColor)
295 , mpComplexColor(pComplexColor)
296{
297}
298
300 sal_Int32 nElement, const AttributeList& )
301{
302 switch( nElement )
303 {
304 case A_TOKEN( scrgbClr ):
305 case A_TOKEN( srgbClr ):
306 case A_TOKEN( hslClr ):
307 case A_TOKEN( sysClr ):
308 case A_TOKEN( schemeClr ):
309 case A_TOKEN( prstClr ):
310 return new ColorValueContext(*this, mrColor, mpComplexColor);
311 }
312 return nullptr;
313}
314
315ColorsContext::ColorsContext(ContextHandler2Helper const& rParent, std::vector<Color>& rColors)
316 : ContextHandler2(rParent)
317 , mrColors(rColors)
318{
319}
320
322 const AttributeList&)
323{
324 switch (nElement)
325 {
326 case A_TOKEN(scrgbClr):
327 case A_TOKEN(srgbClr):
328 case A_TOKEN(hslClr):
329 case A_TOKEN(sysClr):
330 case A_TOKEN(schemeClr):
331 case A_TOKEN(prstClr):
332 {
333 mrColors.emplace_back();
334 return new ColorValueContext(*this, mrColors.back(), nullptr);
335 }
336 }
337 return nullptr;
338}
339
340} // namespace oox::drawingml
341
342/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt8 GetBlue() const
sal_uInt8 GetRed() const
sal_uInt8 GetGreen() const
void setSchemeColor(ThemeColorType eType)
void setCRGB(sal_Int32 nR, sal_Int32 nG, sal_Int32 nB)
sal_Int32 mnComponent2
std::vector< Transformation > maTransformations
void setHSL(sal_Int32 nH, sal_Int32 nS, sal_Int32 nL)
void setRGB(sal_Int32 nRGB)
sal_Int32 mnComponent1
void setSystemColor(SystemColorType eSystemColorType, sal_Int32 nRGB)
sal_Int32 mnComponent3
Provides access to attribute values of an element.
OUString getStringDefaulted(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute, returns an empty string if attribute not present...
std::optional< sal_Int32 > getIntegerHex(sal_Int32 nAttrToken) const
Returns the 32-bit signed integer value of the specified attribute (hexadecimal).
std::optional< sal_Int32 > getInteger(sal_Int32 nAttrToken) const
Returns the 32-bit signed integer value of the specified attribute (decimal).
std::optional< OUString > getString(sal_Int32 nAttrToken) const
Returns the string value of the specified attribute.
std::optional< sal_Int32 > getToken(sal_Int32 nAttrToken) const
Returns the token identifier of the value of the specified attribute.
ColorContext(::oox::core::ContextHandler2Helper const &rParent, Color &rColor, model::ComplexColor *pComplexColor=nullptr)
model::ComplexColor * mpComplexColor
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const ::oox::AttributeList &rAttribs) override
Context handler for the different color value elements (a:scrgbClr, a:srgbClr, a:hslClr,...
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const ::oox::AttributeList &rAttribs) override
virtual void onStartElement(const ::oox::AttributeList &rAttribs) override
ColorValueContext(::oox::core::ContextHandler2Helper const &rParent, Color &rColor, model::ComplexColor *pComplexColor=nullptr)
void addTransformation(sal_Int32 nElement, sal_Int32 nValue=-1)
Inserts the passed color transformation.
Definition: color.cxx:408
void setPrstClr(sal_Int32 nToken)
Sets a predefined color from the a:prstClr element.
Definition: color.cxx:368
void setScrgbClr(sal_Int32 nR, sal_Int32 nG, sal_Int32 nB)
Sets the percentual RGB values from the a:scrgbClr element.
Definition: color.cxx:346
void setSchemeClr(sal_Int32 nToken)
Sets a scheme color from the a:schemeClr element.
Definition: color.cxx:384
void setHslClr(sal_Int32 nHue, sal_Int32 nSat, sal_Int32 nLum)
Sets the HSL values from the a:hslClr element.
Definition: color.cxx:357
void setSysClr(sal_Int32 nToken, sal_Int32 nLastRgb)
Sets a system color from the a:sysClr element.
Definition: color.cxx:400
::Color getDmlPresetColor(sal_Int32 nToken, ::Color nDefaultRgb)
Returns the RGB value for the passed DrawingML color token, or nDefaultRgb on error.
Definition: color.cxx:302
void setSchemeName(const OUString &sSchemeName)
Sets the scheme name from the a:schemeClr element for interoperability purposes.
Definition: color.hxx:71
void setSrgbClr(sal_Int32 nRgb)
Sets an RGB value (hexadecimal RRGGBB) from the a:srgbClr element.
Definition: color.cxx:339
virtual ::oox::core::ContextHandlerRef onCreateContext(sal_Int32 nElement, const ::oox::AttributeList &rAttribs) override
ColorsContext(::oox::core::ContextHandler2Helper const &rParent, std::vector< Color > &rColors)
std::vector< Color > & mrColors
DocumentType eType
sal_Int16 nValue
constexpr double alpha[nDetails]
ThemeColorType
TransformationType
SystemColorType
model::ThemeColorType schemeNameToThemeColorType(OUString const &rSchemeName)
Definition: color.cxx:274
const sal_Int32 PER_PERCENT
const ::Color API_RGB_TRANSPARENT(ColorTransparency, 0xffffffff)
Transparent color for API calls.
comp
XML_TOKEN_INVALID
DefTokenId nToken