Skip to content

Commit 065eb4b

Browse files
committed
Make "Click to set color" text either black or white, whichever is more contrasting
1 parent 1ad670d commit 065eb4b

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

ui/randomizer_window.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import string
1515
import struct
1616
import base64
17+
import colorsys
1718

1819
import yaml
1920
try:
@@ -688,8 +689,21 @@ def set_color(self, option_name, color, update_preview=True):
688689
color_button.setStyleSheet("")
689690
hex_input.setText("")
690691
else:
691-
color_button.setStyleSheet("background-color: rgb(%d, %d, %d)" % tuple(color))
692692
hex_input.setText("%02X%02X%02X" % tuple(color))
693+
694+
r, g, b = color
695+
696+
# Depending on the value of the background color of the button, we need to make the text color either black or white for contrast.
697+
h, s, v = colorsys.rgb_to_hsv(r/255, g/255, b/255)
698+
if v > 0.5:
699+
text_color = (0, 0, 0)
700+
else:
701+
text_color = (255, 255, 255)
702+
703+
color_button.setStyleSheet(
704+
"background-color: rgb(%d, %d, %d);" % (r, g, b) + \
705+
"color: rgb(%d, %d, %d);" % text_color,
706+
)
693707

694708
if update_preview:
695709
self.update_model_preview()

0 commit comments

Comments
 (0)