File indexing completed on 2024-10-13 10:49:32

0001 /*
0002     SPDX-FileCopyrightText: 2018 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 */
0006 
0007 #include "commontools.h"
0008 
0009 // Qt
0010 #include <QDir>
0011 #include <QFileInfo>
0012 #include <QStandardPaths>
0013 #include <QStringList>
0014 #include <QtMath>
0015 
0016 namespace Latte {
0017 
0018 float colorBrightness(QColor color)
0019 {
0020     return colorBrightness(color.red(), color.green(), color.blue());
0021 }
0022 
0023 float colorBrightness(QRgb rgb)
0024 {
0025     return colorBrightness(qRed(rgb), qGreen(rgb), qBlue(rgb));
0026 }
0027 
0028 float colorBrightness(float r, float g, float b)
0029 {
0030     float brightness = (r * 299 + g * 587 + b * 114) / 1000;
0031 
0032     return brightness;
0033 }
0034 
0035 
0036 float colorLumina(QRgb rgb)
0037 {
0038     float r = (float)(qRed(rgb)) / 255;
0039     float g = (float)(qGreen(rgb)) / 255;
0040     float b = (float)(qBlue(rgb)) / 255;
0041 
0042     return colorLumina(r, g, b);
0043 }
0044 
0045 float colorLumina(QColor color)
0046 {
0047     return colorLumina(color.redF(), color.greenF(), color.blueF());
0048 }
0049 
0050 float colorLumina(float r, float g, float b)
0051 {
0052     // formula for luminance according to:
0053     // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
0054 
0055     float rS = (r <= 0.03928 ? r / 12.92 : qPow(((r + 0.055) / 1.055), 2.4));
0056     float gS = (g <= 0.03928 ? g / 12.92 : qPow(((g + 0.055) / 1.055), 2.4));
0057     float bS = (b <= 0.03928 ? b / 12.92 : qPow(((b + 0.055) / 1.055), 2.4));
0058 
0059     float luminosity = 0.2126 * rS + 0.7152 * gS + 0.0722 * bS;
0060 
0061     return luminosity;
0062 }
0063 
0064 QString rectToString(const QRect &rect)
0065 {
0066     QString result;
0067     result += QString(QString::number(rect.x()) + ","  + QString::number(rect.y()));
0068     result += " ";
0069     result += QString(QString::number(rect.width()) + "x" + QString::number(rect.height()));
0070 
0071     return result;
0072 }
0073 
0074 QRect stringToRect(const QString &str)
0075 {
0076     QStringList parts = str.split(" ");
0077     QStringList pos = parts[0].split(",");
0078     QStringList size = parts[1].split("x");
0079     return QRect(pos[0].toInt(), pos[1].toInt(), size[0].toInt(), size[1].toInt());
0080 }
0081 
0082 QString standardPath(QString subPath, bool localfirst)
0083 {
0084     QStringList paths = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
0085 
0086     QString separator = subPath.startsWith("/") ? "" : "/";
0087 
0088     if (localfirst) {
0089         for (const auto &pt : paths) {
0090             QString ptF = pt + separator +subPath;
0091             if (QFileInfo(ptF).exists()) {
0092                 return ptF;
0093             }
0094         }
0095     } else {
0096         for (int i=paths.count()-1; i>=0; i--) {
0097             QString ptF = paths[i] + separator +subPath;
0098             if (QFileInfo(ptF).exists()) {
0099                 return ptF;
0100             }
0101         }
0102     }
0103 
0104     //! in any case that above fails
0105     if (QFileInfo("/usr/share" + separator + subPath).exists()) {
0106         return "/usr/share" + separator + subPath;
0107     }
0108 
0109     return "";
0110 }
0111 
0112 QString configPath()
0113 {
0114     QStringList configPaths = QStandardPaths::standardLocations(QStandardPaths::ConfigLocation);
0115 
0116     if (configPaths.count() == 0) {
0117         return QDir::homePath() + "/.config";
0118     }
0119 
0120     return configPaths[0];
0121 }
0122 
0123 
0124 }