Ensure theme list radiobutton contrast.

This commit is contained in:
John Preston 2019-09-09 23:25:54 +03:00
parent 9fd32fc85b
commit 1d4fbc64e2
4 changed files with 53 additions and 18 deletions

View File

@ -165,7 +165,7 @@ QImage createCircleMask(int size, QColor bg, QColor fg) {
return (distance > kMinContrastDistance);
}
void EnsureContrast(ColorData &over, const ColorData &under) {
QColor EnsureContrast(const QColor &over, const QColor &under) {
auto overH = 0;
auto overS = 0;
auto overL = 0;
@ -173,11 +173,11 @@ void EnsureContrast(ColorData &over, const ColorData &under) {
auto underH = 0;
auto underS = 0;
auto underL = 0;
over.c.getHsl(&overH, &overS, &overL, &overA);
under.c.getHsl(&underH, &underS, &underL);
const auto good = GoodForContrast(over.c, under.c);
over.getHsl(&overH, &overS, &overL, &overA);
under.getHsl(&underH, &underS, &underL);
const auto good = GoodForContrast(over, under);
if (overA >= kMinContrastAlpha && good) {
return;
return over;
}
const auto newA = std::max(overA, kMinContrastAlpha);
const auto newL = (overL > underL && overL + kContrastDeltaL <= 255)
@ -187,9 +187,16 @@ void EnsureContrast(ColorData &over, const ColorData &under) {
: (underL > 128)
? (underL - kContrastDeltaL)
: (underL + kContrastDeltaL);
over.c = QColor::fromHsl(overH, overS, newL, newA).toRgb();
over.p = QPen(over.c);
over.b = QBrush(over.c);
return QColor::fromHsl(overH, overS, newL, newA).toRgb();
}
void EnsureContrast(ColorData &over, const ColorData &under) {
const auto good = EnsureContrast(over.c, under.c);
if (over.c != good) {
over.c = good;
over.p = QPen(good);
over.b = QBrush(good);
}
}
} // namespace internal

View File

@ -54,6 +54,7 @@ void unregisterModule(ModuleBase *module);
// This method is implemented in palette.cpp (codegen).
bool setPaletteColor(QLatin1String name, uchar r, uchar g, uchar b, uchar a);
[[nodiscard]] QColor EnsureContrast(const QColor &over, const QColor &under);
void EnsureContrast(ColorData &over, const ColorData &under);
} // namespace internal

View File

@ -154,19 +154,45 @@ CloudListCheck::CloudListCheck(bool checked)
void CloudListCheck::setColors(const Colors &colors) {
_colors = colors;
_radio.setToggledOverride(_colors->radiobuttonActive);
_radio.setUntoggledOverride(_colors->radiobuttonInactive);
const auto size = st::settingsThemePreviewSize * cIntRetinaFactor();
_backgroundFull = (_colors->background.size() == size)
? _colors->background
: _colors->background.scaled(
size,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
_backgroundCacheWidth = -1;
if (!_colors->background.isNull()) {
const auto size = st::settingsThemePreviewSize * cIntRetinaFactor();
_backgroundFull = (_colors->background.size() == size)
? _colors->background
: _colors->background.scaled(
size,
Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);
_backgroundCacheWidth = -1;
ensureContrast();
_radio.setToggledOverride(_colors->radiobuttonActive);
_radio.setUntoggledOverride(_colors->radiobuttonInactive);
}
update();
}
void CloudListCheck::ensureContrast() {
const auto radio = _radio.getSize();
const auto x = (getSize().width() - radio.width()) / 2;
const auto y = getSize().height()
- radio.height()
- st::settingsThemeRadioBottom;
const auto under = QRect(
QPoint(x, y) * cIntRetinaFactor(),
radio * cIntRetinaFactor());
const auto image = _backgroundFull.copy(under).convertToFormat(
QImage::Format_ARGB32_Premultiplied);
const auto active = style::internal::EnsureContrast(
_colors->radiobuttonActive,
CountAverageColor(image));
_colors->radiobuttonInactive = _colors->radiobuttonActive = QColor(
active.red(),
active.green(),
active.blue(),
255);
_colors->radiobuttonInactive.setAlpha(192);
}
QSize CloudListCheck::getSize() const {
return st::settingsThemePreviewSize;
}

View File

@ -61,6 +61,7 @@ private:
void paintWithColors(Painter &p, int left, int top, int outerWidth);
void checkedChangedHook(anim::type animated) override;
void validateBackgroundCache(int width);
void ensureContrast();
std::optional<Colors> _colors;
Ui::RadioView _radio;