Commit 1732cc5b authored by Aran Gilman's avatar Aran Gilman Committed by Commit Bot

Add method to determine whether to apply high contrast filters to page.

Currently it only looks at the background color and does not check
whether high contrast mode is enabled overall. Future CLs will add the
ability to process and correctly classify background images.

This code is not under platform/graphics with the existing high contrast
code because it needs access to ComputedStyle and LayoutObject, and code
in platform cannot depend on core.

Follow-up CLs are planned to modify GraphicsContext::SetHighContrast()
to take the return value of this function as a parameter.


Bug: 925949
Change-Id: I79376205d5ee5d0b20a1138ffcf1568ac7bf0ae7
Reviewed-on: https://chromium-review.googlesource.com/c/1448988
Commit-Queue: Aran Gilman <gilmanmh@google.com>
Reviewed-by: default avatarChris Harrelson <chrishtr@chromium.org>
Reviewed-by: default avatarRune Lillesveen <futhark@chromium.org>
Reviewed-by: default avatarDominic Mazzoni <dmazzoni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#636615}
parent 2f42bbc1
......@@ -2268,6 +2268,7 @@ jumbo_source_set("unit_tests") {
"//skia:skcms",
"//testing/gmock",
"//testing/gtest",
"//third_party/blink/renderer/core/accessibility:unit_tests",
"//third_party/blink/renderer/core/editing:unit_tests",
"//third_party/blink/renderer/core/fileapi:unit_tests",
]
......
......@@ -6,6 +6,8 @@ import("//third_party/blink/renderer/core/core.gni")
blink_core_sources("accessibility") {
sources = [
"apply_high_contrast_check.cc",
"apply_high_contrast_check.h",
"ax_context.cc",
"ax_context.h",
"ax_object_cache.cc",
......@@ -15,3 +17,9 @@ blink_core_sources("accessibility") {
"axid.h",
]
}
blink_core_tests("unit_tests") {
sources = [
"apply_high_contrast_check_test.cc",
]
}
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/accessibility/apply_high_contrast_check.h"
#include "third_party/blink/renderer/core/css/properties/css_property.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/graphics/high_contrast_color_classifier.h"
namespace blink {
namespace {
// TODO(https://crbug.com/925949): Add detection and classification of
// background image color. Most sites with dark background images also have a
// dark background color set, so this is less of a priority than it would be
// otherwise.
bool HasLightBackground(const LayoutObject& layout_object) {
const ComputedStyle& style = layout_object.StyleRef();
if (style.HasBackground()) {
Color color = style.VisitedDependentColor(GetCSSPropertyBackgroundColor());
return IsLight(color, style.Opacity());
}
// If we can't easily determine the background color, default to inverting the
// page.
return true;
}
} // namespace
bool ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy policy,
const LayoutObject& root_layout_object) {
switch (policy) {
case HighContrastPagePolicy::kFilterAll:
return true;
case HighContrastPagePolicy::kFilterByBackground:
return HasLightBackground(root_layout_object);
}
}
} // namespace blink
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_ACCESSIBILITY_APPLY_HIGH_CONTRAST_CHECK_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_ACCESSIBILITY_APPLY_HIGH_CONTRAST_CHECK_H_
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
namespace blink {
// TODO(https://crbug.com/925949): Move this to high_contrast_settings.h.
enum class HighContrastPagePolicy {
// Apply high-contrast filter to all frames, regardless of content.
kFilterAll,
// Apply high-contrast filter to frames based on background color.
kFilterByBackground,
};
// Determine whether the page with the provided |root_layout_object| should have
// its colors inverted, based on the provided |policy|.
//
// This method does not check whether High Contrast Mode is enabled overall.
bool CORE_EXPORT
ShouldApplyHighContrastFilterToPage(HighContrastPagePolicy policy,
const LayoutObject& root_layout_object);
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_CORE_ACCESSIBILITY_APPLY_HIGH_CONTRAST_CHECK_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/accessibility/apply_high_contrast_check.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/core/testing/core_unit_test_helper.h"
namespace blink {
namespace {
using ApplyHighContrastCheckTest = RenderingTest;
TEST_F(ApplyHighContrastCheckTest, LightSolidBackgroundAlwaysFiltered) {
GetDocument().body()->SetInlineStyleProperty(CSSPropertyBackgroundColor,
CSSValueWhite);
UpdateAllLifecyclePhasesForTest();
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterByBackground, GetLayoutView()));
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterAll, GetLayoutView()));
}
TEST_F(ApplyHighContrastCheckTest,
DarkSolidBackgroundFilteredIfPolicyIsFilterAll) {
GetDocument().body()->SetInlineStyleProperty(CSSPropertyBackgroundColor,
CSSValueBlack);
// TODO(https://crbug.com/925949): Set opacity the same way as the other CSS
// properties.
GetLayoutView().MutableStyle()->SetOpacity(0.9);
UpdateAllLifecyclePhasesForTest();
EXPECT_FALSE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterByBackground, GetLayoutView()));
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterAll, GetLayoutView()));
}
TEST_F(ApplyHighContrastCheckTest, DarkLowOpacityBackgroundAlwaysFiltered) {
GetDocument().body()->SetInlineStyleProperty(CSSPropertyBackgroundColor,
CSSValueBlack);
// TODO(https://crbug.com/925949): Set opacity the same way as the other CSS
// properties.
GetLayoutView().MutableStyle()->SetOpacity(0.1);
UpdateAllLifecyclePhasesForTest();
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterByBackground, GetLayoutView()));
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterAll, GetLayoutView()));
}
TEST_F(ApplyHighContrastCheckTest, DarkTransparentBackgroundAlwaysFiltered) {
GetDocument().body()->SetInlineStyleProperty(CSSPropertyBackgroundColor,
CSSValueTransparent);
UpdateAllLifecyclePhasesForTest();
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterByBackground, GetLayoutView()));
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterAll, GetLayoutView()));
}
TEST_F(ApplyHighContrastCheckTest, BackgroundColorNotDefinedAlwaysFiltered) {
GetDocument().body()->RemoveInlineStyleProperty(CSSPropertyBackgroundColor);
UpdateAllLifecyclePhasesForTest();
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterByBackground, GetLayoutView()));
EXPECT_TRUE(ShouldApplyHighContrastFilterToPage(
HighContrastPagePolicy::kFilterAll, GetLayoutView()));
}
} // namespace
} // namespace blink
......@@ -972,6 +972,8 @@ jumbo_component("platform") {
"graphics/graphics_types.cc",
"graphics/graphics_types.h",
"graphics/graphics_types_3d.h",
"graphics/high_contrast_color_classifier.cc",
"graphics/high_contrast_color_classifier.h",
"graphics/high_contrast_image_classifier.cc",
"graphics/high_contrast_image_classifier.h",
"graphics/high_contrast_settings.h",
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/platform/graphics/high_contrast_color_classifier.h"
namespace blink {
// Values below which a color is considered sufficiently transparent that a
// lighter color behind it would make the final color as seen by the user light.
// TODO(https://crbug.com/925949): This is a placeholder value. It should be
// replaced with a better researched value before launching high contrast/dark
// mode.
const float kOpacityThreshold = 0.4;
// TODO(https://crbug.com/925949): Find a better algorithm for this.
bool IsLight(const Color& color, float opacity) {
// Multiply the opacity by the alpha value to get a more accurate sense of how
// transparent the element is.
float real_opacity = opacity * (color.Alpha() / 255);
// Assume the color is light if the background is sufficiently transparent.
if (real_opacity < kOpacityThreshold) {
return true;
}
double hue;
double saturation;
double lightness;
color.GetHSL(hue, saturation, lightness);
return lightness > 0.5;
}
} // namespace blink
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_HIGH_CONTRAST_COLOR_CLASSIFIER_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_HIGH_CONTRAST_COLOR_CLASSIFIER_H_
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/platform_export.h"
namespace blink {
bool PLATFORM_EXPORT IsLight(const Color& color, float opacity);
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_HIGH_CONTRAST_COLOR_CLASSIFIER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment