Bug 1464722 part 2 - Move relative luminance functions into gfx. r?jrmuizel
MozReview-Commit-ID: H1BDdffJxaz
new file mode 100644
--- /dev/null
+++ b/gfx/src/RelativeLuminanceUtils.h
@@ -0,0 +1,71 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* 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/. */
+
+#ifndef mozilla_RelativeLuminanceUtils_h
+#define mozilla_RelativeLuminanceUtils_h
+
+#include "nsColor.h"
+
+namespace mozilla {
+
+// Utilities for calculating relative luminance based on the algorithm
+// defined in https://www.w3.org/TR/WCAG20/#relativeluminancedef
+class RelativeLuminanceUtils
+{
+public:
+ // Compute the relative luminance.
+ static float Compute(nscolor aColor)
+ {
+ float r = ComputeComponent(NS_GET_R(aColor));
+ float g = ComputeComponent(NS_GET_G(aColor));
+ float b = ComputeComponent(NS_GET_B(aColor));
+ return ComputeFromComponents(r, g, b);
+ }
+
+ // Adjust the relative luminance of the given color.
+ static nscolor Adjust(nscolor aColor, float aLuminance)
+ {
+ float r = ComputeComponent(NS_GET_R(aColor));
+ float g = ComputeComponent(NS_GET_G(aColor));
+ float b = ComputeComponent(NS_GET_B(aColor));
+ float luminance = ComputeFromComponents(r, g, b);
+ float factor = aLuminance / luminance;
+ uint8_t r1 = DecomputeComponent(r * factor);
+ uint8_t g1 = DecomputeComponent(g * factor);
+ uint8_t b1 = DecomputeComponent(b * factor);
+ return NS_RGB(r1, g1, b1);
+ }
+
+private:
+ static float ComputeComponent(uint8_t aComponent)
+ {
+ float v = float(aComponent) / 255.0f;
+ if (v <= 0.03928f) {
+ return v / 12.92f;
+ }
+ return std::pow((v + 0.055f) / 1.055f, 2.4f);
+ }
+
+ static constexpr float ComputeFromComponents(float aR, float aG, float aB)
+ {
+ return 0.2126f * aR + 0.7152f * aG + 0.0722f * aB;
+ }
+
+ // Inverse function of ComputeComponent.
+ static uint8_t DecomputeComponent(float aComponent)
+ {
+ if (aComponent <= 0.03928f / 12.92f) {
+ aComponent *= 12.92f;
+ } else {
+ aComponent = std::pow(aComponent, 1.0f / 2.4f) * 1.055f - 0.055f;
+ }
+ return ClampColor(aComponent * 255.0f);
+ }
+};
+
+} // namespace mozilla
+
+#endif // mozilla_RelativeLuminanceUtils_h
--- a/gfx/src/moz.build
+++ b/gfx/src/moz.build
@@ -40,16 +40,17 @@ EXPORTS += [
'RegionBuilder.h',
'X11UndefineNone.h'
]
EXPORTS.mozilla += [
'AppUnits.h',
'ArrayView.h',
'FontPropertyTypes.h',
+ 'RelativeLuminanceUtils.h',
]
EXPORTS.mozilla.gfx += [
'CompositorHitTestInfo.h',
'TiledRegion.h',
]
if CONFIG['MOZ_X11']:
--- a/widget/windows/nsNativeThemeWin.cpp
+++ b/widget/windows/nsNativeThemeWin.cpp
@@ -2,16 +2,17 @@
/* 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/. */
#include "nsNativeThemeWin.h"
#include "mozilla/EventStates.h"
#include "mozilla/Logging.h"
+#include "mozilla/RelativeLuminanceUtils.h"
#include "mozilla/WindowsVersion.h"
#include "nsColor.h"
#include "nsDeviceContext.h"
#include "nsRect.h"
#include "nsSize.h"
#include "nsTransform2D.h"
#include "nsThemeConstants.h"
#include "nsIPresShell.h"
@@ -4106,95 +4107,41 @@ nsNativeThemeWin::GetWidgetNativeDrawing
}
static COLORREF
ToColorRef(nscolor aColor)
{
return RGB(NS_GET_R(aColor), NS_GET_G(aColor), NS_GET_B(aColor));
}
-static float
-ComputeColorComponentLuminance(uint8_t aComponent)
-{
- float v = float(aComponent) / 255.0f;
- if (v <= 0.03928f) {
- return v / 12.92f;
- }
- return std::pow((v + 0.055f) / 1.055f, 2.4f);
-}
-
-static constexpr float
-ComputeRelativeLuminanceFromComponents(float aR, float aG, float aB)
-{
- return 0.2126f * aR + 0.7152f * aG + 0.0722f * aB;
-}
-
-// This function is written according to the algorithm defined in
-// https://www.w3.org/TR/WCAG20/#relativeluminancedef
-static float
-ComputeRelativeLuminance(nscolor aColor)
-{
- float r = ComputeColorComponentLuminance(NS_GET_R(aColor));
- float g = ComputeColorComponentLuminance(NS_GET_G(aColor));
- float b = ComputeColorComponentLuminance(NS_GET_B(aColor));
- return ComputeRelativeLuminanceFromComponents(r, g, b);
-}
-
-// Inverse function of ComputeColorComponentLuminance.
-static uint8_t
-DecomputeColorComponentLuminance(float aComponent)
-{
- if (aComponent <= 0.03928f / 12.92f) {
- aComponent *= 12.92f;
- } else {
- aComponent = std::pow(aComponent, 1.0f / 2.4f) * 1.055f - 0.055f;
- }
- return ClampColor(aComponent * 255.0f);
-}
-
-// Adjust the luminance of the color to the given value.
-static nscolor
-AdjustColorLuminance(nscolor aColor, float aLuminance)
-{
- float r = ComputeColorComponentLuminance(NS_GET_R(aColor));
- float g = ComputeColorComponentLuminance(NS_GET_G(aColor));
- float b = ComputeColorComponentLuminance(NS_GET_B(aColor));
- float luminance = ComputeRelativeLuminanceFromComponents(r, g, b);
- float factor = aLuminance / luminance;
- uint8_t r1 = DecomputeColorComponentLuminance(r * factor);
- uint8_t g1 = DecomputeColorComponentLuminance(g * factor);
- uint8_t b1 = DecomputeColorComponentLuminance(b * factor);
- return NS_RGB(r1, g1, b1);
-}
-
static nscolor
GetScrollbarArrowColor(nscolor aTrackColor)
{
// In Windows 10 scrollbar, there are several gray colors used:
//
// State | Background (lum) | Arrow | Contrast
// -------+------------------+---------+---------
// Normal | Gray 240 (87.1%) | Gray 96 | 5.5
// Hover | Gray 218 (70.1%) | Black | 15.0
// Active | Gray 96 (11.7%) | White | 6.3
//
// Contrast value is computed based on the definition in
// https://www.w3.org/TR/WCAG20/#contrast-ratiodef
//
// This function is written based on these values.
- float luminance = ComputeRelativeLuminance(aTrackColor);
+ float luminance = RelativeLuminanceUtils::Compute(aTrackColor);
// Color with luminance larger than 0.72 has contrast ratio over 4.6
// to color with luminance of gray 96, so this value is chosen for
// this range. It is the luminance of gray 221.
if (luminance >= 0.72) {
// ComputeRelativeLuminanceFromComponents(96). That function cannot
// be constexpr because of std::pow.
const float GRAY96_LUMINANCE = 0.117f;
- return AdjustColorLuminance(aTrackColor, GRAY96_LUMINANCE);
+ return RelativeLuminanceUtils::Adjust(aTrackColor, GRAY96_LUMINANCE);
}
// The contrast ratio of a color to black equals that to white when its
// luminance is around 0.18, with a contrast ratio ~4.6 to both sides,
// thus the value below. It's the lumanince of gray 118.
if (luminance >= 0.18) {
return NS_RGB(0, 0, 0);
}
return NS_RGB(255, 255, 255);