File indexing completed on 2024-05-19 05:42:18
0001 // ct_lvtqtc_iconhelpers.cpp -*-C++-*- 0002 0003 /* 0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk> 0005 // SPDX-License-Identifier: Apache-2.0 0006 // 0007 // Licensed under the Apache License, Version 2.0 (the "License"); 0008 // you may not use this file except in compliance with the License. 0009 // You may obtain a copy of the License at 0010 // 0011 // http://www.apache.org/licenses/LICENSE-2.0 0012 // 0013 // Unless required by applicable law or agreed to in writing, software 0014 // distributed under the License is distributed on an "AS IS" BASIS, 0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0016 // See the License for the specific language governing permissions and 0017 // limitations under the License. 0018 */ 0019 #include "ct_lvtqtc_iconhelpers.h" 0020 0021 #include <QApplication> 0022 #include <QColor> 0023 #include <QImage> 0024 #include <QPainter> 0025 #include <QPalette> 0026 #include <QPixmap> 0027 0028 QIcon IconHelpers::iconFrom(const QString& resource) 0029 { 0030 if (hasBrightBackground()) { 0031 return QIcon(resource); 0032 } 0033 0034 auto img = QImage(resource); 0035 img.invertPixels(); 0036 return QPixmap::fromImage(img); 0037 } 0038 0039 QIcon IconHelpers::iconFrom(const QIcon& icon) 0040 { 0041 if (hasBrightBackground()) { 0042 return icon; 0043 } 0044 0045 auto sizes = icon.availableSizes(); 0046 if (sizes.empty()) { 0047 return icon; 0048 } 0049 auto maximum = *std::max_element(sizes.begin(), sizes.end(), [](const QSize& a, const QSize& b) { 0050 return a.width() < b.width(); 0051 }); 0052 0053 auto img = QImage(icon.pixmap(maximum).toImage()); 0054 img.invertPixels(); 0055 return QPixmap::fromImage(img); 0056 } 0057 0058 bool IconHelpers::hasBrightBackground() 0059 { 0060 QPalette pal = qApp->palette(); 0061 const QColor hsvBackground = pal.window().color().toHsv(); 0062 return hsvBackground.value() > 100; 0063 }