Moved out util function for contrast calculation between 2 colors.

This commit is contained in:
23rd 2022-08-29 21:22:57 +03:00 committed by John Preston
parent 5e81c65ea6
commit 35c59ad35a
4 changed files with 52 additions and 21 deletions

View file

@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/ui_utility.h"
#include "ui/chat/message_bubble.h"
#include "ui/chat/chat_style.h"
#include "ui/color_contrast.h"
#include "ui/style/style_core_palette.h"
#include "ui/style/style_palette_colorizer.h"
@ -158,25 +159,6 @@ constexpr auto kMinAcceptableContrast = 1.14;// 4.5;
return Images::GenerateLinearGradient(QSize(kSize, kSize), data.colors);
}
// https://stackoverflow.com/a/9733420
[[nodiscard]] float64 CountContrast(const QColor &a, const QColor &b) {
const auto luminance = [](const QColor &c) {
const auto map = [](double value) {
return (value <= 0.03928)
? (value / 12.92)
: std::pow((value + 0.055) / 1.055, 2.4);
};
return map(c.redF()) * 0.2126
+ map(c.greenF()) * 0.7152
+ map(c.blueF()) * 0.0722;
};
const auto luminance1 = luminance(a);
const auto luminance2 = luminance(b);
const auto brightest = std::max(luminance1, luminance2);
const auto darkest = std::min(luminance1, luminance2);
return (brightest + 0.05) / (darkest + 0.05);
}
} // namespace
bool operator==(const ChatThemeBackground &a, const ChatThemeBackground &b) {
@ -350,7 +332,7 @@ void ChatTheme::adjustPalette(const ChatThemeDescriptor &descriptor) {
const auto minimal = [&](const QColor &with) {
auto result = kMaxContrastValue;
for (const auto &color : colors) {
result = std::min(result, CountContrast(color->c, with));
result = std::min(result, Ui::CountContrast(color->c, with));
}
return result;
};
@ -363,7 +345,7 @@ void ChatTheme::adjustPalette(const ChatThemeDescriptor &descriptor) {
};
//const auto singleWithBg = [&](const QColor &c) {
// return withBg([&](const QColor &with) {
// return CountContrast(c, with);
// return Ui::CountContrast(c, with);
// });
//};
if (withBg(minimal) < kMinAcceptableContrast) {

View file

@ -0,0 +1,31 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "ui/color_contrast.h"
namespace Ui {
// https://stackoverflow.com/a/9733420
float64 CountContrast(const QColor &a, const QColor &b) {
const auto luminance = [](const QColor &c) {
const auto map = [](double value) {
return (value <= 0.03928)
? (value / 12.92)
: std::pow((value + 0.055) / 1.055, 2.4);
};
return map(c.redF()) * 0.2126
+ map(c.greenF()) * 0.7152
+ map(c.blueF()) * 0.0722;
};
const auto luminance1 = luminance(a);
const auto luminance2 = luminance(b);
const auto brightest = std::max(luminance1, luminance2);
const auto darkest = std::min(luminance1, luminance2);
return (brightest + 0.05) / (darkest + 0.05);
}
} // namespace Ui

View file

@ -0,0 +1,16 @@
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#pragma once
class QColor;
namespace Ui {
[[nodiscard]] float64 CountContrast(const QColor &a, const QColor &b);
} // namespace Ui

View file

@ -264,6 +264,8 @@ PRIVATE
ui/cached_round_corners.cpp
ui/cached_round_corners.h
ui/color_contrast.cpp
ui/color_contrast.h
ui/grouped_layout.cpp
ui/grouped_layout.h
ui/widgets/fields/special_fields.cpp