1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "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"
39 class ColorNameMap
: private boost::noncopyable
{
43 OUString
lookUp(long color
) const;
46 typedef boost::unordered_map
< long, OUString
> Map
;
51 ColorNameMap::ColorNameMap() {
52 css::uno::Sequence
< OUString
> aNames
;
53 css::uno::Reference
< css::container::XNameAccess
> xNA
;
57 // Create color table in which to look up the given color.
58 css::uno::Reference
< css::container::XNameContainer
> xColorTable
=
59 css::drawing::ColorTable::create( comphelper::getProcessComponentContext() );
61 // Get list of color names in order to iterate over the color table.
63 // Lock the solar mutex here as workarround for missing lock in
65 SolarMutexGuard aGuard
;
66 aNames
= xNA
->getElementNames();
68 catch (css::uno::RuntimeException
const&)
70 // When an exception occurred then whe have an empty name sequence
71 // and the loop below is not entered.
74 // Fill the map to convert from numerical color values to names.
76 for (long int i
=0; i
<aNames
.getLength(); i
++)
78 // Get the numerical value for the i-th color name.
81 css::uno::Any
aColor (xNA
->getByName (aNames
[i
]));
84 map_
[nColor
] = aNames
[i
];
86 catch (css::uno::RuntimeException
const&)
88 // Ignore the exception: the color who lead to the exception
89 // is not included into the map.
94 OUString
ColorNameMap::lookUp(long color
) const {
95 Map::const_iterator
i(map_
.find(color
));
96 if (i
!= map_
.end()) {
99 // Did not find the given color; return its RGB tuple representation:
101 buf
.append(sal_Unicode('#'));
102 buf
.append(color
, 16);
103 return buf
.makeStringAndClear();
106 struct theColorNameMap
: public rtl::Static
< ColorNameMap
, theColorNameMap
> {};
110 namespace accessibility
{
112 OUString
lookUpColorName(long color
) {
113 return theColorNameMap::get().lookUp(color
);
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */