| 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 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | QQuickFusionStyle::QQuickFusionStyle(QObject *parent) |
| 18 | : QObject(parent) |
| 19 | { |
| 20 | connect(sender: QGuiApplication::styleHints()->accessibility(), signal: &QAccessibilityHints::contrastPreferenceChanged, context: this, slot: &QQuickFusionStyle::highContrastChanged); |
| 21 | } |
| 22 | |
| 23 | QColor QQuickFusionStyle::lightShade() |
| 24 | { |
| 25 | return QColor(255, 255, 255, 90); |
| 26 | } |
| 27 | |
| 28 | QColor QQuickFusionStyle::darkShade() |
| 29 | { |
| 30 | return QColor(0, 0, 0, 60); |
| 31 | } |
| 32 | |
| 33 | QColor QQuickFusionStyle::topShadow() |
| 34 | { |
| 35 | return QColor(0, 0, 0, 18); |
| 36 | } |
| 37 | |
| 38 | QColor QQuickFusionStyle::innerContrastLine() |
| 39 | { |
| 40 | return QColor(255, 255, 255, 30); |
| 41 | } |
| 42 | |
| 43 | bool QQuickFusionStyle::isHighContrast() |
| 44 | { |
| 45 | return QGuiApplication::styleHints()->accessibility()->contrastPreference() == Qt::ContrastPreference::HighContrast; |
| 46 | } |
| 47 | |
| 48 | QColor QQuickFusionStyle::highlight(QQuickPalette *palette) |
| 49 | { |
| 50 | return palette->highlight(); |
| 51 | } |
| 52 | |
| 53 | QColor QQuickFusionStyle::highlightedText(QQuickPalette *palette) |
| 54 | { |
| 55 | return palette->highlightedText(); |
| 56 | } |
| 57 | |
| 58 | QColor QQuickFusionStyle::outline(QQuickPalette *palette) |
| 59 | { |
| 60 | return isHighContrast() ? palette->windowText() : palette->window().darker(f: 140); |
| 61 | } |
| 62 | |
| 63 | QColor 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 | |
| 80 | QColor QQuickFusionStyle::tabFrameColor(QQuickPalette *palette) |
| 81 | { |
| 82 | return buttonColor(palette).lighter(f: 104); |
| 83 | } |
| 84 | |
| 85 | QColor 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 | |
| 101 | QColor 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 | |
| 107 | QColor QQuickFusionStyle::gradientStart(const QColor &baseColor) |
| 108 | { |
| 109 | return baseColor.lighter(f: 124); |
| 110 | } |
| 111 | |
| 112 | QColor QQuickFusionStyle::gradientStop(const QColor &baseColor) |
| 113 | { |
| 114 | return baseColor.lighter(f: 102); |
| 115 | } |
| 116 | |
| 117 | QColor 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 | |
| 128 | QColor 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 | |
| 137 | QT_END_NAMESPACE |
| 138 | |
| 139 | #include "moc_qquickfusionstyle_p.cpp" |
| 140 | |