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> 29 #include <comphelper/processfactory.hxx> 30 #include <rtl/ustring.hxx> 31 #include <vcl/svapp.hxx> 32 33 #include "lookupcolorname.hxx" 34 #include <unordered_map> 35 36 namespace 37 { 38 class ColorNameMap 39 { 40 public: 41 ColorNameMap(); 42 ColorNameMap(const ColorNameMap&) = delete; 43 ColorNameMap& operator=(const ColorNameMap&) = delete; 44 45 OUString lookUp(tools::Long color) const; 46 47 private: 48 typedef std::unordered_map<tools::Long, OUString> Map; 49 50 Map map_; 51 }; 52 ColorNameMap()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 lookUp(tools::Long color) const100OUString 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 struct theColorNameMap : public rtl::Static<ColorNameMap, theColorNameMap> 112 { 113 }; 114 } 115 116 namespace accessibility 117 { lookUpColorName(tools::Long color)118OUString lookUpColorName(tools::Long color) { return theColorNameMap::get().lookUp(color); } 119 } 120 121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 122