1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <sal/config.h>
21 
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/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/uno/Any.hxx>
27 #include <com/sun/star/uno/Reference.hxx>
28 #include <com/sun/star/uno/RuntimeException.hpp>
29 #include <com/sun/star/uno/Sequence.hxx>
30 #include <comphelper/processfactory.hxx>
31 #include <rtl/ustring.h>
32 #include <rtl/ustring.hxx>
33 #include <rtl/ustrbuf.hxx>
34 #include <vcl/svapp.hxx>
35 
36 #include "lookupcolorname.hxx"
37 #include <unordered_map>
38 
39 namespace {
40 
41 class ColorNameMap {
42 public:
43     ColorNameMap();
44     ColorNameMap(const ColorNameMap&) = delete;
45     ColorNameMap& operator=(const ColorNameMap&) = delete;
46 
47     OUString lookUp(long color) const;
48 
49 private:
50     typedef std::unordered_map< long, OUString > Map;
51 
52     Map map_;
53 };
54 
ColorNameMap()55 ColorNameMap::ColorNameMap() {
56     css::uno::Sequence< OUString > aNames;
57     css::uno::Reference< css::container::XNameAccess > xNA;
58 
59     try
60     {
61         // Create color table in which to look up the given color.
62         css::uno::Reference< css::container::XNameContainer > xColorTable =
63              css::drawing::ColorTable::create( comphelper::getProcessComponentContext() );
64 
65         // Get list of color names in order to iterate over the color table.
66 
67         // Lock the solar mutex here as workaround for missing lock in
68         // called function.
69         SolarMutexGuard aGuard;
70         xNA = xColorTable;
71         aNames = xColorTable->getElementNames();
72     }
73     catch (css::uno::RuntimeException const&)
74     {
75         // When an exception occurred then we have an empty name sequence
76         // and the loop below is not entered.
77     }
78 
79     // Fill the map to convert from numerical color values to names.
80     if (xNA.is())
81         for (const auto& rName : std::as_const(aNames))
82         {
83             // Get the numerical value for the i-th color name.
84             try
85             {
86                 css::uno::Any aColor = xNA->getByName(rName);
87                 long nColor = 0;
88                 aColor >>= nColor;
89                 map_[nColor] = rName;
90             }
91             catch (css::uno::RuntimeException const&)
92             {
93                 // Ignore the exception: the color who lead to the exception
94                 // is not included into the map.
95             }
96         }
97 }
98 
lookUp(long color) const99 OUString ColorNameMap::lookUp(long color) const {
100     Map::const_iterator i(map_.find(color));
101     if (i != map_.end()) {
102         return i->second;
103     }
104     // Did not find the given color; return its RGB tuple representation:
105     return "#" + OUString::number(color, 16);
106 }
107 
108 struct theColorNameMap: public rtl::Static< ColorNameMap, theColorNameMap > {};
109 
110 }
111 
112 namespace accessibility {
113 
lookUpColorName(long color)114 OUString lookUpColorName(long color) {
115     return theColorNameMap::get().lookUp(color);
116 }
117 
118 }
119 
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
121