Mercurial > hg > tilerswift
changeset 77:cc5ffdcf7108
colours: add is_dark function
I realise having luminance contrast is more widely useful, so I'm
moving this function to a common location.
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Tue, 10 Sep 2019 13:16:38 -0400 |
parents | f311c18ef557 |
children | 9922cde5ad6c |
files | colors.py tilerswift |
diffstat | 2 files changed, 13 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
--- a/colors.py +++ b/colors.py @@ -77,6 +77,13 @@ } +def is_dark(qt_colour): + r, g, b = qt_colour.red(), qt_colour.green(), qt_colour.blue() + luminance = (0.2126*r + 0.7152*g + 0.0722*b)/256 + + return luminance < 0.5 + + def palette_to_qt(palette): return [ QG.QColor(0, 0, 0, 0).rgba() if color_idx is None
--- a/tilerswift +++ b/tilerswift @@ -3,7 +3,10 @@ from PyQt5 import QtCore as QC, QtGui as QG, QtWidgets as QW -from colors import NES_PALETTE, TILE_PALETTES, palette_to_qt +from colors import ( + NES_PALETTE, TILE_PALETTES, is_dark, palette_to_qt, + widget_icon_path +) class NES_ROM(object): @@ -368,11 +371,9 @@ self.setText(f"{colour_idx:0{2}X}") bgcolour = NES_PALETTE[colour_idx] qt_colour = QG.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' - bordercolour = "#444444" if r == g == b == 0 else qt_colour.darker().name() + textcolour = 'white' if is_dark(qt_colour) else 'black' + bordercolour = "#444444" if qt_colour.rgb() == 0 else qt_colour.darker().name() self.setStyleSheet( f''' QPushButton {{