/* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// Ensure accent color is opaque by blending with white. This serves two // purposes: On one hand, it avoids surprises if we overdraw. On the other, it // makes our math below make more sense, as we want to match the browser // style, which has an opaque accent color. static nscolor EnsureOpaque(nscolor aAccent) { if (NS_GET_A(aAccent) != 0xff) { return NS_ComposeColors(NS_RGB(0xff, 0xff, 0xff), aAccent);
} return aAccent;
}
static nscolor GetLight(nscolor aAccent) { // The luminance from the light color divided by the one of the accent color // in the default palette.
constexpr float kLightLuminanceScale = 25.048f / 13.693f; constfloat lightLuminanceAdjust = ThemeColors::ScaleLuminanceBy(
RelativeLuminanceUtils::Compute(aAccent), kLightLuminanceScale);
nscolor lightColor =
RelativeLuminanceUtils::Adjust(aAccent, lightLuminanceAdjust); return NS_RGBA(NS_GET_R(lightColor), NS_GET_G(lightColor),
NS_GET_B(lightColor), 0x4d);
}
static nscolor GetDark(nscolor aAccent) { // Same deal as above (but without the alpha).
constexpr float kDarkLuminanceScale = 9.338f / 13.693f; constfloat darkLuminanceAdjust = ThemeColors::ScaleLuminanceBy(
RelativeLuminanceUtils::Compute(aAccent), kDarkLuminanceScale); return RelativeLuminanceUtils::Adjust(aAccent, darkLuminanceAdjust);
}
auto ThemeColors::ShouldBeHighContrast(const nsPresContext& aPc)
-> HighContrastInfo { // We make sure that we're drawing backgrounds, since otherwise layout will // darken our used text colors etc anyways, and that can cause contrast issues // with dark high-contrast themes. if (!aPc.GetBackgroundColorDraw()) { return {};
} constauto& prefs = PreferenceSheet::PrefsFor(*aPc.Document()); return {prefs.NonNativeThemeShouldBeHighContrast(),
prefs.mMustUseLightSystemColors};
}
ColorScheme ThemeColors::ColorSchemeForWidget(const nsIFrame* aFrame,
StyleAppearance aAppearance, const HighContrastInfo& aInfo) { if (aInfo.mMustUseLightSystemColors) { return ColorScheme::Light;
} if (!nsNativeTheme::IsWidgetScrollbarPart(aAppearance)) { return LookAndFeel::ColorSchemeForFrame(aFrame);
} // Scrollbars are a bit tricky. Their used color-scheme depends on whether the // background they are on is light or dark. // // TODO(emilio): This heuristic effectively predates the color-scheme CSS // property. Perhaps we should check whether the style or the document set // `color-scheme` to something that isn't `normal`, and if so go through the // code-path above. if (StaticPrefs::widget_disable_dark_scrollbar()) { return ColorScheme::Light;
} return nsNativeTheme::IsDarkBackgroundForScrollbar( const_cast<nsIFrame*>(aFrame))
? ColorScheme::Dark
: ColorScheme::Light;
}
/*static*/
nscolor ThemeColors::ComputeCustomAccentForeground(nscolor aColor) { // Contrast ratio is defined in // https://www.w3.org/TR/WCAG20/#contrast-ratiodef as: // // (L1 + 0.05) / (L2 + 0.05) // // Where L1 is the lighter color, and L2 is the darker one. So we determine // whether we're dark or light and resolve the equation for the target ratio. // // So when lightening: // // L1 = k * (L2 + 0.05) - 0.05 // // And when darkening: // // L2 = (L1 + 0.05) / k - 0.05 // constfloat luminance = RelativeLuminanceUtils::Compute(aColor);
// We generally prefer white unless we can't because the color is really light // and we can't provide reasonable contrast. constfloat ratioWithWhite = 1.05f / (luminance + 0.05f); constbool canBeWhite =
ratioWithWhite >=
StaticPrefs::layout_css_accent_color_min_contrast_ratio(); if (canBeWhite) { return NS_RGB(0xff, 0xff, 0xff);
} constfloat targetRatio =
StaticPrefs::layout_css_accent_color_darkening_target_contrast_ratio(); constfloat targetLuminance = (luminance + 0.05f) / targetRatio - 0.05f; return RelativeLuminanceUtils::Adjust(aColor, targetLuminance);
}
nscolor ThemeColors::AdjustUnthemedScrollbarThumbColor(
nscolor aFaceColor, dom::ElementState aStates) { // In Windows 10, scrollbar thumb has the following colors: // // State | Color | Luminance // -------+----------+---------- // Normal | Gray 205 | 61.0% // Hover | Gray 166 | 38.1% // Active | Gray 96 | 11.7% // // This function is written based on the ratios between the values. bool isActive = aStates.HasState(dom::ElementState::ACTIVE); bool isHover = aStates.HasState(dom::ElementState::HOVER); if (!isActive && !isHover) { return aFaceColor;
} float luminance = RelativeLuminanceUtils::Compute(aFaceColor); if (isActive) { // 11.7 / 61.0
luminance = ScaleLuminanceBy(luminance, 0.192f);
} else { // 38.1 / 61.0
luminance = ScaleLuminanceBy(luminance, 0.625f);
} return RelativeLuminanceUtils::Adjust(aFaceColor, luminance);
}
} // namespace mozilla::widget
¤ Dauer der Verarbeitung: 0.36 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.