commit 547fefa: [Fix] Fix visibility calculations
Vsevolod Stakhov
vsevolod at highsecure.ru
Tue Jul 6 13:49:06 UTC 2021
Author: Vsevolod Stakhov
Date: 2021-07-06 13:55:34 +0100
URL: https://github.com/rspamd/rspamd/commit/547fefa3664eee4839552757a6b3810444127671
[Fix] Fix visibility calculations
---
src/libserver/css/css_rule.cxx | 3 ++-
src/libserver/html/html_block.hxx | 50 +++++++++++++++++++++++++++++----------
2 files changed, 39 insertions(+), 14 deletions(-)
diff --git a/src/libserver/css/css_rule.cxx b/src/libserver/css/css_rule.cxx
index d8c3a04dd..17301e724 100644
--- a/src/libserver/css/css_rule.cxx
+++ b/src/libserver/css/css_rule.cxx
@@ -413,7 +413,8 @@ css_declarations_block::compile_to_block(rspamd_mempool_t *pool) const -> rspamd
opacity = vals.back().to_number().value_or(opacity);
break;
}
- case css_property_type::PROPERTY_FONT_COLOR: {
+ case css_property_type::PROPERTY_FONT_COLOR:
+ case css_property_type::PROPERTY_COLOR: {
auto color = vals.back().to_color();
if (color) {
block->set_fgcolor(color.value());
diff --git a/src/libserver/html/html_block.hxx b/src/libserver/html/html_block.hxx
index f2bbf1d64..28ab3bae1 100644
--- a/src/libserver/html/html_block.hxx
+++ b/src/libserver/html/html_block.hxx
@@ -179,6 +179,21 @@ struct html_block {
}
}
+ auto is_similar_colors = [](const rspamd::css::css_color &fg, const rspamd::css::css_color &bg) -> bool {
+ auto diff_r = std::abs(fg.r - bg.r);
+ auto diff_g = std::abs(fg.g - bg.g);
+ auto diff_b = std::abs(fg.b - bg.b);
+ auto ravg = (fg.r + bg.r) / 2.0;
+
+ diff_r *= diff_r;
+ diff_g *= diff_g;
+ diff_b *= diff_b;
+
+ auto diff = std::sqrt(2.0 * diff_r + 4.0 * diff_g + 3.0 * diff_b +
+ (ravg * (diff_r - diff_b) / 256.0)) / 256.0;
+
+ return diff < 0.1;
+ };
/* Check if we have both bg/fg colors */
if ((mask & (bg_color_mask|fg_color_mask)) == (bg_color_mask|fg_color_mask)) {
if (fg_color.alpha < 10) {
@@ -189,24 +204,33 @@ struct html_block {
}
if (bg_color.alpha > 10) {
- auto diff_r = std::abs(fg_color.r - bg_color.r);
- auto diff_g = std::abs(fg_color.g - bg_color.g);
- auto diff_b = std::abs(fg_color.b - bg_color.b);
- auto ravg = (fg_color.r + bg_color.r) / 2.0;
-
- diff_r *= diff_r;
- diff_g *= diff_g;
- diff_b *= diff_b;
-
- auto diff = std::sqrt(2.0 * diff_r + 4.0 * diff_g + 3.0 * diff_b +
- (ravg * (diff_r - diff_b) / 256.0)) / 256.0;
-
- if (diff < 0.1) {
+ if (is_similar_colors(fg_color, bg_color)) {
mask |= invisible_flag|transparent_flag;
return;
}
}
}
+ else if (mask & fg_color_mask) {
+ /* Merely fg color */
+ if (fg_color.alpha < 10) {
+ /* Too transparent */
+ mask |= invisible_flag|transparent_flag;
+
+ return;
+ }
+
+ /* Implicit fg color */
+ if (is_similar_colors(fg_color, rspamd::css::css_color::white())) {
+ mask |= invisible_flag|transparent_flag;
+ return;
+ }
+ }
+ else if (mask & bg_color_mask) {
+ if (is_similar_colors(rspamd::css::css_color::black(), bg_color)) {
+ mask |= invisible_flag|transparent_flag;
+ return;
+ }
+ }
mask &= ~(invisible_flag|transparent_flag);
}
More information about the Commits
mailing list