Home | History | Annotate | Download | only in cg
      1 /*
      2  * Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "Color.h"
     28 
     29 #if PLATFORM(CG)
     30 
     31 #include <wtf/Assertions.h>
     32 #include <wtf/RetainPtr.h>
     33 #include <ApplicationServices/ApplicationServices.h>
     34 
     35 namespace WebCore {
     36 
     37 Color::Color(CGColorRef color)
     38 {
     39     if (!color) {
     40         m_color = 0;
     41         m_valid = false;
     42         return;
     43     }
     44 
     45     size_t numComponents = CGColorGetNumberOfComponents(color);
     46     const CGFloat* components = CGColorGetComponents(color);
     47 
     48     float r = 0;
     49     float g = 0;
     50     float b = 0;
     51     float a = 0;
     52 
     53     switch (numComponents) {
     54     case 2:
     55         r = g = b = components[0];
     56         a = components[1];
     57         break;
     58     case 4:
     59         r = components[0];
     60         g = components[1];
     61         b = components[2];
     62         a = components[3];
     63         break;
     64     default:
     65         ASSERT_NOT_REACHED();
     66     }
     67 
     68     m_color = makeRGBA(r * 255, g * 255, b * 255, a * 255);
     69 }
     70 
     71 #if OS(WINDOWS)
     72 
     73 CGColorRef createCGColor(const Color& c)
     74 {
     75     CGColorRef color = NULL;
     76     CMProfileRef prof = NULL;
     77     CMGetSystemProfile(&prof);
     78 
     79     RetainPtr<CGColorSpaceRef> rgbSpace(AdoptCF, CGColorSpaceCreateWithPlatformColorSpace(prof));
     80 
     81     if (rgbSpace) {
     82         CGFloat components[4] = { static_cast<CGFloat>(c.red()) / 255, static_cast<CGFloat>(c.green()) / 255,
     83                                   static_cast<CGFloat>(c.blue()) / 255, static_cast<CGFloat>(c.alpha()) / 255 };
     84         color = CGColorCreate(rgbSpace.get(), components);
     85     }
     86 
     87     CMCloseProfile(prof);
     88 
     89     return color;
     90 }
     91 
     92 #endif // OS(WINDOWS)
     93 
     94 }
     95 
     96 #endif // PLATFORM(CG)
     97