Avoid un-needed conversion to hsv to check if a color is dark

The V is HSV is anyway defined as max(r, g, b)
This commit is contained in:
Kovid Goyal 2019-12-15 16:28:05 +05:30
parent 6ca7282fd9
commit 4ea2bd2a90
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 15 additions and 7 deletions

View File

@ -669,8 +669,7 @@ else:
def is_dark_theme():
pal = QApplication.instance().palette()
col = pal.color(pal.Window)
h, s, v, a = col.getHsvF()
return v < 0.45
return max(col.getRgb()[:3]) < 115
def choose_osx_app(window, name, title, default_dir='/Applications'):

View File

@ -133,6 +133,13 @@ static inline QByteArray detectDesktopEnvironment()
return QByteArrayLiteral("UNKNOWN");
}
static inline bool
is_color_dark(const QColor &col) {
int r, g, b;
col.getRgb(&r, &g, &b);
return r < 115 && g < 155 && b < 115;
}
class CalibreStyle: public QProxyStyle {
private:
QHash<int, QString> icon_map;
@ -213,14 +220,14 @@ class CalibreStyle: public QProxyStyle {
if (const QStyleOptionTabBarBase *tbb = qstyleoption_cast<const QStyleOptionTabBarBase *>(option)) {
if (tbb->shape == QTabBar::RoundedNorth) {
QColor bg = option->palette.color(QPalette::Window);
if (bg.valueF() < 0.45) return;
if (is_color_dark(bg)) return;
}
}
break; // }}}
case PE_IndicatorCheckBox: // {{{
// Fix color used to draw checkbox outline in dark mode
if (option->palette.color(QPalette::Window).valueF() < 0.45) {
if (is_color_dark(option->palette.color(QPalette::Window))) {
baseStyle()->drawPrimitive(element, option, painter, widget);
painter->save();
painter->translate(0.5, 0.5);
@ -254,7 +261,7 @@ class CalibreStyle: public QProxyStyle {
const int margin = 6;
QColor bg = option->palette.color(QPalette::Window);
QColor first, second;
if (bg.valueF() < 0.45) {
if (is_color_dark(bg)) {
first = bg.darker(115);
second = bg.lighter(115);
} else {
@ -311,7 +318,7 @@ class CalibreStyle: public QProxyStyle {
QPalette::Text);
w = menuItem->fontMetrics.horizontalAdvance(menuItem->text) + margin;
}
if (menuItem->palette.color(QPalette::Window).valueF() < 0.45) painter->setPen(Qt::gray);
if (is_color_dark(menuItem->palette.color(QPalette::Window))) painter->setPen(Qt::gray);
else painter->setPen(QColor(0, 0, 0, 60).lighter(106));
bool reverse = menuItem->direction == Qt::RightToLeft;
painter->drawLine(menuItem->rect.left() + margin + (reverse ? 0 : w), menuItem->rect.center().y(),

View File

@ -681,8 +681,9 @@ class View:
bg_image_fade = 'transparent'
cs = self.get_color_scheme(True)
fade = int(sd.get('background_image_fade'))
if self.iframe.style.backgroundImage is not 'none' and fade > 0:
rgba = cached_color_to_rgba(cs.background)
is_dark_theme = max(rgba[0], rgba[1], rgba[2]) < 115
if self.iframe.style.backgroundImage is not 'none' and fade > 0:
bg_image_fade = f'rgba({rgba[0]}, {rgba[1]}, {rgba[2]}, {fade/100})'
return {
'margin_left': 0 if name is self.book.manifest.title_page_name else sd.get('margin_left'),
@ -690,6 +691,7 @@ class View:
'read_mode': sd.get('read_mode'),
'columns_per_screen': sd.get('columns_per_screen'),
'color_scheme': cs,
'is_dark_theme': is_dark_theme,
'bg_image_fade': bg_image_fade,
'base_font_size': sd.get('base_font_size'),
'user_stylesheet': sd.get('user_stylesheet'),