File indexing completed on 2025-01-05 03:29:15

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2020 Carl Schwan <carl@carlschwan.eu>
0003  * SPDX-FileCopyrightText: (C) 2022 Vlad Rakhmanin <vladimir.rakhmanin@ucdconnect.ie>
0004  *
0005  * SPDX-License-Identifier: GPL-3.0-or-later
0006  */
0007 
0008 #pragma once
0009 
0010 #include <QColor>
0011 #include <QImage>
0012 #include <QObject>
0013 
0014 /**
0015  * @brief Main class that expose all the value to the QML.
0016  */
0017 class Kontrast : public QObject
0018 {
0019     Q_OBJECT
0020 
0021     Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY textColorChanged)
0022 
0023     Q_PROPERTY(int textHue READ textHue WRITE setTextHue NOTIFY textColorChanged)
0024 
0025     Q_PROPERTY(int textSaturation READ textSaturation WRITE setTextSaturation NOTIFY textColorChanged)
0026 
0027     Q_PROPERTY(int textLightness READ textLightness WRITE setTextLightness NOTIFY textColorChanged)
0028 
0029     Q_PROPERTY(int fontSize READ fontSize WRITE setFontSize NOTIFY fontSizeChanged)
0030 
0031     Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor NOTIFY backgroundColorChanged)
0032 
0033     Q_PROPERTY(int backgroundHue READ backgroundHue WRITE setBackgroundHue NOTIFY backgroundColorChanged)
0034 
0035     Q_PROPERTY(int backgroundSaturation READ backgroundSaturation WRITE setBackgroundSaturation NOTIFY backgroundColorChanged)
0036 
0037     Q_PROPERTY(QString fontSizeLabel READ getFontSizeQualityLabel NOTIFY fontSizeChanged)
0038 
0039     Q_PROPERTY(int backgroundLightness READ backgroundLightness WRITE setBackgroundLightness NOTIFY backgroundColorChanged)
0040 
0041     Q_PROPERTY(qreal contrast READ contrast NOTIFY contrastChanged)
0042 
0043     Q_PROPERTY(QColor displayTextColor READ displayTextColor NOTIFY contrastChanged)
0044 
0045     Q_PROPERTY(QColor grabbedColor READ grabbedColor NOTIFY grabbedColorChanged)
0046 
0047 public:
0048     explicit Kontrast(QObject *parent = nullptr);
0049     ~Kontrast() override = default;
0050 
0051     enum Quality { Bad, Good, Perfect };
0052 
0053     Q_ENUM(Quality)
0054 
0055     struct ContrastQualities {
0056         Quality small;
0057         Quality medium;
0058         Quality large;
0059     };
0060 
0061     struct ColorRGB {
0062         double red;
0063         double green;
0064         double blue;
0065     };
0066 
0067     QColor textColor() const;
0068     void setTextColor(const QColor textColor);
0069 
0070     int textHue() const;
0071     void setTextHue(int hue);
0072 
0073     int textSaturation() const;
0074     void setTextSaturation(int saturation);
0075 
0076     int textLightness() const;
0077     void setTextLightness(int lightness);
0078 
0079     int fontSize() const;
0080     void setFontSize(int fontSize);
0081 
0082     QColor backgroundColor() const;
0083     void setBackgroundColor(const QColor backgroundColor);
0084 
0085     int backgroundHue() const;
0086     void setBackgroundHue(int hue);
0087 
0088     int backgroundSaturation() const;
0089     void setBackgroundSaturation(int saturation);
0090 
0091     int backgroundLightness() const;
0092     void setBackgroundLightness(int lightness);
0093 
0094     qreal contrast() const;
0095 
0096     QColor displayTextColor() const;
0097 
0098     QColor grabbedColor() const;
0099 
0100     QString getFontSizeQualityLabel();
0101 
0102     Q_INVOKABLE void random();
0103     Q_INVOKABLE void reverse();
0104     Q_INVOKABLE void grabColor();
0105     Q_INVOKABLE QColor pixelAt(const QImage &image, int x, int y) const;
0106 
0107 private Q_SLOTS:
0108     void gotColorResponse(uint response, const QVariantMap &results);
0109 
0110 Q_SIGNALS:
0111     void textColorChanged();
0112     void backgroundColorChanged();
0113     void contrastChanged();
0114     void grabbedColorChanged();
0115     void fontSizeChanged();
0116 
0117 private:
0118     QColor m_textColor;
0119     QColor m_backgroundColor;
0120     QColor m_grabbedColor;
0121     int m_fontSize;
0122 
0123     ContrastQualities getContrastQualities() const;
0124 };
0125 
0126 Q_DECLARE_METATYPE(Kontrast::ColorRGB)