bump product version to 7.2.5.1
[LibreOffice.git] / svx / source / accessibility / lookupcolorname.cxx
blob520843c26caa61fc20bd9c72e264d358d1294c41
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 <sal/config.h>
22 #include <com/sun/star/container/XNameAccess.hpp>
23 #include <com/sun/star/container/XNameContainer.hpp>
24 #include <com/sun/star/drawing/ColorTable.hpp>
25 #include <com/sun/star/uno/Any.hxx>
26 #include <com/sun/star/uno/Reference.hxx>
27 #include <com/sun/star/uno/RuntimeException.hpp>
28 #include <com/sun/star/uno/Sequence.hxx>
29 #include <comphelper/processfactory.hxx>
30 #include <rtl/ustring.hxx>
31 #include <vcl/svapp.hxx>
33 #include "lookupcolorname.hxx"
34 #include <unordered_map>
36 namespace
38 class ColorNameMap
40 public:
41 ColorNameMap();
42 ColorNameMap(const ColorNameMap&) = delete;
43 ColorNameMap& operator=(const ColorNameMap&) = delete;
45 OUString lookUp(tools::Long color) const;
47 private:
48 typedef std::unordered_map<tools::Long, OUString> Map;
50 Map map_;
53 ColorNameMap::ColorNameMap()
55 css::uno::Sequence<OUString> aNames;
56 css::uno::Reference<css::container::XNameAccess> xNA;
58 try
60 // Create color table in which to look up the given color.
61 css::uno::Reference<css::container::XNameContainer> xColorTable
62 = css::drawing::ColorTable::create(comphelper::getProcessComponentContext());
64 // Get list of color names in order to iterate over the color table.
66 // Lock the solar mutex here as workaround for missing lock in
67 // called function.
68 SolarMutexGuard aGuard;
69 xNA = xColorTable;
70 aNames = xColorTable->getElementNames();
72 catch (css::uno::RuntimeException const&)
74 // When an exception occurred then we have an empty name sequence
75 // and the loop below is not entered.
78 // Fill the map to convert from numerical color values to names.
79 if (!xNA.is())
80 return;
82 for (const auto& rName : std::as_const(aNames))
84 // Get the numerical value for the i-th color name.
85 try
87 css::uno::Any aColor = xNA->getByName(rName);
88 tools::Long nColor = 0;
89 aColor >>= nColor;
90 map_[nColor] = rName;
92 catch (css::uno::RuntimeException const&)
94 // Ignore the exception: the color who lead to the exception
95 // is not included into the map.
100 OUString ColorNameMap::lookUp(tools::Long color) const
102 Map::const_iterator i(map_.find(color));
103 if (i != map_.end())
105 return i->second;
107 // Did not find the given color; return its RGB tuple representation:
108 return "#" + OUString::number(color, 16);
111 struct theColorNameMap : public rtl::Static<ColorNameMap, theColorNameMap>
116 namespace accessibility
118 OUString lookUpColorName(tools::Long color) { return theColorNameMap::get().lookUp(color); }
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */