[app] Fixed color inversion in night mode

This commit is contained in:
Slava Monich 2022-10-29 20:10:02 +03:00
parent 4cc749ff63
commit 34eff94867
3 changed files with 32 additions and 16 deletions

View file

@ -61,20 +61,21 @@ public:
static const Colors DEFAULT_COLORS; static const Colors DEFAULT_COLORS;
static const QString DEFAULT_SCHEME_ID; static const QString DEFAULT_SCHEME_ID;
Private(const Colors*); Private(const Colors*, bool);
~Private(); ~Private();
void updateSchemeId(); void updateSchemeId();
static QString generateSchemeId(const Colors*); static QString generateSchemeId(const Colors*, bool);
static QString defaultSchemeId() { return generateSchemeId(&DEFAULT_COLORS); } static QString defaultSchemeId() { return generateSchemeId(&DEFAULT_COLORS, false); }
static void parseRgb(QRgb*, const QString&); static void parseRgb(QRgb*, const QString&);
static uint rgb(QRgb); static uint rgb(QRgb);
public: public:
QAtomicInt iRef; QAtomicInt iRef;
Colors iColors;
QString iSchemeId; QString iSchemeId;
Colors iColors;
bool iInverted;
}; };
const BooksColorScheme::Private::Colors BooksColorScheme::Private::DEFAULT_COLORS = { const BooksColorScheme::Private::Colors BooksColorScheme::Private::DEFAULT_COLORS = {
@ -86,9 +87,11 @@ const QString BooksColorScheme::Private::DEFAULT_SCHEME_ID
(BooksColorScheme::Private::defaultSchemeId()); (BooksColorScheme::Private::defaultSchemeId());
BooksColorScheme::Private::Private( BooksColorScheme::Private::Private(
const Colors* aColors) : const Colors* aColors,
bool aInverted) :
iRef(1), iRef(1),
iColors(*aColors) iColors(*aColors),
iInverted(aInverted)
{ {
// Note: leaving iSchemeId empty. Caller must do updateSchemeId() // Note: leaving iSchemeId empty. Caller must do updateSchemeId()
} }
@ -99,13 +102,15 @@ BooksColorScheme::Private::~Private()
QString QString
BooksColorScheme::Private::generateSchemeId( BooksColorScheme::Private::generateSchemeId(
const Colors* aColors) const Colors* aColors,
bool aInverted)
{ {
QCryptographicHash md5(QCryptographicHash::Md5); QCryptographicHash md5(QCryptographicHash::Md5);
#define HASH_COLOR(colorName,ColorName,key,default) \ #define HASH_COLOR(colorName,ColorName,key,default) \
md5.addData((const char*)&aColors->MEMBER_VAR(ColorName), sizeof(aColors->MEMBER_VAR(ColorName))); md5.addData((const char*)&aColors->MEMBER_VAR(ColorName), sizeof(aColors->MEMBER_VAR(ColorName)));
BOOKS_COLORS(HASH_COLOR) BOOKS_COLORS(HASH_COLOR)
#undef HASH_COLOR #undef HASH_COLOR
md5.addData((const char*)&aInverted, sizeof(aInverted));
return QString(QLatin1String(md5.result().toHex())); return QString(QLatin1String(md5.result().toHex()));
} }
@ -128,7 +133,7 @@ inline
void void
BooksColorScheme::Private::updateSchemeId() BooksColorScheme::Private::updateSchemeId()
{ {
iSchemeId = generateSchemeId(&iColors); iSchemeId = generateSchemeId(&iColors, iInverted);
} }
void void
@ -201,7 +206,7 @@ BooksColorScheme::BooksColorScheme(
#undef PARSE_KEY #undef PARSE_KEY
if (!colors.isDefault()) { if (!colors.isDefault()) {
(iPrivate = new Private(&colors))->updateSchemeId(); (iPrivate = new Private(&colors, false))->updateSchemeId();
} }
} }
@ -221,6 +226,12 @@ BooksColorScheme::~BooksColorScheme()
} }
} }
bool
BooksColorScheme::isInverted() const
{
return iPrivate && iPrivate->iInverted;
}
const QString const QString
BooksColorScheme::schemeId() const BooksColorScheme::schemeId() const
{ {
@ -295,11 +306,11 @@ BooksColorScheme BooksColorScheme::with##ColorName(QRgb aColor) const { \
if (iPrivate) { \ if (iPrivate) { \
Private::Colors colors(iPrivate->iColors); \ Private::Colors colors(iPrivate->iColors); \
colors.MEMBER_VAR(ColorName) = aColor; \ colors.MEMBER_VAR(ColorName) = aColor; \
if (!colors.isDefault()) { \ if (isInverted() || !colors.isDefault()) { \
(scheme.iPrivate = new Private(&colors))->updateSchemeId(); \ (scheme.iPrivate = new Private(&colors, isInverted()))->updateSchemeId(); \
} \ } \
} else { \ } else { \
scheme.iPrivate = new Private(&Private::DEFAULT_COLORS); \ scheme.iPrivate = new Private(&Private::DEFAULT_COLORS, isInverted()); \
scheme.iPrivate->iColors.MEMBER_VAR(ColorName) = aColor; \ scheme.iPrivate->iColors.MEMBER_VAR(ColorName) = aColor; \
scheme.iPrivate->updateSchemeId(); \ scheme.iPrivate->updateSchemeId(); \
} \ } \
@ -315,8 +326,8 @@ BooksColorScheme::inverted() const
BooksColorScheme scheme; BooksColorScheme scheme;
Private::Colors colors = iPrivate ? iPrivate->iColors : Private::DEFAULT_COLORS; Private::Colors colors = iPrivate ? iPrivate->iColors : Private::DEFAULT_COLORS;
colors.invert(); colors.invert();
if (!colors.isDefault()) { if (!isInverted() || !colors.isDefault()) {
(scheme.iPrivate = new Private(&colors))->updateSchemeId(); (scheme.iPrivate = new Private(&colors, !isInverted()))->updateSchemeId();
} }
return scheme; return scheme;
} }
@ -329,8 +340,8 @@ BooksColorScheme::invertedWithSelectionBackground(
Private::Colors colors = iPrivate ? iPrivate->iColors : Private::DEFAULT_COLORS; Private::Colors colors = iPrivate ? iPrivate->iColors : Private::DEFAULT_COLORS;
colors.invert(); colors.invert();
colors.iSelectionBackground = aColor; colors.iSelectionBackground = aColor;
if (!colors.isDefault()) { if (!isInverted() || !colors.isDefault()) {
(scheme.iPrivate = new Private(&colors))->updateSchemeId(); (scheme.iPrivate = new Private(&colors, !isInverted()))->updateSchemeId();
} }
return scheme; return scheme;
} }

View file

@ -62,6 +62,7 @@ public:
bool operator != (const BooksColorScheme&) const; bool operator != (const BooksColorScheme&) const;
bool equals(const BooksColorScheme&) const; bool equals(const BooksColorScheme&) const;
bool isInverted() const;
const QString schemeId() const; const QString schemeId() const;
const QString toString() const; const QString toString() const;

View file

@ -291,6 +291,10 @@ ZLColor BooksPaintContext::realColor(const std::string& aStyle, BooksColorScheme
} else { } else {
argb = ZLColor::rgbValue(rgb); argb = ZLColor::rgbValue(rgb);
} }
if (aColors.isInverted()) {
argb = ((~(argb & ZLColor::RGB_MASK)) & ZLColor::RGB_MASK) |
(argb & ZLColor::ALPHA_MASK);
}
} }
} }
} else if (aStyle == INTERNAL_HYPERLINK) { } else if (aStyle == INTERNAL_HYPERLINK) {