mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Moved out util function for contrast calculation between 2 colors.
This commit is contained in:
parent
5e81c65ea6
commit
35c59ad35a
4 changed files with 52 additions and 21 deletions
|
@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "ui/ui_utility.h"
|
#include "ui/ui_utility.h"
|
||||||
#include "ui/chat/message_bubble.h"
|
#include "ui/chat/message_bubble.h"
|
||||||
#include "ui/chat/chat_style.h"
|
#include "ui/chat/chat_style.h"
|
||||||
|
#include "ui/color_contrast.h"
|
||||||
#include "ui/style/style_core_palette.h"
|
#include "ui/style/style_core_palette.h"
|
||||||
#include "ui/style/style_palette_colorizer.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);
|
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
|
} // namespace
|
||||||
|
|
||||||
bool operator==(const ChatThemeBackground &a, const ChatThemeBackground &b) {
|
bool operator==(const ChatThemeBackground &a, const ChatThemeBackground &b) {
|
||||||
|
@ -350,7 +332,7 @@ void ChatTheme::adjustPalette(const ChatThemeDescriptor &descriptor) {
|
||||||
const auto minimal = [&](const QColor &with) {
|
const auto minimal = [&](const QColor &with) {
|
||||||
auto result = kMaxContrastValue;
|
auto result = kMaxContrastValue;
|
||||||
for (const auto &color : colors) {
|
for (const auto &color : colors) {
|
||||||
result = std::min(result, CountContrast(color->c, with));
|
result = std::min(result, Ui::CountContrast(color->c, with));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
@ -363,7 +345,7 @@ void ChatTheme::adjustPalette(const ChatThemeDescriptor &descriptor) {
|
||||||
};
|
};
|
||||||
//const auto singleWithBg = [&](const QColor &c) {
|
//const auto singleWithBg = [&](const QColor &c) {
|
||||||
// return withBg([&](const QColor &with) {
|
// return withBg([&](const QColor &with) {
|
||||||
// return CountContrast(c, with);
|
// return Ui::CountContrast(c, with);
|
||||||
// });
|
// });
|
||||||
//};
|
//};
|
||||||
if (withBg(minimal) < kMinAcceptableContrast) {
|
if (withBg(minimal) < kMinAcceptableContrast) {
|
||||||
|
|
31
Telegram/SourceFiles/ui/color_contrast.cpp
Normal file
31
Telegram/SourceFiles/ui/color_contrast.cpp
Normal 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
|
16
Telegram/SourceFiles/ui/color_contrast.h
Normal file
16
Telegram/SourceFiles/ui/color_contrast.h
Normal 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
|
|
@ -264,6 +264,8 @@ PRIVATE
|
||||||
|
|
||||||
ui/cached_round_corners.cpp
|
ui/cached_round_corners.cpp
|
||||||
ui/cached_round_corners.h
|
ui/cached_round_corners.h
|
||||||
|
ui/color_contrast.cpp
|
||||||
|
ui/color_contrast.h
|
||||||
ui/grouped_layout.cpp
|
ui/grouped_layout.cpp
|
||||||
ui/grouped_layout.h
|
ui/grouped_layout.h
|
||||||
ui/widgets/fields/special_fields.cpp
|
ui/widgets/fields/special_fields.cpp
|
||||||
|
|
Loading…
Add table
Reference in a new issue