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>
12 #include <boost/property_tree/json_parser.hpp>
15 #include <sal/log.hxx>
16 #include <tools/json_writer.hxx>
18 namespace model::color
20 bool convertFromJSON(OString
const& rJsonString
, model::ComplexColor
& rComplexColor
)
22 model::ComplexColor aComplexColor
;
26 std::stringstream
aStream((std::string(rJsonString
)));
27 boost::property_tree::ptree aRootTree
;
28 boost::property_tree::read_json(aStream
, aRootTree
);
30 sal_Int32 nThemeType
= aRootTree
.get
<sal_Int32
>("ThemeIndex", -1);
31 aComplexColor
.setThemeColor(model::convertToThemeColorType(nThemeType
));
32 boost::property_tree::ptree aTransformTree
= aRootTree
.get_child("Transformations");
33 for (const auto& rEachTransformationNode
:
34 boost::make_iterator_range(aTransformTree
.equal_range("")))
36 auto const& rTransformationTree
= rEachTransformationNode
.second
;
37 std::string sType
= rTransformationTree
.get
<std::string
>("Type", "");
38 sal_Int16 nValue
= rTransformationTree
.get
<sal_Int16
>("Value", 0);
40 auto eType
= model::TransformationType::Undefined
;
41 if (sType
== "LumOff")
42 eType
= model::TransformationType::LumOff
;
43 else if (sType
== "LumMod")
44 eType
= model::TransformationType::LumMod
;
45 else if (sType
== "Tint")
46 eType
= model::TransformationType::Tint
;
47 else if (sType
== "Shade")
48 eType
= model::TransformationType::Shade
;
50 if (eType
!= model::TransformationType::Undefined
)
51 aComplexColor
.addTransformation({ eType
, nValue
});
54 catch (const boost::property_tree::json_parser_error
& /*exception*/)
59 rComplexColor
= std::move(aComplexColor
);
63 void convertToJSONTree(tools::JsonWriter
& rTree
, model::ComplexColor
const& rComplexColor
)
65 rTree
.put("ThemeIndex", sal_Int16(rComplexColor
.getThemeColorType()));
66 auto aTransformationsList
= rTree
.startArray("Transformations");
68 for (auto const& rTransformation
: rComplexColor
.getTransformations())
71 switch (rTransformation
.meType
)
73 case model::TransformationType::LumMod
:
76 case model::TransformationType::LumOff
:
79 case model::TransformationType::Tint
:
82 case model::TransformationType::Shade
:
90 auto aChild
= rTree
.startStruct();
91 rTree
.put("Type", aType
);
92 rTree
.put("Value", rTransformation
.mnValue
);
97 OString
convertToJSON(model::ComplexColor
const& rComplexColor
)
99 tools::JsonWriter aTree
;
100 convertToJSONTree(aTree
, rComplexColor
);
101 return aTree
.finishAndGetAsOString();
104 } // end model::theme
106 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */