summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/accessibility/apply_dark_mode.cc
blob: 88164f47a8d56680c3f31a11a545432d67a4a2be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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_dark_mode.h"

#include "base/metrics/field_trial_params.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/forcedark/forcedark_switches.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/dark_mode_color_classifier.h"
#include "third_party/blink/renderer/platform/graphics/dark_mode_settings.h"

namespace blink {
namespace {

const int kAlphaThreshold = 100;
const int kBrightnessThreshold = 50;

// 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 LayoutView& root) {
  const ComputedStyle& style = root.StyleRef();

  // If we can't easily determine the background color, default to inverting the
  // page.
  if (!style.HasBackground())
    return true;

  Color color = style.VisitedDependentColor(GetCSSPropertyBackgroundColor());
  if (color.Alpha() < kAlphaThreshold)
    return true;

  return DarkModeColorClassifier::CalculateColorBrightness(color) >
         kBrightnessThreshold;
}

DarkModeInversionAlgorithm GetMode(const Settings& frame_settings) {
  switch (features::kForceDarkInversionMethodParam.Get()) {
    case ForceDarkInversionMethod::kUseBlinkSettings:
      return frame_settings.GetForceDarkModeInversionAlgorithm();
    case ForceDarkInversionMethod::kCielabBased:
      return DarkModeInversionAlgorithm::kInvertLightnessLAB;
    case ForceDarkInversionMethod::kHslBased:
      return DarkModeInversionAlgorithm::kInvertLightness;
    case ForceDarkInversionMethod::kRgbBased:
      return DarkModeInversionAlgorithm::kInvertBrightness;
  }
}

DarkModeImagePolicy GetImagePolicy(const Settings& frame_settings) {
  switch (features::kForceDarkImageBehaviorParam.Get()) {
    case ForceDarkImageBehavior::kUseBlinkSettings:
      return frame_settings.GetForceDarkModeImagePolicy();
    case ForceDarkImageBehavior::kInvertNone:
      return DarkModeImagePolicy::kFilterNone;
    case ForceDarkImageBehavior::kInvertSelectively:
      return DarkModeImagePolicy::kFilterSmart;
  }
}

int GetTextBrightnessThreshold(const Settings& frame_settings) {
  const int flag_value = base::GetFieldTrialParamByFeatureAsInt(
      features::kForceWebContentsDarkMode,
      features::kForceDarkTextLightnessThresholdParam.name, -1);
  return flag_value >= 0
             ? flag_value
             : frame_settings.GetForceDarkModeTextBrightnessThreshold();
}

int GetBackgroundBrightnessThreshold(const Settings& frame_settings) {
  const int flag_value = base::GetFieldTrialParamByFeatureAsInt(
      features::kForceWebContentsDarkMode,
      features::kForceDarkBackgroundLightnessThresholdParam.name, -1);
  return flag_value >= 0
             ? flag_value
             : frame_settings.GetForceDarkModeBackgroundBrightnessThreshold();
}

DarkModeSettings GetEnabledSettings(const Settings& frame_settings) {
  DarkModeSettings settings;
  settings.mode = GetMode(frame_settings);
  settings.image_policy = GetImagePolicy(frame_settings);
  settings.text_brightness_threshold =
      GetTextBrightnessThreshold(frame_settings);
  settings.background_brightness_threshold =
      GetBackgroundBrightnessThreshold(frame_settings);

  settings.grayscale = frame_settings.GetForceDarkModeGrayscale();
  settings.contrast = frame_settings.GetForceDarkModeContrast();
  settings.image_grayscale_percent =
      frame_settings.GetForceDarkModeImageGrayscale();
  return settings;
}

DarkModeSettings GetDisabledSettings() {
  DarkModeSettings settings;
  settings.mode = DarkModeInversionAlgorithm::kOff;
  return settings;
}

}  // namespace

DarkModeSettings BuildDarkModeSettings(const Settings& frame_settings,
                                       const LayoutView& root) {
  if (frame_settings.GetForceDarkModeEnabled() &&
      ShouldApplyDarkModeFilterToPage(
          frame_settings.GetForceDarkModePagePolicy(), root)) {
    return GetEnabledSettings(frame_settings);
  }
  return GetDisabledSettings();
}

bool ShouldApplyDarkModeFilterToPage(DarkModePagePolicy policy,
                                     const LayoutView& root) {
  if (root.StyleRef().DarkColorScheme())
    return false;

  switch (policy) {
    case DarkModePagePolicy::kFilterAll:
      return true;
    case DarkModePagePolicy::kFilterByBackground:
      return HasLightBackground(root);
  }
}

}  // namespace blink