mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 02:34:06 -04:00
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:
parent
6ca7282fd9
commit
4ea2bd2a90
@ -669,8 +669,7 @@ else:
|
|||||||
def is_dark_theme():
|
def is_dark_theme():
|
||||||
pal = QApplication.instance().palette()
|
pal = QApplication.instance().palette()
|
||||||
col = pal.color(pal.Window)
|
col = pal.color(pal.Window)
|
||||||
h, s, v, a = col.getHsvF()
|
return max(col.getRgb()[:3]) < 115
|
||||||
return v < 0.45
|
|
||||||
|
|
||||||
|
|
||||||
def choose_osx_app(window, name, title, default_dir='/Applications'):
|
def choose_osx_app(window, name, title, default_dir='/Applications'):
|
||||||
|
@ -133,6 +133,13 @@ static inline QByteArray detectDesktopEnvironment()
|
|||||||
return QByteArrayLiteral("UNKNOWN");
|
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 {
|
class CalibreStyle: public QProxyStyle {
|
||||||
private:
|
private:
|
||||||
QHash<int, QString> icon_map;
|
QHash<int, QString> icon_map;
|
||||||
@ -213,14 +220,14 @@ class CalibreStyle: public QProxyStyle {
|
|||||||
if (const QStyleOptionTabBarBase *tbb = qstyleoption_cast<const QStyleOptionTabBarBase *>(option)) {
|
if (const QStyleOptionTabBarBase *tbb = qstyleoption_cast<const QStyleOptionTabBarBase *>(option)) {
|
||||||
if (tbb->shape == QTabBar::RoundedNorth) {
|
if (tbb->shape == QTabBar::RoundedNorth) {
|
||||||
QColor bg = option->palette.color(QPalette::Window);
|
QColor bg = option->palette.color(QPalette::Window);
|
||||||
if (bg.valueF() < 0.45) return;
|
if (is_color_dark(bg)) return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break; // }}}
|
break; // }}}
|
||||||
|
|
||||||
case PE_IndicatorCheckBox: // {{{
|
case PE_IndicatorCheckBox: // {{{
|
||||||
// Fix color used to draw checkbox outline in dark mode
|
// 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);
|
baseStyle()->drawPrimitive(element, option, painter, widget);
|
||||||
painter->save();
|
painter->save();
|
||||||
painter->translate(0.5, 0.5);
|
painter->translate(0.5, 0.5);
|
||||||
@ -254,7 +261,7 @@ class CalibreStyle: public QProxyStyle {
|
|||||||
const int margin = 6;
|
const int margin = 6;
|
||||||
QColor bg = option->palette.color(QPalette::Window);
|
QColor bg = option->palette.color(QPalette::Window);
|
||||||
QColor first, second;
|
QColor first, second;
|
||||||
if (bg.valueF() < 0.45) {
|
if (is_color_dark(bg)) {
|
||||||
first = bg.darker(115);
|
first = bg.darker(115);
|
||||||
second = bg.lighter(115);
|
second = bg.lighter(115);
|
||||||
} else {
|
} else {
|
||||||
@ -311,7 +318,7 @@ class CalibreStyle: public QProxyStyle {
|
|||||||
QPalette::Text);
|
QPalette::Text);
|
||||||
w = menuItem->fontMetrics.horizontalAdvance(menuItem->text) + margin;
|
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));
|
else painter->setPen(QColor(0, 0, 0, 60).lighter(106));
|
||||||
bool reverse = menuItem->direction == Qt::RightToLeft;
|
bool reverse = menuItem->direction == Qt::RightToLeft;
|
||||||
painter->drawLine(menuItem->rect.left() + margin + (reverse ? 0 : w), menuItem->rect.center().y(),
|
painter->drawLine(menuItem->rect.left() + margin + (reverse ? 0 : w), menuItem->rect.center().y(),
|
||||||
|
@ -681,8 +681,9 @@ class View:
|
|||||||
bg_image_fade = 'transparent'
|
bg_image_fade = 'transparent'
|
||||||
cs = self.get_color_scheme(True)
|
cs = self.get_color_scheme(True)
|
||||||
fade = int(sd.get('background_image_fade'))
|
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)
|
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})'
|
bg_image_fade = f'rgba({rgba[0]}, {rgba[1]}, {rgba[2]}, {fade/100})'
|
||||||
return {
|
return {
|
||||||
'margin_left': 0 if name is self.book.manifest.title_page_name else sd.get('margin_left'),
|
'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'),
|
'read_mode': sd.get('read_mode'),
|
||||||
'columns_per_screen': sd.get('columns_per_screen'),
|
'columns_per_screen': sd.get('columns_per_screen'),
|
||||||
'color_scheme': cs,
|
'color_scheme': cs,
|
||||||
|
'is_dark_theme': is_dark_theme,
|
||||||
'bg_image_fade': bg_image_fade,
|
'bg_image_fade': bg_image_fade,
|
||||||
'base_font_size': sd.get('base_font_size'),
|
'base_font_size': sd.get('base_font_size'),
|
||||||
'user_stylesheet': sd.get('user_stylesheet'),
|
'user_stylesheet': sd.get('user_stylesheet'),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user