Skip to content

Commit e852346

Browse files
authored
ADD: color degrade when ON/OFF (#120)
1 parent 8e48a21 commit e852346

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

BlocksScreen/lib/utils/toggleAnimatedButton.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ def __init__(self, parent) -> None:
3636
self.icon_pixmap: QtGui.QPixmap = QtGui.QPixmap()
3737
self._backgroundColor: QtGui.QColor = QtGui.QColor(223, 223, 223)
3838
self._handleColor: QtGui.QColor = QtGui.QColor(255, 100, 10)
39+
40+
self._handleONcolor: QtGui.QColor = QtGui.QColor(0, 200, 0)
41+
self._handleOFFcolor: QtGui.QColor = QtGui.QColor(200, 0, 0)
42+
3943
self.disable_bg_color: QtGui.QColor = QtGui.QColor("#A9A9A9")
4044
self.disable_handle_color: QtGui.QColor = QtGui.QColor("#666666")
4145
self._state = ToggleAnimatedButton.State.OFF
@@ -207,6 +211,32 @@ def paintEvent(self, a0: QtGui.QPaintEvent) -> None:
207211
painter.setBackgroundMode(QtCore.Qt.BGMode.TransparentMode)
208212
painter.setRenderHint(painter.RenderHint.LosslessImageRendering)
209213

214+
rect_norm = _rect.toRectF().normalized()
215+
min_x = rect_norm.x()
216+
max_x = rect_norm.x() + rect_norm.width() - rect_norm.height() * 0.80
217+
progress = (self._handle_position - min_x) / (max_x - min_x)
218+
progress = max(0.0, min(1.0, progress))
219+
220+
# Inline color interpolation (no separate functions)
221+
r = (
222+
self._handleOFFcolor.red()
223+
+ (self._handleONcolor.red() - self._handleOFFcolor.red()) * progress
224+
)
225+
g = (
226+
self._handleOFFcolor.green()
227+
+ (self._handleONcolor.green() - self._handleOFFcolor.green()) * progress
228+
)
229+
b = (
230+
self._handleOFFcolor.blue()
231+
+ (self._handleONcolor.blue() - self._handleOFFcolor.blue()) * progress
232+
)
233+
a = (
234+
self._handleOFFcolor.alpha()
235+
+ (self._handleONcolor.alpha() - self._handleOFFcolor.alpha()) * progress
236+
)
237+
238+
self.handleColor = QtGui.QColor(int(r), int(g), int(b), int(a))
239+
210240
painter.fillPath(
211241
self.trailPath,
212242
bg_color if self.isEnabled() else self.disable_bg_color,

0 commit comments

Comments
 (0)