Title: [246042] trunk
Revision
246042
Author
[email protected]
Date
2019-06-03 12:14:42 -0700 (Mon, 03 Jun 2019)

Log Message

Tweak the text and underline color for data detected text.
https://bugs.webkit.org/show_bug.cgi?id=198487
rdar://problem/50667125

Reviewed by Devin Rousso.

Source/WebCore:

Tests: Color.RGBToHSL API tests

* editing/cocoa/DataDetection.mm:
(WebCore::DataDetection::detectContentInRange): Use currentcolor so semantic text colors work.
Force the lightness of the underline color to the middle, and multiply the alpha by 38%,
so the color will appear on light and dark backgrounds, since only one color can be specified.
* platform/graphics/Color.cpp:
(WebCore::Color::getHSL const): Return hue in [0...6) range to easily round-trip with makeRGBAFromHSLA().

Tools:

* TestWebKitAPI/Tests/WebCore/Color.cpp:
(TestWebKitAPI::TEST): Added Color.RGBToHSL tests.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (246041 => 246042)


--- trunk/Source/WebCore/ChangeLog	2019-06-03 18:42:34 UTC (rev 246041)
+++ trunk/Source/WebCore/ChangeLog	2019-06-03 19:14:42 UTC (rev 246042)
@@ -1,3 +1,20 @@
+2019-06-03  Timothy Hatcher  <[email protected]>
+
+        Tweak the text and underline color for data detected text.
+        https://bugs.webkit.org/show_bug.cgi?id=198487
+        rdar://problem/50667125
+
+        Reviewed by Devin Rousso.
+
+        Tests: Color.RGBToHSL API tests
+
+        * editing/cocoa/DataDetection.mm:
+        (WebCore::DataDetection::detectContentInRange): Use currentcolor so semantic text colors work.
+        Force the lightness of the underline color to the middle, and multiply the alpha by 38%,
+        so the color will appear on light and dark backgrounds, since only one color can be specified.
+        * platform/graphics/Color.cpp:
+        (WebCore::Color::getHSL const): Return hue in [0...6) range to easily round-trip with makeRGBAFromHSLA().
+
 2019-06-03  Don Olmstead  <[email protected]>
 
         [CMake] Add WebKit::_javascript_Core target

Modified: trunk/Source/WebCore/editing/cocoa/DataDetection.mm (246041 => 246042)


--- trunk/Source/WebCore/editing/cocoa/DataDetection.mm	2019-06-03 18:42:34 UTC (rev 246041)
+++ trunk/Source/WebCore/editing/cocoa/DataDetection.mm	2019-06-03 19:14:42 UTC (rev 246042)
@@ -580,12 +580,14 @@
             auto* parentNode = range->startContainer().parentNode();
             if (!parentNode)
                 continue;
+
             if (!is<Text>(range->startContainer()))
                 continue;
+
             auto& currentTextNode = downcast<Text>(range->startContainer());
             Document& document = currentTextNode.document();
             String textNodeData;
-            
+
             if (lastTextNodeToUpdate != &currentTextNode) {
                 if (lastTextNodeToUpdate)
                     lastTextNodeToUpdate->setData(lastNodeContent);
@@ -594,12 +596,12 @@
                     textNodeData = currentTextNode.data().substring(0, range->startOffset());
             } else
                 textNodeData = currentTextNode.data().substring(contentOffset, range->startOffset() - contentOffset);
-            
+
             if (!textNodeData.isEmpty()) {
                 parentNode->insertBefore(Text::create(document, textNodeData), &currentTextNode);
                 contentOffset = range->startOffset();
             }
-            
+
             // Create the actual anchor node and insert it before the current node.
             textNodeData = currentTextNode.data().substring(range->startOffset(), range->endOffset() - range->startOffset());
             Ref<Text> newTextNode = Text::create(document, textNodeData);
@@ -608,35 +610,31 @@
             Ref<HTMLAnchorElement> anchorElement = HTMLAnchorElement::create(document);
             anchorElement->setHref(correspondingURL);
             anchorElement->setDir("ltr");
+            anchorElement->setInlineStyleProperty(CSSPropertyColor, CSSValueCurrentcolor);
+
             if (shouldUseLightLinks) {
                 document.updateStyleIfNeeded();
+
                 auto* renderStyle = parentNode->computedStyle();
                 if (renderStyle) {
                     auto textColor = renderStyle->visitedDependentColor(CSSPropertyColor);
                     if (textColor.isValid()) {
-                        double h = 0;
-                        double s = 0;
-                        double v = 0;
-                        textColor.getHSV(h, s, v);
+                        double hue, saturation, lightness;
+                        textColor.getHSL(hue, saturation, lightness);
 
-                        // 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));
+                        // Force the lightness of the underline color to the middle, and multiply the alpha by 38%,
+                        // so the color will appear on light and dark backgrounds, since only one color can be specified.
+                        double overrideLightness = 0.5;
+                        double overrideAlphaMultiplier = 0.38;
+                        auto underlineColor = Color(makeRGBAFromHSLA(hue, saturation, overrideLightness, overrideAlphaMultiplier * textColor.alphaAsFloat()));
 
-                        anchorElement->setInlineStyleProperty(CSSPropertyColor, textColor.cssText());
                         anchorElement->setInlineStyleProperty(CSSPropertyTextDecorationColor, underlineColor.cssText());
                     }
                 }
-            } else if (is<StyledElement>(*parentNode)) {
-                if (auto* style = downcast<StyledElement>(*parentNode).presentationAttributeStyle()) {
-                    String color = style->getPropertyValue(CSSPropertyColor);
-                    if (!color.isEmpty())
-                        anchorElement->setInlineStyleProperty(CSSPropertyColor, color);
-                }
             }
+
             anchorElement->appendChild(WTFMove(newTextNode));
+
             // Add a special attribute to mark this URLification as the result of data detectors.
             anchorElement->setAttributeWithoutSynchronization(x_apple_data_detectorsAttr, AtomicString("true", AtomicString::ConstructFromLiteral));
             anchorElement->setAttributeWithoutSynchronization(x_apple_data_detectors_typeAttr, dataDetectorTypeForCategory(softLink_DataDetectorsCore_DDResultGetCategory(coreResult)));
@@ -645,11 +643,12 @@
             parentNode->insertBefore(WTFMove(anchorElement), &currentTextNode);
 
             contentOffset = range->endOffset();
-            
+
             lastNodeContent = currentTextNode.data().substring(range->endOffset(), currentTextNode.length() - range->endOffset());
             lastTextNodeToUpdate = &currentTextNode;
         }        
     }
+
     if (lastTextNodeToUpdate)
         lastTextNodeToUpdate->setData(lastNodeContent);
     

Modified: trunk/Source/WebCore/platform/graphics/Color.cpp (246041 => 246042)


--- trunk/Source/WebCore/platform/graphics/Color.cpp	2019-06-03 18:42:34 UTC (rev 246041)
+++ trunk/Source/WebCore/platform/graphics/Color.cpp	2019-06-03 19:14:42 UTC (rev 246042)
@@ -542,7 +542,7 @@
 {
     // http://en.wikipedia.org/wiki/HSL_color_space. This is a direct copy of
     // the algorithm therein, although it's 360^o based and we end up wanting
-    // [0...1) based. It's clearer if we stick to 360^o until the end.
+    // [0...6) based. It's clearer if we stick to 360^o until the end.
     double r = static_cast<double>(red()) / 255.0;
     double g = static_cast<double>(green()) / 255.0;
     double b = static_cast<double>(blue()) / 255.0;
@@ -562,8 +562,8 @@
     if (hue >= 360.0)
         hue -= 360.0;
 
-    // makeRGBAFromHSLA assumes that hue is in [0...1).
-    hue /= 360.0;
+    // makeRGBAFromHSLA assumes that hue is in [0...6).
+    hue /= 60.0;
 
     lightness = 0.5 * (max + min);
     if (!chroma)

Modified: trunk/Tools/ChangeLog (246041 => 246042)


--- trunk/Tools/ChangeLog	2019-06-03 18:42:34 UTC (rev 246041)
+++ trunk/Tools/ChangeLog	2019-06-03 19:14:42 UTC (rev 246042)
@@ -1,3 +1,14 @@
+2019-06-03  Timothy Hatcher  <[email protected]>
+
+        Tweak the text and underline color for data detected text.
+        https://bugs.webkit.org/show_bug.cgi?id=198487
+        rdar://problem/50667125
+
+        Reviewed by Devin Rousso.
+
+        * TestWebKitAPI/Tests/WebCore/Color.cpp:
+        (TestWebKitAPI::TEST): Added Color.RGBToHSL tests.
+
 2019-06-03  Don Olmstead  <[email protected]>
 
         [CMake] Add WebKit::_javascript_Core target

Modified: trunk/Tools/TestWebKitAPI/Tests/WebCore/Color.cpp (246041 => 246042)


--- trunk/Tools/TestWebKitAPI/Tests/WebCore/Color.cpp	2019-06-03 18:42:34 UTC (rev 246041)
+++ trunk/Tools/TestWebKitAPI/Tests/WebCore/Color.cpp	2019-06-03 19:14:42 UTC (rev 246042)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
+ * Copyright (C) 2011, 2012, 2019 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -144,6 +144,102 @@
     EXPECT_DOUBLE_EQ(0.75294117647058822, v);
 }
 
