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