changeset 48:fa5b36acf03f

ColourButton: label each colour with its hex index The luminance is computed via the formula found here: https://en.wikipedia.org/wiki/Relative_luminance
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Wed, 04 Sep 2019 10:14:04 -0400
parents 493bfe6b2b90
children 5ca986354475
files tilerswift
diffstat 1 files changed, 8 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/tilerswift
+++ b/tilerswift
@@ -226,7 +226,14 @@
 
     def set_colour(self, colour_idx):
         self.colour_idx = colour_idx
-        self.setStyleSheet("background-color: %s;" % NES_PALETTE[colour_idx])
+        self.setText(f"{colour_idx:0{2}X}")
+
+        bgcolour = NES_PALETTE[colour_idx]
+        qt_colour = QtGui.QColor(bgcolour)
+        [r, g, b] = qt_colour.red(), qt_colour.green(), qt_colour.blue()
+        luminance = (0.2126*r + 0.7152*g + 0.0722*b)/256
+        textcolour = 'white' if luminance < 0.5 else 'black'
+        self.setStyleSheet(f"color: {textcolour}; background-color: {bgcolour}")
 
 
 class PaletteButton(ColourButton):