File indexing completed on 2024-04-28 16:49:37

0001 /*
0002  * Copyright 2018  Michail Vourlakos <mvourlakos@gmail.com>
0003  *
0004  * This file is part of Latte-Dock
0005  *
0006  * Latte-Dock is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License as
0008  * published by the Free Software Foundation; either version 2 of
0009  * the License, or (at your option) any later version.
0010  *
0011  * Latte-Dock is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0018  *
0019  */
0020 
0021 #include "commontools.h"
0022 
0023 // Qt
0024 #include <QFileInfo>
0025 #include <QStandardPaths>
0026 #include <QtMath>
0027 
0028 namespace Latte {
0029 
0030 float colorBrightness(QColor color)
0031 {
0032     return colorBrightness(color.red(), color.green(), color.blue());
0033 }
0034 
0035 float colorBrightness(QRgb rgb)
0036 {
0037     return colorBrightness(qRed(rgb), qGreen(rgb), qBlue(rgb));
0038 }
0039 
0040 float colorBrightness(float r, float g, float b)
0041 {
0042     float brightness = (r * 299 + g * 587 + b * 114) / 1000;
0043 
0044     return brightness;
0045 }
0046 
0047 
0048 float colorLumina(QRgb rgb)
0049 {
0050     float r = (float)(qRed(rgb)) / 255;
0051     float g = (float)(qGreen(rgb)) / 255;
0052     float b = (float)(qBlue(rgb)) / 255;
0053 
0054     return colorLumina(r, g, b);
0055 }
0056 
0057 float colorLumina(QColor color)
0058 {
0059     return colorLumina(color.redF(), color.greenF(), color.blueF());
0060 }
0061 
0062 float colorLumina(float r, float g, float b)
0063 {
0064     // formula for luminance according to:
0065     // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
0066 
0067     float rS = (r <= 0.03928 ? r / 12.92 : qPow(((r + 0.055) / 1.055), 2.4));
0068     float gS = (g <= 0.03928 ? g / 12.92 : qPow(((g + 0.055) / 1.055), 2.4));
0069     float bS = (b <= 0.03928 ? b / 12.92 : qPow(((b + 0.055) / 1.055), 2.4));
0070 
0071     float luminosity = 0.2126 * rS + 0.7152 * gS + 0.0722 * bS;
0072 
0073     return luminosity;
0074 }
0075 
0076 QString standardPath(QString subPath, bool localfirst)
0077 {
0078     QStringList paths = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
0079 
0080     if (localfirst) {
0081         for (const auto &pt : paths) {
0082             QString ptF = pt + "/" +subPath;
0083             if (QFileInfo(ptF).exists()) {
0084                 return ptF;
0085             }
0086         }
0087     } else {
0088         for (int i=paths.count()-1; i>=0; i--) {
0089             QString ptF = paths[i] + "/" +subPath;
0090             if (QFileInfo(ptF).exists()) {
0091                 return ptF;
0092             }
0093         }
0094     }
0095 
0096     //! in any case that above fails
0097     if (QFileInfo("/usr/share/"+subPath).exists()) {
0098         return "/usr/share/"+subPath;
0099     }
0100 
0101     return "";
0102 }
0103 
0104 }