Fix: Even smarter secondHand color picker.

This commit is contained in:
Corey Woodworth
2025-10-30 12:19:40 -04:00
parent b7ff9e73e4
commit fba4bf6b74
2 changed files with 36 additions and 30 deletions
+1 -1
View File
@@ -7,7 +7,6 @@ Item {
property color backgroundColor: Color.mPrimary property color backgroundColor: Color.mPrimary
property color clockColor: Color.mOnPrimary property color clockColor: Color.mOnPrimary
property color secondHandColor: Color.mError property color secondHandColor: Color.mError
property real markAlpha: 0.7
anchors.fill: parent anchors.fill: parent
Canvas { Canvas {
@@ -19,6 +18,7 @@ Item {
property int seconds: now.getSeconds() property int seconds: now.getSeconds()
onPaint: { onPaint: {
const markAlpha = 0.7
var ctx = getContext("2d") var ctx = getContext("2d")
ctx.reset() ctx.reset()
ctx.translate(width / 2, height / 2) ctx.translate(width / 2, height / 2)
+35 -29
View File
@@ -5,46 +5,57 @@ import Quickshell
Item { Item {
id: clockRoot id: clockRoot
property var now property var now
// Default colors
property color backgroundColor: Color.mPrimary property color backgroundColor: Color.mPrimary
property color clockColor: Color.mOnPrimary property color clockColor: Color.mOnPrimary
readonly property real markAlpha: 0.7 // alpha value of hour markers in AnalogClock
function getRelativeLuminance(color) {
return 0.2126 * color.r + 0.7152 * color.g + 0.0722 * color.b
}
function getContrastRatio(color1, color2) {
var L1 = getRelativeLuminance(color1)
var L2 = getRelativeLuminance(color2)
if (L1 > L2) {
return (L1 + 0.05) / (L2 + 0.05)
} else {
return (L2 + 0.05) / (L1 + 0.05)
}
}
property color secondHandColor: { property color secondHandColor: {
var defaultColor = Color.mError var defaultColor = Color.mError
var backgroundL = backgroundColor.hslLightness var bestContrast = 1.0 // A contrast of 1.0 is "no contrast".
var hourMarkL
if (Settings.data.location.analogClockInCalendar) {
hourMarkL = (clockColor.hslLightness * markAlpha) + (backgroundL * (1.0 - markAlpha))
} else {
hourMarkL = backgroundL
}
var bestWorstContrast = -1
var bestColor = defaultColor var bestColor = defaultColor
var candidates = [Color.mSecondary,
Color.mTertiary,
Color.mError,
]
var candidates = [Color.mSecondary, Color.mTertiary, Color.mError] const minContrast = 1.149
for (var i = 0; i < candidates.length; i++) { for (var i = 0; i < candidates.length; i++) {
var candidateColor = candidates[i] var candidate = candidates[i]
var candidateL = candidateColor.hslLightness var contrastClock = getContrastRatio(candidate, clockColor)
if (contrastClock < minContrast) {
continue // This color is terrible, don't even consider it.
}
var contrastBg = getContrastRatio(candidate, backgroundColor)
if (contrastBg < minContrast) {
continue // This color is terrible, don't even consider it.
}
var diffBackground = Math.abs(backgroundL - candidateL) var currentWorstContrast = Math.min(contrastBg, contrastClock)
var diffHourMark = Math.abs(hourMarkL - candidateL)
var currentWorstContrast = Math.min(diffBackground, diffHourMark) if (currentWorstContrast > bestContrast) {
bestContrast = currentWorstContrast
if (currentWorstContrast > bestWorstContrast) { bestColor = candidate
bestWorstContrast = currentWorstContrast
bestColor = candidateColor
} }
} }
return bestColor return bestColor
} }
property color progressColor: clockRoot.secondHandColor property color progressColor: clockRoot.secondHandColor
height: Math.round((Style.fontSizeXXXL * 1.9) / 2 * Style.uiScaleRatio) * 2 height: Math.round((Style.fontSizeXXXL * 1.9) / 2 * Style.uiScaleRatio) * 2
@@ -76,11 +87,6 @@ Item {
return clockRoot.progressColor return clockRoot.progressColor
}) })
} }
if (item.hasOwnProperty("markAlpha")) {
item.markAlpha = Qt.binding(function () {
return clockRoot.markAlpha
})
}
} }
} }
} }