Title: [200659] trunk
Revision
200659
Author
wei...@apple.com
Date
2016-05-10 17:16:14 -0700 (Tue, 10 May 2016)

Log Message

Tweak underline style for data detected links
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.

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (200658 => 200659)


--- trunk/Source/WebCore/ChangeLog	2016-05-11 00:08:50 UTC (rev 200658)
+++ trunk/Source/WebCore/ChangeLog	2016-05-11 00:16:14 UTC (rev 200659)
@@ -1,3 +1,23 @@
+2016-05-10  Sam Weinig  <s...@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  <achristen...@webkit.org>
 
         Handle _schemeUpgraded delegate callbacks in NSURLSessionDataDelegate

Modified: trunk/Source/WebCore/editing/cocoa/DataDetection.mm (200658 => 200659)


--- trunk/Source/WebCore/editing/cocoa/DataDetection.mm	2016-05-11 00:08:50 UTC (rev 200658)
+++ trunk/Source/WebCore/editing/cocoa/DataDetection.mm	2016-05-11 00:16:14 UTC (rev 200659)
@@ -639,8 +639,17 @@
                 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());
                     }

Modified: trunk/Source/WebCore/platform/graphics/Color.cpp (200658 => 200659)


--- trunk/Source/WebCore/platform/graphics/Color.cpp	2016-05-11 00:08:50 UTC (rev 200658)
+++ trunk/Source/WebCore/platform/graphics/Color.cpp	2016-05-11 00:16:14 UTC (rev 200659)
@@ -422,15 +422,16 @@
     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,14 +440,45 @@
     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)
 {
     int alpha = alphaChannel(pixelColor);

Modified: trunk/Source/WebCore/platform/graphics/Color.h (200658 => 200659)


--- trunk/Source/WebCore/platform/graphics/Color.h	2016-05-11 00:08:50 UTC (rev 200658)
+++ trunk/Source/WebCore/platform/graphics/Color.h	2016-05-11 00:16:14 UTC (rev 200659)
@@ -147,6 +147,7 @@
     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;

Modified: trunk/Tools/ChangeLog (200658 => 200659)


--- trunk/Tools/ChangeLog	2016-05-11 00:08:50 UTC (rev 200658)
+++ trunk/Tools/ChangeLog	2016-05-11 00:16:14 UTC (rev 200659)
@@ -1,3 +1,15 @@
+2016-05-10  Sam Weinig  <s...@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  <fpi...@apple.com>
 
         Internal JSC profiler should have a timestamped log of events for each code block

Modified: trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj (200658 => 200659)


--- trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2016-05-11 00:08:50 UTC (rev 200658)
+++ trunk/Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj	2016-05-11 00:16:14 UTC (rev 200659)
@@ -110,6 +110,7 @@
 		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 */; };
@@ -694,6 +695,7 @@
 		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>"; };
@@ -1102,6 +1104,7 @@
 				41973B5C1AF22875006C7B36 /* SharedBuffer.cpp */,
 				CDC2C7141797089D00E627FB /* TimeRanges.cpp */,
 				440A1D3814A0103A008A66F2 /* URL.cpp */,
+				7C3965051CDD74F90094DBB8 /* Color.cpp */,
 			);
 			path = WebCore;
 			sourceTree = "<group>";
@@ -1726,10 +1729,18 @@
 				TargetAttributes = {
 					7CCE7E8B1A41144E00447C4C = {
 						CreatedOnToolsVersion = 6.3;
+						ProvisioningStyle = Manual;
 					};
+					8DD76F960486AA7600D96B5E = {
+						ProvisioningStyle = Manual;
+					};
 					A13EBB481B87339E00097110 = {
 						CreatedOnToolsVersion = 7.0;
+						ProvisioningStyle = Manual;
 					};
+					BC57597F126E74AF006F0F12 = {
+						ProvisioningStyle = Manual;
+					};
 				};
 			};
 			buildConfigurationList = 1DEB927808733DD40010E9CD /* Build configuration list for PBXProject "TestWebKitAPI" */;
@@ -1895,6 +1906,7 @@
 				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 */,

Added: trunk/Tools/TestWebKitAPI/Tests/WebCore/Color.cpp (0 => 200659)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/Color.cpp	                        (rev 0)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/Color.cpp	2016-05-11 00:16:14 UTC (rev 200659)
@@ -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
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to