1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickfusionstyle_p.h"
5
6#include <QtGui/qcolor.h>
7#include <QtGui/qpalette.h>
8#include <QtGui/qstylehints.h>
9#include <QtGui/qaccessibilityhints.h>
10#include <QtGui/qpa/qplatformtheme.h>
11#include <QtGui/private/qguiapplication_p.h>
12
13#include <QtQuick/private/qquickpalette_p.h>
14
15QT_BEGIN_NAMESPACE
16
17QQuickFusionStyle::QQuickFusionStyle(QObject *parent)
18 : QObject(parent)
19{
20 connect(sender: QGuiApplication::styleHints()->accessibility(), signal: &QAccessibilityHints::contrastPreferenceChanged, context: this, slot: &QQuickFusionStyle::highContrastChanged);
21}
22
23QColor QQuickFusionStyle::lightShade()
24{
25 return QColor(255, 255, 255, 90);
26}
27
28QColor QQuickFusionStyle::darkShade()
29{
30 return QColor(0, 0, 0, 60);
31}
32
33QColor QQuickFusionStyle::topShadow()
34{
35 return QColor(0, 0, 0, 18);
36}
37
38QColor QQuickFusionStyle::innerContrastLine()
39{
40 return QColor(255, 255, 255, 30);
41}
42
43bool QQuickFusionStyle::isHighContrast()
44{
45 return QGuiApplication::styleHints()->accessibility()->contrastPreference() == Qt::ContrastPreference::HighContrast;
46}
47
48QColor QQuickFusionStyle::highlight(QQuickPalette *palette)
49{
50 return palette->highlight();
51}
52
53QColor QQuickFusionStyle::highlightedText(QQuickPalette *palette)
54{
55 return palette->highlightedText();
56}
57
58QColor QQuickFusionStyle::outline(QQuickPalette *palette)
59{
60 return isHighContrast() ? palette->windowText() : palette->window().darker(f: 140);
61}
62
63QColor QQuickFusionStyle::highlightedOutline(QQuickPalette *palette)
64{
65 if (isHighContrast()) {
66 if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Light) {
67 return highlight(palette).darker(f: 125);
68 } else {
69 QColor highlightedOutline = highlight(palette).toHsv();
70 highlightedOutline.setHsv(h: highlightedOutline.hsvHue(), s: highlightedOutline.hsvSaturation(), v: 255);
71 return highlightedOutline;
72 }
73 }
74 QColor highlightedOutline = highlight(palette).darker(f: 125).toHsv();
75 if (highlightedOutline.value() > 160)
76 highlightedOutline.setHsl(h: highlightedOutline.hue(), s: highlightedOutline.saturation(), l: 160);
77 return highlightedOutline;
78}
79
80QColor QQuickFusionStyle::tabFrameColor(QQuickPalette *palette)
81{
82 return buttonColor(palette).lighter(f: 104);
83}
84
85QColor QQuickFusionStyle::buttonColor(QQuickPalette *palette, bool highlighted, bool down, bool hovered)
86{
87 QColor buttonColor = palette->button();
88 int val = qGray(rgb: buttonColor.rgb());
89 buttonColor = buttonColor.lighter(f: 100 + qMax(a: 1, b: (180 - val)/6));
90 buttonColor = buttonColor.toHsv();
91 buttonColor.setHsv(h: buttonColor.hue(), s: int(buttonColor.saturation() * 0.75), v: buttonColor.value());
92 if (highlighted)
93 buttonColor = mergedColors(colorA: buttonColor, colorB: highlightedOutline(palette).lighter(f: 130), factor: 90);
94 if (!hovered)
95 buttonColor = buttonColor.darker(f: 104);
96 if (down)
97 buttonColor = buttonColor.darker(f: 110);
98 return buttonColor;
99}
100
101QColor QQuickFusionStyle::buttonOutline(QQuickPalette *palette, bool highlighted, bool enabled)
102{
103 QColor darkOutline = enabled && highlighted ? highlightedOutline(palette) : outline(palette);
104 return !enabled ? darkOutline.lighter(f: 115) : darkOutline;
105}
106
107QColor QQuickFusionStyle::gradientStart(const QColor &baseColor)
108{
109 return baseColor.lighter(f: 124);
110}
111
112QColor QQuickFusionStyle::gradientStop(const QColor &baseColor)
113{
114 return baseColor.lighter(f: 102);
115}
116
117QColor QQuickFusionStyle::mergedColors(const QColor &colorA, const QColor &colorB, int factor)
118{
119 const int maxFactor = 100;
120 const auto rgbColorB = colorB.toRgb();
121 QColor tmp = colorA.toRgb();
122 tmp.setRed((tmp.red() * factor) / maxFactor + (rgbColorB.red() * (maxFactor - factor)) / maxFactor);
123 tmp.setGreen((tmp.green() * factor) / maxFactor + (rgbColorB.green() * (maxFactor - factor)) / maxFactor);
124 tmp.setBlue((tmp.blue() * factor) / maxFactor + (rgbColorB.blue() * (maxFactor - factor)) / maxFactor);
125 return tmp;
126}
127
128QColor QQuickFusionStyle::grooveColor(QQuickPalette *palette)
129{
130 QColor color = buttonColor(palette).toHsv();
131 color.setHsv(h: color.hue(),
132 s: qMin(a: 255, b: color.saturation()),
133 v: qMin<int>(a: 255, b: color.value() * 0.9));
134 return color;
135}
136
137QT_END_NAMESPACE
138
139#include "moc_qquickfusionstyle_p.cpp"
140

source code of qtdeclarative/src/quickcontrols/fusion/qquickfusionstyle.cpp