android: Update app-specific/MIME type icons
[LibreOffice.git] / docmodel / source / color / ComplexColorJSON.cxx
blobf6b52d1bf55cbd011dcc45c2faeb5b3f9652f91c
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 */
11 #include <docmodel/color/ComplexColorJSON.hxx>
12 #include <sstream>
13 #include <utility>
14 #include <sal/log.hxx>
16 namespace model::color
18 bool convertFromJSON(OString const& rJsonString, model::ComplexColor& rComplexColor)
20 model::ComplexColor aComplexColor;
22 try
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*/)
54 return false;
57 rComplexColor = aComplexColor;
58 return true;
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())
68 std::string aType;
69 switch (rTransformation.meType)
71 case model::TransformationType::LumMod:
72 aType = "LumMod";
73 break;
74 case model::TransformationType::LumOff:
75 aType = "LumOff";
76 break;
77 case model::TransformationType::Tint:
78 aType = "Tint";
79 break;
80 case model::TransformationType::Shade:
81 aType = "Shade";
82 break;
83 default:
84 break;
86 if (!aType.empty())
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: */