bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / accessibility / lookupcolorname.cxx
blob1e941b069a81cd304975fe7274b56f8fd2194058
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 "boost/noncopyable.hpp"
23 #include "com/sun/star/container/XNameAccess.hpp"
24 #include "com/sun/star/container/XNameContainer.hpp"
25 #include "com/sun/star/drawing/ColorTable.hpp"
26 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
27 #include "com/sun/star/uno/Any.hxx"
28 #include "com/sun/star/uno/Reference.hxx"
29 #include "com/sun/star/uno/RuntimeException.hpp"
30 #include "com/sun/star/uno/Sequence.hxx"
31 #include "comphelper/processfactory.hxx"
32 #include "rtl/ustring.h"
33 #include "rtl/ustring.hxx"
34 #include "vcl/svapp.hxx"
36 #include <lookupcolorname.hxx>
37 #include <unordered_map>
39 namespace {
41 class ColorNameMap: private boost::noncopyable {
42 public:
43 ColorNameMap();
45 OUString lookUp(long color) const;
47 private:
48 typedef std::unordered_map< long, OUString > Map;
50 Map map_;
53 ColorNameMap::ColorNameMap() {
54 css::uno::Sequence< OUString > aNames;
55 css::uno::Reference< css::container::XNameAccess > xNA;
57 try
59 // Create color table in which to look up the given color.
60 css::uno::Reference< css::container::XNameContainer > xColorTable =
61 css::drawing::ColorTable::create( comphelper::getProcessComponentContext() );
63 // Get list of color names in order to iterate over the color table.
65 // Lock the solar mutex here as workaround for missing lock in
66 // called function.
67 SolarMutexGuard aGuard;
68 aNames = xNA->getElementNames();
70 catch (css::uno::RuntimeException const&)
72 // When an exception occurred then whe have an empty name sequence
73 // and the loop below is not entered.
76 // Fill the map to convert from numerical color values to names.
77 if (xNA.is())
78 for (long int i=0; i<aNames.getLength(); i++)
80 // Get the numerical value for the i-th color name.
81 try
83 css::uno::Any aColor (xNA->getByName (aNames[i]));
84 long nColor = 0;
85 aColor >>= nColor;
86 map_[nColor] = aNames[i];
88 catch (css::uno::RuntimeException const&)
90 // Ignore the exception: the color who lead to the exception
91 // is not included into the map.
96 OUString ColorNameMap::lookUp(long color) const {
97 Map::const_iterator i(map_.find(color));
98 if (i != map_.end()) {
99 return i->second;
101 // Did not find the given color; return its RGB tuple representation:
102 OUStringBuffer buf;
103 buf.append('#');
104 buf.append(color, 16);
105 return buf.makeStringAndClear();
108 struct theColorNameMap: public rtl::Static< ColorNameMap, theColorNameMap > {};
112 namespace accessibility {
114 OUString lookUpColorName(long color) {
115 return theColorNameMap::get().lookUp(color);
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */