Tweak underline style for data detected links
authorweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 11 May 2016 00:16:14 +0000 (00:16 +0000)
committerweinig@apple.com <weinig@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 11 May 2016 00:16:14 +0000 (00:16 +0000)
https://bugs.webkit.org/show_bug.cgi?id=157546

Reviewed by Tim Horton.

Source/WebCore:

Added API Tests in Color.cpp.

* editing/cocoa/DataDetection.mm:
(WebCore::DataDetection::detectContentInRange):
Tweak the underline's opacity based on the text color. White-ish text gets
46% opacity, everything else gets 26% opacity.

* platform/graphics/Color.cpp:
(WebCore::Color::getHSV):
* platform/graphics/Color.h:
Add support for getting the HSV (also know as HSB) computation of the color
to help determine the "whiteness" of a color.

Tools:

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebCore/Color.cpp: Added.
(TestWebKitAPI::TEST):
Add tests for the new Color::getHSV() function.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@200659 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/editing/cocoa/DataDetection.mm
Source/WebCore/platform/graphics/Color.cpp
Source/WebCore/platform/graphics/Color.h
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebCore/Color.cpp [new file with mode: 0644]

index d2aa739..c076e7c 100644 (file)
@@ -1,3 +1,23 @@
+2016-05-10  Sam Weinig  <sam@webkit.org>
+
+        Tweak underline style for data detected links
+        https://bugs.webkit.org/show_bug.cgi?id=157546
+
+        Reviewed by Tim Horton.
+
+        Added API Tests in Color.cpp.
+
+        * editing/cocoa/DataDetection.mm:
+        (WebCore::DataDetection::detectContentInRange):
+        Tweak the underline's opacity based on the text color. White-ish text gets
+        46% opacity, everything else gets 26% opacity.
+
+        * platform/graphics/Color.cpp:
+        (WebCore::Color::getHSV):
+        * platform/graphics/Color.h:
+        Add support for getting the HSV (also know as HSB) computation of the color
+        to help determine the "whiteness" of a color.
+
 2016-05-10  Alex Christensen  <achristensen@webkit.org>
 
         Handle _schemeUpgraded delegate callbacks in NSURLSessionDataDelegate
index daa4084..937b47d 100644 (file)
@@ -639,7 +639,16 @@ NSArray *DataDetection::detectContentInRange(RefPtr<Range>& contextRange, DataDe
                 if (renderStyle) {
                     auto textColor = renderStyle->visitedDependentColor(CSSPropertyColor);
                     if (textColor.isValid()) {
-                        auto underlineColor = Color(colorWithOverrideAlpha(textColor.rgb(), 0.2));
+                        double h = 0;
+                        double s = 0;
+                        double v = 0;
+                        textColor.getHSV(h, s, v);
+
+                        // Set the alpha of the underline to 46% if the text color is white-ish (defined
+                        // as having a saturation of less than 2% and a value/brightness or greater than
+                        // 98%). Otherwise, set the alpha of the underline to 26%.
+                        double overrideAlpha = (s < 0.02 && v > 0.98) ? 0.46 : 0.26;
+                        auto underlineColor = Color(colorWithOverrideAlpha(textColor.rgb(), overrideAlpha));
 
                         anchorElement->setInlineStyleProperty(CSSPropertyColor, textColor.cssText());
                         anchorElement->setInlineStyleProperty(CSSPropertyWebkitTextDecorationColor, underlineColor.cssText());
index 0355f87..4e25aa6 100644 (file)
@@ -422,15 +422,16 @@ void Color::getHSL(double& hue, double& saturation, double& lightness) const
     double b = static_cast<double>(blue()) / 255.0;
     double max = std::max(std::max(r, g), b);
     double min = std::min(std::min(r, g), b);
+    double chroma = max - min;
 
-    if (max == min)
+    if (!chroma)
         hue = 0.0;
     else if (max == r)
-        hue = (60.0 * ((g - b) / (max - min))) + 360.0;
+        hue = (60.0 * ((g - b) / chroma)) + 360.0;
     else if (max == g)
-        hue = (60.0 * ((b - r) / (max - min))) + 120.0;
+        hue = (60.0 * ((b - r) / chroma)) + 120.0;
     else
-        hue = (60.0 * ((r - g) / (max - min))) + 240.0;
+        hue = (60.0 * ((r - g) / chroma)) + 240.0;
 
     if (hue >= 360.0)
         hue -= 360.0;
@@ -439,12 +440,43 @@ void Color::getHSL(double& hue, double& saturation, double& lightness) const
     hue /= 360.0;
 
     lightness = 0.5 * (max + min);
-    if (max == min)
+    if (!chroma)
         saturation = 0.0;
     else if (lightness <= 0.5)
-        saturation = ((max - min) / (max + min));
+        saturation = (chroma / (max + min));
     else
-        saturation = ((max - min) / (2.0 - (max + min)));
+        saturation = (chroma / (2.0 - (max + min)));
+}
+
+void Color::getHSV(double& hue, double& saturation, double& value) const
+{
+    double r = static_cast<double>(red()) / 255.0;
+    double g = static_cast<double>(green()) / 255.0;
+    double b = static_cast<double>(blue()) / 255.0;
+    double max = std::max(std::max(r, g), b);
+    double min = std::min(std::min(r, g), b);
+    double chroma = max - min;
+
+    if (!chroma)
+        hue = 0.0;
+    else if (max == r)
+        hue = (60.0 * ((g - b) / chroma)) + 360.0;
+    else if (max == g)
+        hue = (60.0 * ((b - r) / chroma)) + 120.0;
+    else
+        hue = (60.0 * ((r - g) / chroma)) + 240.0;
+
+    if (hue >= 360.0)
+        hue -= 360.0;
+
+    hue /= 360.0;
+
+    if (!max)
+        saturation = 0;
+    else
+        saturation = chroma / max;
+
+    value = max;
 }
 
 Color colorFromPremultipliedARGB(RGBA32 pixelColor)
index 1f853e4..dd26270 100644 (file)
@@ -147,6 +147,7 @@ public:
     WEBCORE_EXPORT void getRGBA(float& r, float& g, float& b, float& a) const;
     WEBCORE_EXPORT void getRGBA(double& r, double& g, double& b, double& a) const;
     WEBCORE_EXPORT void getHSL(double& h, double& s, double& l) const;
+    WEBCORE_EXPORT void getHSV(double& h, double& s, double& v) const;
 
     Color light() const;
     Color dark() const;
index ecdd4eb..6b86ad6 100644 (file)
@@ -1,3 +1,15 @@
+2016-05-10  Sam Weinig  <sam@webkit.org>
+
+        Tweak underline style for data detected links
+        https://bugs.webkit.org/show_bug.cgi?id=157546
+
+        Reviewed by Tim Horton.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebCore/Color.cpp: Added.
+        (TestWebKitAPI::TEST):
+        Add tests for the new Color::getHSV() function.
+
 2016-05-10  Filip Pizlo  <fpizlo@apple.com>
 
         Internal JSC profiler should have a timestamped log of events for each code block
index 5c078f4..024e40f 100644 (file)
                7AA021BB1AB09EA70052953F /* DateMath.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7AA021BA1AB09EA70052953F /* DateMath.cpp */; };
                7AA6A1521AAC0B31002B2ED3 /* WorkQueue.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7AA6A1511AAC0B31002B2ED3 /* WorkQueue.cpp */; };
                7AE9E5091AE5AE8B00CF874B /* test.pdf in Copy Resources */ = {isa = PBXBuildFile; fileRef = 7AE9E5081AE5AE8B00CF874B /* test.pdf */; };
+               7C3965061CDD74F90094DBB8 /* Color.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7C3965051CDD74F90094DBB8 /* Color.cpp */; };
                7C486BA11AA12567003F6F9B /* bundle-file.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = 7C486BA01AA1254B003F6F9B /* bundle-file.html */; };
                7C54A4BE1AA11CCA00380F78 /* WKBundleFileHandle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7C54A4BC1AA11CCA00380F78 /* WKBundleFileHandle.cpp */; };
                7C54A4C11AA11CE400380F78 /* WKBundleFileHandle_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7C54A4BF1AA11CE400380F78 /* WKBundleFileHandle_Bundle.cpp */; };
                7AA021BA1AB09EA70052953F /* DateMath.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DateMath.cpp; sourceTree = "<group>"; };
                7AA6A1511AAC0B31002B2ED3 /* WorkQueue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WorkQueue.cpp; sourceTree = "<group>"; };
                7AE9E5081AE5AE8B00CF874B /* test.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = test.pdf; sourceTree = "<group>"; };
+               7C3965051CDD74F90094DBB8 /* Color.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Color.cpp; sourceTree = "<group>"; };
                7C486BA01AA1254B003F6F9B /* bundle-file.html */ = {isa = PBXFileReference; lastKnownFileType = text.html; path = "bundle-file.html"; sourceTree = "<group>"; };
                7C54A4BC1AA11CCA00380F78 /* WKBundleFileHandle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKBundleFileHandle.cpp; sourceTree = "<group>"; };
                7C54A4BF1AA11CE400380F78 /* WKBundleFileHandle_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = WKBundleFileHandle_Bundle.cpp; sourceTree = "<group>"; };
                                41973B5C1AF22875006C7B36 /* SharedBuffer.cpp */,
                                CDC2C7141797089D00E627FB /* TimeRanges.cpp */,
                                440A1D3814A0103A008A66F2 /* URL.cpp */,
