LibreOffice Module svx (master) 1
lookupcolorname.cxx
Go to the documentation of this file.
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/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>
30#include <rtl/ustring.hxx>
31#include <vcl/svapp.hxx>
32
33#include "lookupcolorname.hxx"
34#include <unordered_map>
35
36namespace
37{
38class ColorNameMap
39{
40public:
41 ColorNameMap();
42 ColorNameMap(const ColorNameMap&) = delete;
43 ColorNameMap& operator=(const ColorNameMap&) = delete;
44
45 OUString lookUp(tools::Long color) const;
46
47private:
48 typedef std::unordered_map<tools::Long, OUString> Map;
49
50 Map map_;
51};
52
53ColorNameMap::ColorNameMap()
54{
55 css::uno::Sequence<OUString> aNames;
56 css::uno::Reference<css::container::XNameAccess> xNA;
57
58 try
59 {
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());
63
64 // Get list of color names in order to iterate over the color table.
65
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();
71 }
72 catch (css::uno::RuntimeException const&)
73 {
74 // When an exception occurred then we have an empty name sequence
75 // and the loop below is not entered.
76 }
77
78 // Fill the map to convert from numerical color values to names.
79 if (!xNA.is())
80 return;
81
82 for (const auto& rName : std::as_const(aNames))
83 {
84 // Get the numerical value for the i-th color name.
85 try
86 {
87 css::uno::Any aColor = xNA->getByName(rName);
88 tools::Long nColor = 0;
89 aColor >>= nColor;
90 map_[nColor] = rName;
91 }
92 catch (css::uno::RuntimeException const&)
93 {
94 // Ignore the exception: the color who lead to the exception
95 // is not included into the map.
96 }
97 }
98}
99
100OUString ColorNameMap::lookUp(tools::Long color) const
101{
102 Map::const_iterator i(map_.find(color));
103 if (i != map_.end())
104 {
105 return i->second;
106 }
107 // Did not find the given color; return its RGB tuple representation:
108 return "#" + OUString::number(color, 16);
109}
110}
111
112namespace accessibility
113{
115{
116 static ColorNameMap theColorNameMap;
117 return theColorNameMap.lookUp(color);
118}
119}
120
121/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString lookUpColorName(tools::Long color)
This is a color name lookup targeted to be used by the accessibility <type>DescriptionGenerator</type...
Reference< XComponentContext > getProcessComponentContext()
int i
long Long
std::map< OUString, rtl::Reference< Entity > > const & map_