Make WebKitGraphics able to handle grayscale colors
authoraroben@apple.com <aroben@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 27 Nov 2007 02:20:03 +0000 (02:20 +0000)
committeraroben@apple.com <aroben@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 27 Nov 2007 02:20:03 +0000 (02:20 +0000)
 WebCore:

         Add a Color(CGColorRef) constructor

         Reviewed by Darin.

         * platform/graphics/Color.h:
         * platform/graphics/cg/ColorCG.cpp:
         (WebCore::Color::Color): Added.

 WebKit/win:

         Take advantage of the new Color constructor that takes a CGColorRef

         This lets us handle grayscale colors (which only have 2 components).

         Reviewed by Darin.

         * WebKitGraphics.cpp:
         (DrawTextAtPoint):

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

WebCore/ChangeLog
WebCore/platform/graphics/Color.h
WebCore/platform/graphics/cg/ColorCG.cpp
WebKit/win/ChangeLog
WebKit/win/WebKitGraphics.cpp

index e4a8a6d..3a0f95b 100644 (file)
@@ -1,3 +1,13 @@
+2007-11-26  Adam Roben  <aroben@apple.com>
+
+        Add a Color(CGColorRef) constructor
+
+        Reviewed by Darin.
+
+        * platform/graphics/Color.h:
+        * platform/graphics/cg/ColorCG.cpp:
+        (WebCore::Color::Color): Added.
+
 2007-11-26  Adele Peterson  <adele@apple.com>
 
         Reviewed by Darin.
index dcca6a5..5706c6f 100644 (file)
@@ -105,6 +105,10 @@ public:
     operator wxColour() const;
 #endif
 
+#if PLATFORM(CG)
+    Color(CGColorRef);
+#endif
+
     static bool parseHexColor(const String& name, RGBA32& rgb);
 
     static const RGBA32 black = 0xFF000000;
index 9dfa93c..48ce9f2 100644 (file)
 
 namespace WebCore {
 
+Color::Color(CGColorRef color)
+{
+    if (!color) {
+        m_color = 0;
+        m_valid = false;
+        return;
+    }
+
+    size_t numComponents = CGColorGetNumberOfComponents(color);
+    const CGFloat* components = CGColorGetComponents(color);
+
+    float r = 0;
+    float g = 0;
+    float b = 0;
+    float a = 0;
+
+    switch (numComponents) {
+    case 2:
+        r = g = b = components[0];
+        a = components[1];
+        break;
+    case 4:
+        r = components[0];
+        g = components[1];
+        b = components[2];
+        a = components[3];
+        break;
+    default:
+        ASSERT_NOT_REACHED();
+    }
+
+    m_color = makeRGBA(r * 255, g * 255, b * 255, a * 255);
+}
+
 #if !PLATFORM(MAC)
 
 CGColorRef cgColor(const Color& c)
index 952e7a9..924cadc 100644 (file)
@@ -1,3 +1,14 @@
+2007-11-26  Adam Roben  <aroben@apple.com>
+
+        Take advantage of the new Color constructor that takes a CGColorRef
+
+        This lets us handle grayscale colors (which only have 2 components).
+
+        Reviewed by Darin.
+
+        * WebKitGraphics.cpp:
+        (DrawTextAtPoint):
+
 2007-11-26  Steve Falkenburg  <sfalken@apple.com>
 
         Build fix.
index 66ec08e..5953644 100644 (file)
@@ -74,14 +74,10 @@ void DrawTextAtPoint(CGContextRef cgContext, LPCTSTR text, int length, POINT poi
 {
     GraphicsContext context(cgContext);
 
-    ASSERT(CGColorGetNumberOfComponents(color) == 4);   // this code assumes the CGColorRef has 4 components
-    const CGFloat* components = CGColorGetComponents(color);
-    Color textColor((int)(components[0] * 255), (int)(components[1] * 255), (int)(components[2] * 255), (int)(components[3] * 255));
-
     String drawString(text, length);
     if (drawAsPassword)
         drawString = drawString.impl()->secure(WebCore::bullet);
-    WebCoreDrawTextAtPoint(context, drawString, point, makeFont(description), textColor, underlinedIndex);
+    WebCoreDrawTextAtPoint(context, drawString, point, makeFont(description), color, underlinedIndex);
 }
 
 void WebDrawText(WebTextRenderInfo* info)