tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / oox / source / drawingml / clrscheme.cxx
blob7a0b12fe3f0c307834e63c0370d263335df4896b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <algorithm>
22 #include <com/sun/star/uno/Sequence.hxx>
23 #include <com/sun/star/util/Color.hpp>
25 #include <osl/diagnose.h>
26 #include <oox/drawingml/clrscheme.hxx>
27 #include <oox/token/tokens.hxx>
28 #include <comphelper/sequence.hxx>
30 #include <frozen/bits/defines.h>
31 #include <frozen/bits/elsa_std.h>
32 #include <frozen/unordered_map.h>
34 using namespace com::sun::star;
36 namespace oox::drawingml {
38 namespace
41 constexpr frozen::unordered_map<PredefinedClrSchemeId, std::u16string_view, 12> constPredefinedClrNames
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" }
57 } // end anonymous namespace
59 std::u16string_view getPredefinedClrNames(PredefinedClrSchemeId eID)
61 std::u16string_view empty;
62 auto iterator = constPredefinedClrNames.find(eID);
63 if (iterator == constPredefinedClrNames.end())
64 return empty;
65 return iterator->second;
68 bool ClrMap::getColorMap( sal_Int32& nClrToken )
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 )
76 nClrToken = nMapped;
77 return true;
79 else
80 return false;
83 void ClrMap::setColorMap( sal_Int32 nClrToken, sal_Int32 nMappedClrToken )
85 maClrMap[ nClrToken ] = nMappedClrToken;
88 namespace {
90 struct find_by_token
92 explicit find_by_token(sal_Int32 token):
93 m_token(token)
97 bool operator()(const std::pair<sal_Int32, ::Color>& r)
99 return r.first == m_token;
102 private:
103 sal_Int32 m_token;
108 bool ClrScheme::getColor( sal_Int32 nSchemeClrToken, ::Color& rColor ) const
110 OSL_ASSERT((nSchemeClrToken & sal_Int32(0xFFFF0000))==0);
111 switch( nSchemeClrToken )
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;
129 auto aIter = std::find_if(maClrScheme.begin(), maClrScheme.end(), find_by_token(nSchemeClrToken) );
131 if ( aIter != maClrScheme.end() )
132 rColor = aIter->second;
134 return aIter != maClrScheme.end();
137 void ClrScheme::setColor( sal_Int32 nSchemeClrToken, ::Color nColor )
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);
146 bool ClrScheme::getColorByIndex(size_t nIndex, ::Color& rColor) const
148 if (nIndex >= maClrScheme.size())
149 return false;
151 rColor = maClrScheme[nIndex].second;
152 return true;
155 void ClrScheme::ToAny(css::uno::Any& rVal) const
157 std::vector<util::Color> aRet;
159 for (const auto& rIndexAndColor : maClrScheme)
161 aRet.push_back(static_cast<sal_Int32>(rIndexAndColor.second));
164 rVal <<= comphelper::containerToSequence(aRet);
167 void ClrScheme::fill(model::ColorSet& rColorSet) const
169 for (const auto& [nToken, rColor] : maClrScheme)
171 switch (nToken)
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;
196 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */