1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include <docmodel/color/ComplexColorJSON.hxx>
14 #include <sal/log.hxx>
16 namespace model::color
18 bool convertFromJSON(OString
const& rJsonString
, model::ComplexColor
& rComplexColor
)
20 model::ComplexColor aComplexColor
;
24 std::stringstream
aStream((std::string(rJsonString
)));
25 boost::property_tree::ptree aRootTree
;
26 boost::property_tree::read_json(aStream
, aRootTree
);
28 sal_Int32 nThemeType
= aRootTree
.get
<sal_Int32
>("ThemeIndex", -1);
29 aComplexColor
.setSchemeColor(model::convertToThemeColorType(nThemeType
));
30 boost::property_tree::ptree aTransformTree
= aRootTree
.get_child("Transformations");
31 for (const auto& rEachTransformationNode
:
32 boost::make_iterator_range(aTransformTree
.equal_range("")))
34 auto const& rTransformationTree
= rEachTransformationNode
.second
;
35 std::string sType
= rTransformationTree
.get
<std::string
>("Type", "");
36 sal_Int16 nValue
= rTransformationTree
.get
<sal_Int16
>("Value", 0);
38 auto eType
= model::TransformationType::Undefined
;
39 if (sType
== "LumOff")
40 eType
= model::TransformationType::LumOff
;
41 else if (sType
== "LumMod")
42 eType
= model::TransformationType::LumMod
;
43 else if (sType
== "Tint")
44 eType
= model::TransformationType::Tint
;
45 else if (sType
== "Shade")
46 eType
= model::TransformationType::Shade
;
48 if (eType
!= model::TransformationType::Undefined
)
49 aComplexColor
.addTransformation({ eType
, nValue
});
52 catch (const boost::property_tree::json_parser_error
& /*exception*/)
57 rComplexColor
= aComplexColor
;
61 void convertToJSONTree(boost::property_tree::ptree
& rTree
, model::ComplexColor
const& rComplexColor
)
63 rTree
.put("ThemeIndex", sal_Int16(rComplexColor
.getSchemeType()));
65 boost::property_tree::ptree aTransformationsList
;
66 for (auto const& rTransformation
: rComplexColor
.getTransformations())
69 switch (rTransformation
.meType
)
71 case model::TransformationType::LumMod
:
74 case model::TransformationType::LumOff
:
77 case model::TransformationType::Tint
:
80 case model::TransformationType::Shade
:
88 boost::property_tree::ptree aChild
;
89 aChild
.put("Type", aType
);
90 aChild
.put("Value", rTransformation
.mnValue
);
91 aTransformationsList
.push_back(std::make_pair("", aChild
));
94 rTree
.add_child("Transformations", aTransformationsList
);
97 OString
convertToJSON(model::ComplexColor
const& rComplexColor
)
99 boost::property_tree::ptree aTree
;
100 convertToJSONTree(aTree
, rComplexColor
);
101 std::stringstream aStream
;
102 boost::property_tree::write_json(aStream
, aTree
);
103 return OString(aStream
.str());
106 } // end model::theme
108 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */