File indexing completed on 2024-04-14 05:24:53

0001 /*
0002     SPDX-FileCopyrightText: 2020 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "tools.h"
0007 
0008 // Qt
0009 #include <QtMath>
0010 
0011 namespace Latte{
0012 
0013 Tools::Tools(QObject *parent)
0014     : QObject(parent)
0015 {
0016 }
0017 
0018 float Tools::colorBrightness(QColor color)
0019 {
0020     return colorBrightness(color.red(), color.green(), color.blue());
0021 }
0022 
0023 float Tools::colorBrightness(QRgb rgb)
0024 {
0025     return colorBrightness(qRed(rgb), qGreen(rgb), qBlue(rgb));
0026 }
0027 
0028 float Tools::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 float Tools::colorLumina(QRgb rgb)
0036 {
0037     float r = (float)(qRed(rgb)) / 255;
0038     float g = (float)(qGreen(rgb)) / 255;
0039     float b = (float)(qBlue(rgb)) / 255;
0040 
0041     return colorLumina(r, g, b);
0042 }
0043 
0044 float Tools::colorLumina(QColor color)
0045 {
0046     return colorLumina(color.redF(), color.greenF(), color.blueF());
0047 }
0048 
0049 float Tools::colorLumina(float r, float g, float b)
0050 {
0051     // formula for luminance according to:
0052     // https://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
0053 
0054     float rS = (r <= 0.03928 ? r / 12.92 : qPow(((r + 0.055) / 1.055), 2.4));
0055     float gS = (g <= 0.03928 ? g / 12.92 : qPow(((g + 0.055) / 1.055), 2.4));
0056     float bS = (b <= 0.03928 ? b / 12.92 : qPow(((b + 0.055) / 1.055), 2.4));
0057 
0058     float luminosity = 0.2126 * rS + 0.7152 * gS + 0.0722 * bS;
0059 
0060     return luminosity;
0061 }
0062 
0063 }