LibreOffice Module oox (master) 1
clrscheme.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
20#include <algorithm>
21
22#include <com/sun/star/uno/Sequence.hxx>
23#include <com/sun/star/util/Color.hpp>
24
25#include <osl/diagnose.h>
27#include <oox/token/tokens.hxx>
29
30#include <frozen/bits/defines.h>
31#include <frozen/bits/elsa_std.h>
32#include <frozen/unordered_map.h>
33
34using namespace com::sun::star;
35
36namespace oox::drawingml {
37
38namespace
39{
40
41constexpr frozen::unordered_map<PredefinedClrSchemeId, std::u16string_view, 12> constPredefinedClrNames
42{
43 { dk1, u"dk1" },
44 { lt1, u"lt1" },
45 { dk2, u"dk2" },
46 { lt2, u"lt2" },
47 { accent1, u"accent1" },
48 { accent2, u"accent2" },
49 { accent3, u"accent3" },
50 { accent4, u"accent4" },
51 { accent5, u"accent5" },
52 { accent6, u"accent6" },
53 { hlink, u"hlink" },
54 { folHlink, u"folHlink" }
55};
56
57} // end anonymous namespace
58
60{
61 std::u16string_view empty;
62 auto iterator = constPredefinedClrNames.find(eID);
63 if (iterator == constPredefinedClrNames.end())
64 return empty;
65 return iterator->second;
66}
67
68bool ClrMap::getColorMap( sal_Int32& nClrToken )
69{
70 sal_Int32 nMapped = 0;
71 std::map < sal_Int32, sal_Int32 >::const_iterator aIter( maClrMap.find( nClrToken ) );
72 if ( aIter != maClrMap.end() )
73 nMapped = (*aIter).second;
74 if ( nMapped )
75 {
76 nClrToken = nMapped;
77 return true;
78 }
79 else
80 return false;
81}
82
83void ClrMap::setColorMap( sal_Int32 nClrToken, sal_Int32 nMappedClrToken )
84{
85 maClrMap[ nClrToken ] = nMappedClrToken;
86}
87
88namespace {
89
90struct find_by_token
91{
92 explicit find_by_token(sal_Int32 token):
93 m_token(token)
94 {
95 }
96
97 bool operator()(const std::pair<sal_Int32, ::Color>& r)
98 {
99 return r.first == m_token;
100 }
101
102private:
103 sal_Int32 m_token;
104};
105
106}
107
108bool ClrScheme::getColor( sal_Int32 nSchemeClrToken, ::Color& rColor ) const
109{
110 OSL_ASSERT((nSchemeClrToken & sal_Int32(0xFFFF0000))==0);
111 switch( nSchemeClrToken )
112 {
113 case XML_bg1 : nSchemeClrToken = XML_lt1; break;
114 case XML_bg2 : nSchemeClrToken = XML_lt2; break;
115 case XML_tx1 : nSchemeClrToken = XML_dk1; break;
116 case XML_tx2 : nSchemeClrToken = XML_dk2; break;
117 case XML_background1 : nSchemeClrToken = XML_lt1; break;
118 case XML_background2 : nSchemeClrToken = XML_lt2; break;
119 case XML_text1 : nSchemeClrToken = XML_dk1; break;
120 case XML_text2 : nSchemeClrToken = XML_dk2; break;
121 case XML_light1 : nSchemeClrToken = XML_lt1; break;
122 case XML_light2 : nSchemeClrToken = XML_lt2; break;
123 case XML_dark1 : nSchemeClrToken = XML_dk1; break;
124 case XML_dark2 : nSchemeClrToken = XML_dk2; break;
125 case XML_hyperlink : nSchemeClrToken = XML_hlink; break;
126 case XML_followedHyperlink: nSchemeClrToken = XML_folHlink; break;
127 }
128
129 auto aIter = std::find_if(maClrScheme.begin(), maClrScheme.end(), find_by_token(nSchemeClrToken) );
130
131 if ( aIter != maClrScheme.end() )
132 rColor = aIter->second;
133
134 return aIter != maClrScheme.end();
135}
136
137void ClrScheme::setColor( sal_Int32 nSchemeClrToken, ::Color nColor )
138{
139 const auto aIter = std::find_if(maClrScheme.begin(), maClrScheme.end(), find_by_token(nSchemeClrToken) );
140 if ( aIter != maClrScheme.end() )
141 aIter->second = nColor;
142 else
143 maClrScheme.emplace_back(nSchemeClrToken, nColor);
144}
145
146bool ClrScheme::getColorByIndex(size_t nIndex, ::Color& rColor) const
147{
148 if (nIndex >= maClrScheme.size())
149 return false;
150
151 rColor = maClrScheme[nIndex].second;
152 return true;
153}
154
155void ClrScheme::ToAny(css::uno::Any& rVal) const
156{
157 std::vector<util::Color> aRet;
158
159 for (const auto& rIndexAndColor : maClrScheme)
160 {
161 aRet.push_back(static_cast<sal_Int32>(rIndexAndColor.second));
162 }
163
164 rVal <<= comphelper::containerToSequence(aRet);
165}
166
167void ClrScheme::fill(model::ColorSet& rColorSet) const
168{
169 for (const auto& [nToken, rColor] : maClrScheme)
170 {
171 switch (nToken)
172 {
173 case XML_tx1:
174 case XML_dk1: rColorSet.add(model::ThemeColorType::Dark1, rColor); break;
175 case XML_bg1:
176 case XML_lt1: rColorSet.add(model::ThemeColorType::Light1, rColor); break;
177 case XML_tx2:
178 case XML_dk2: rColorSet.add(model::ThemeColorType::Dark2, rColor); break;
179 case XML_bg2:
180 case XML_lt2: rColorSet.add(model::ThemeColorType::Light2, rColor); break;
181 case XML_accent1: rColorSet.add(model::ThemeColorType::Accent1, rColor); break;
182 case XML_accent2: rColorSet.add(model::ThemeColorType::Accent2, rColor); break;
183 case XML_accent3: rColorSet.add(model::ThemeColorType::Accent3, rColor); break;
184 case XML_accent4: rColorSet.add(model::ThemeColorType::Accent4, rColor); break;
185 case XML_accent5: rColorSet.add(model::ThemeColorType::Accent5, rColor); break;
186 case XML_accent6: rColorSet.add(model::ThemeColorType::Accent6, rColor); break;
187 case XML_hlink: rColorSet.add(model::ThemeColorType::Hyperlink, rColor); break;
188 case XML_folHlink: rColorSet.add(model::ThemeColorType::FollowedHyperlink, rColor); break;
189 default: break;
190 }
191 }
192}
193
194}
195
196/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void add(model::ThemeColorType Type, Color aColorData)
bool getColorMap(sal_Int32 &nClrToken)
Definition: clrscheme.cxx:68
void setColorMap(sal_Int32 nClrToken, sal_Int32 nMappedClrToken)
Definition: clrscheme.cxx:83
std::map< sal_Int32, sal_Int32 > maClrMap
Definition: clrscheme.hxx:56
bool getColor(sal_Int32 nSchemeClrToken, ::Color &rColor) const
Definition: clrscheme.cxx:108
void ToAny(css::uno::Any &rVal) const
Definition: clrscheme.cxx:155
bool getColorByIndex(size_t nIndex, ::Color &rColor) const
Definition: clrscheme.cxx:146
void setColor(sal_Int32 nSchemeClrToken, ::Color nColor)
Definition: clrscheme.cxx:137
std::vector< std::pair< sal_Int32, ::Color > > maClrScheme
Definition: clrscheme.hxx:68
void fill(model::ColorSet &rColorSet) const
Definition: clrscheme.cxx:167
sal_Int32 m_token
Definition: clrscheme.cxx:103
float u
sal_Int32 nIndex
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
std::u16string_view getPredefinedClrNames(PredefinedClrSchemeId eID)
Definition: clrscheme.cxx:59
DefTokenId nToken