+                               7C3965051CDD74F90094DBB8 /* Color.cpp */,
                        );
                        path = WebCore;
                        sourceTree = "<group>";
                                TargetAttributes = {
                                        7CCE7E8B1A41144E00447C4C = {
                                                CreatedOnToolsVersion = 6.3;
+                                               ProvisioningStyle = Manual;
+                                       };
+                                       8DD76F960486AA7600D96B5E = {
+                                               ProvisioningStyle = Manual;
                                        };
                                        A13EBB481B87339E00097110 = {
                                                CreatedOnToolsVersion = 7.0;
+                                               ProvisioningStyle = Manual;
+                                       };
+                                       BC57597F126E74AF006F0F12 = {
+                                               ProvisioningStyle = Manual;
                                        };
                                };
                        };
                                7CCE7F3F1A411B8E00447C4C /* Ref.cpp in Sources */,
                                7CCE7F401A411B8E00447C4C /* RefCounter.cpp in Sources */,
                                7CCE7F411A411B8E00447C4C /* RefPtr.cpp in Sources */,
+                               7C3965061CDD74F90094DBB8 /* Color.cpp in Sources */,
                                7CCE7F0D1A411AE600447C4C /* ReloadPageAfterCrash.cpp in Sources */,
                                7CCE7EC91A411A7E00447C4C /* RenderedImageFromDOMNode.mm in Sources */,
                                83CF1C301C4F1B8B00688447 /* StringUtilities.mm in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WebCore/Color.cpp b/Tools/TestWebKitAPI/Tests/WebCore/Color.cpp
new file mode 100644 (file)
index 0000000..e8e6820
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "Test.h"
+#include <WebCore/Color.h>
+
+using namespace WebCore;
+
+namespace TestWebKitAPI {
+
+TEST(Color, RGBToHSV_White)
+{
+    Color color = Color::white;
+    
+    double h = 0;
+    double s = 0;
+    double v = 0;
+    color.getHSV(h, s, v);
+
+    EXPECT_DOUBLE_EQ(0, h);
+    EXPECT_DOUBLE_EQ(0, s);
+    EXPECT_DOUBLE_EQ(1, v);
+}
+
+TEST(Color, RGBToHSV_Black)
+{
+    Color color = Color::black;
+    
+    double h = 0;
+    double s = 0;
+    double v = 0;
+    color.getHSV(h, s, v);
+
+    EXPECT_DOUBLE_EQ(0, h);
+    EXPECT_DOUBLE_EQ(0, s);
+    EXPECT_DOUBLE_EQ(0, v);
+}
+
+TEST(Color, RGBToHSV_Red)
+{
+    Color color(255, 0, 0);
+    
+    double h = 0;
+    double s = 0;
+    double v = 0;
+    color.getHSV(h, s, v);
+
+    EXPECT_DOUBLE_EQ(0, h);
+    EXPECT_DOUBLE_EQ(1, s);
+    EXPECT_DOUBLE_EQ(1, v);
+}
+
+TEST(Color, RGBToHSV_Green)
+{
+    Color color(0, 255, 0);
+    
+    double h = 0;
+    double s = 0;
+    double v = 0;
+    color.getHSV(h, s, v);
+
+    EXPECT_DOUBLE_EQ(0.33333333333333331, h);
+    EXPECT_DOUBLE_EQ(1, s);
+    EXPECT_DOUBLE_EQ(1, v);
+}
+
+TEST(Color, RGBToHSV_Blue)
+{
+    Color color(0, 0, 255);
+    
+    double h = 0;
+    double s = 0;
+    double v = 0;
+    color.getHSV(h, s, v);
+
+    EXPECT_DOUBLE_EQ(0.66666666666666663, h);
+    EXPECT_DOUBLE_EQ(1, s);
+    EXPECT_DOUBLE_EQ(1, v);
+}
+
+TEST(Color, RGBToHSV_DarkGray)
+{
+    Color color = Color::darkGray;
+    
+    double h = 0;
+    double s = 0;
+    double v = 0;
+    color.getHSV(h, s, v);
+
+    EXPECT_DOUBLE_EQ(0, h);
+    EXPECT_DOUBLE_EQ(0, s);
+    EXPECT_DOUBLE_EQ(0.50196078431372548, v);
+}
+
+TEST(Color, RGBToHSV_Gray)
+{
+    Color color = Color::gray;
+    
+    double h = 0;
+    double s = 0;
+    double v = 0;
+    color.getHSV(h, s, v);
+
+    EXPECT_DOUBLE_EQ(0, h);
+    EXPECT_DOUBLE_EQ(0, s);
+    EXPECT_DOUBLE_EQ(0.62745098039215685, v);
+}
+
+TEST(Color, RGBToHSV_LightGray)
+{
+    Color color = Color::lightGray;
+    
+    double h = 0;
+    double s = 0;
+    double v = 0;
+    color.getHSV(h, s, v);
+
+    EXPECT_DOUBLE_EQ(0, h);
+    EXPECT_DOUBLE_EQ(0, s);
+    EXPECT_DOUBLE_EQ(0.75294117647058822, v);
+}
+
+} // namespace TestWebKitAPI