diff --git a/src/Qt-Color-Widgets/src/color_wheel.cpp b/src/Qt-Color-Widgets/src/color_wheel.cpp index f925d139..53aa0cfe 100644 --- a/src/Qt-Color-Widgets/src/color_wheel.cpp +++ b/src/Qt-Color-Widgets/src/color_wheel.cpp @@ -49,7 +49,7 @@ private: public: qreal hue, sat, val; - bool backgroundIsDark; + qreal bgBrightness; unsigned int wheel_width; MouseStatus mouse_status; QPixmap hue_ring; @@ -65,8 +65,8 @@ public: display_flags(FLAGS_DEFAULT), color_from(&QColor::fromHsvF), rainbow_from_hue(&detail::rainbow_hsv) { - qreal backgroundValue = widget->palette().background().color().valueF(); - backgroundIsDark = backgroundValue < 0.5; + QColor bgColor = widget->palette().background().color(); + bgBrightness = color_widgets::detail::color_lumaF(bgColor); } /// Calculate outer wheel radius from idget center @@ -312,7 +312,7 @@ void ColorWheel::paintEvent(QPaintEvent * ) painter.drawPixmap(-p->outer_radius(), -p->outer_radius(), p->hue_ring); // hue selector - QColor penColor = p->backgroundIsDark ? Qt::white : Qt::black; + QColor penColor = p->bgBrightness < 0.6 ? Qt::white : Qt::black; painter.setPen(QPen(penColor,3)); painter.setBrush(Qt::NoBrush); QLineF ray(0, 0, p->outer_radius(), 0); @@ -358,14 +358,16 @@ void ColorWheel::paintEvent(QPaintEvent * ) // lum-sat selector // we define the color of the selecto based on the background color of the widget // in order to improve the contrast - if (p->backgroundIsDark) + qreal colorBrightness = color_widgets::detail::color_lumaF(color()); + if (p->bgBrightness < 0.6) // dark theme { - bool isWhite = (p->val < 0.65 || p->sat > 0.43); + bool isWhite = (colorBrightness < 0.7); painter.setPen(QPen(isWhite ? Qt::white : Qt::black, 3)); } - else + else // light theme { - painter.setPen(QPen(p->val > 0.5 ? Qt::black : Qt::white, 3)); + bool isWhite = (colorBrightness < 0.4 && p->val < 0.3); + painter.setPen(QPen(isWhite ? Qt::white : Qt::black, 3)); } painter.setBrush(Qt::NoBrush); painter.drawEllipse(selector_position, selector_radius, selector_radius); diff --git a/src/capture/capturebutton.cpp b/src/capture/capturebutton.cpp index 3d081d8f..39be9512 100644 --- a/src/capture/capturebutton.cpp +++ b/src/capture/capturebutton.cpp @@ -24,12 +24,35 @@ #include #include #include - // Button represents a single button of the capture widget, it can enable // multiple functionality. namespace { const int BUTTON_SIZE = 30; + + inline qreal getColorLuma(const QColor &c) { + return 0.30 * c.redF() + 0.59 * c.greenF() + 0.11 * c.blueF(); + } + + inline int constrain(const int x, const int min, const int max) { + if (x > max) { + return max; + } else if (x < min) { + return min; + } else { + return x; + } + } + + QColor getContrastColor(const QColor &c) { + bool isWhite = CaptureButton::iconIsWhiteByColor(c); + int change = isWhite ? 30 : -45; + + return QColor(constrain(c.red() + change, 0, 255), + constrain(c.green() + change, 0, 255), + constrain(c.blue() + change, 0, 255)); + } + } CaptureButton::CaptureButton(const ButtonType t, QWidget *parent) : QPushButton(parent), @@ -74,11 +97,8 @@ QString CaptureButton::getGlobalStyleSheet() { "CaptureButton:pressed:!hover { " "background-color: %1; }"; // define color when mouse is hovering - QColor contrast(mainColor.darker(120)); - if (mainColor.value() < m_colorValueLimit || - mainColor.saturation() > m_colorSaturationLimit) { - contrast = mainColor.lighter(140); - } + QColor contrast = getContrastColor(m_mainColor); + // foreground color QString color = iconIsWhiteByColor(mainColor) ? "white" : "black"; @@ -93,11 +113,7 @@ QString CaptureButton::getStyleSheet() const { "CaptureButton:pressed:!hover { " "background-color: %1; }"; // define color when mouse is hovering - QColor contrast(m_mainColor.darker(120)); - if (m_mainColor.value() < m_colorValueLimit || - m_mainColor.saturation() > m_colorSaturationLimit) { - contrast = m_mainColor.lighter(140); - } + QColor contrast = getContrastColor(m_mainColor); // foreground color QString color = iconIsWhiteByColor(m_mainColor) ? "white" : "black"; @@ -138,6 +154,7 @@ void CaptureButton::mousePressEvent(QMouseEvent *) { Q_EMIT pressed(); } + void CaptureButton::animatedShow() { show(); emergeAnimation->start(); @@ -164,8 +181,7 @@ size_t CaptureButton::getButtonBaseSize() { bool CaptureButton::iconIsWhiteByColor(const QColor &c) { bool isWhite = false; - if (c.value() < m_colorValueLimit || - c.saturation() > m_colorSaturationLimit) { + if (getColorLuma(c) <= 0.60) { isWhite = true; } return isWhite; diff --git a/src/capture/capturebutton.h b/src/capture/capturebutton.h index 6f964f0b..3f61e011 100644 --- a/src/capture/capturebutton.h +++ b/src/capture/capturebutton.h @@ -87,9 +87,6 @@ private: ButtonType m_buttonType; bool m_pressed; - static const int m_colorValueLimit = 166; - static const int m_colorSaturationLimit = 110; - QPropertyAnimation *emergeAnimation; static QColor m_mainColor;