+TEST(Color, RGBToHSL_White)
+{
+    Color color = Color::white;
+
+    double hue, saturation, lightness;
+    color.getHSL(hue, saturation, lightness);
+
+    EXPECT_DOUBLE_EQ(0, hue);
+    EXPECT_DOUBLE_EQ(0, saturation);
+    EXPECT_DOUBLE_EQ(1, lightness);
+}
+
+TEST(Color, RGBToHSL_Black)
+{
+    Color color = Color::black;
+
+    double hue, saturation, lightness;
+    color.getHSL(hue, saturation, lightness);
+
+    EXPECT_DOUBLE_EQ(0, hue);
+    EXPECT_DOUBLE_EQ(0, saturation);
+    EXPECT_DOUBLE_EQ(0, lightness);
+}
+
+TEST(Color, RGBToHSL_Red)
+{
+    Color color(255, 0, 0);
+
+    double hue, saturation, lightness;
+    color.getHSL(hue, saturation, lightness);
+
+    EXPECT_DOUBLE_EQ(0, hue);
+    EXPECT_DOUBLE_EQ(1, saturation);
+    EXPECT_DOUBLE_EQ(0.5, lightness);
+}
+
+TEST(Color, RGBToHSL_Green)
+{
+    Color color(0, 255, 0);
+
+    double hue, saturation, lightness;
+    color.getHSL(hue, saturation, lightness);
+
+    EXPECT_DOUBLE_EQ(2, hue);
+    EXPECT_DOUBLE_EQ(1, saturation);
+    EXPECT_DOUBLE_EQ(0.5, lightness);
+}
+
+TEST(Color, RGBToHSL_Blue)
+{
+    Color color(0, 0, 255);
+
+    double hue, saturation, lightness;
+    color.getHSL(hue, saturation, lightness);
+
+    EXPECT_DOUBLE_EQ(4, hue);
+    EXPECT_DOUBLE_EQ(1, saturation);
+    EXPECT_DOUBLE_EQ(0.5, lightness);
+}
+
+TEST(Color, RGBToHSL_DarkGray)
+{
+    Color color = Color::darkGray;
+
+    double hue, saturation, lightness;
+    color.getHSL(hue, saturation, lightness);
+
+    EXPECT_DOUBLE_EQ(0, hue);
+    EXPECT_DOUBLE_EQ(0, saturation);
+    EXPECT_DOUBLE_EQ(0.50196078431372548, lightness);
+}
+
+TEST(Color, RGBToHSL_Gray)
+{
+    Color color = Color::gray;
+
+    double hue, saturation, lightness;
+    color.getHSL(hue, saturation, lightness);
+
+    EXPECT_DOUBLE_EQ(0, hue);
+    EXPECT_DOUBLE_EQ(0, saturation);
+    EXPECT_DOUBLE_EQ(0.62745098039215685, lightness);
+}
+
+TEST(Color, RGBToHSL_LightGray)
+{
+    Color color = Color::lightGray;
+
+    double hue, saturation, lightness;
+    color.getHSL(hue, saturation, lightness);
+
+    EXPECT_DOUBLE_EQ(0, hue);
+    EXPECT_DOUBLE_EQ(0, saturation);
+    EXPECT_DOUBLE_EQ(0.75294117647058822, lightness);
+}
+
 TEST(Color, Validity)
 {
     Color invalidColor;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to