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 <ApplicationServices/ApplicationServices.h>
33
34 namespace WebCore {
35
Color(CGColorRef color)36 Color::Color(CGColorRef color)
37 {
38 if (!color) {
39 m_color = 0;
40 m_valid = false;
41 return;
42 }
43
44 size_t numComponents = CGColorGetNumberOfComponents(color);
45 const CGFloat* components = CGColorGetComponents(color);
46
47 float r = 0;
48 float g = 0;
49 float b = 0;
50 float a = 0;
51
52 switch (numComponents) {
53 case 2:
54 r = g = b = components[0];
55 a = components[1];
56 break;
57 case 4:
58 r = components[0];
59 g = components[1];
60 b = components[2];
61 a = components[3];
62 break;
63 default:
64 ASSERT_NOT_REACHED();
65 }
66
67 m_color = makeRGBA(r * 255, g * 255, b * 255, a * 255);
68 }
69
70 #if PLATFORM(WIN_OS)
71
createCGColor(const Color & c)72 CGColorRef createCGColor(const Color& c)
73 {
74 CGColorRef color = NULL;
75 CMProfileRef prof = NULL;
76 CMGetSystemProfile(&prof);
77
78 CGColorSpaceRef rgbSpace = CGColorSpaceCreateWithPlatformColorSpace(prof);
79
80 if (rgbSpace != NULL)
81 {
82 float components[4] = {c.red() / 255.0f, c.green() / 255.0f, c.blue() / 255.0f, c.alpha() / 255.0f};
83 color = CGColorCreate(rgbSpace, components);
84 CGColorSpaceRelease(rgbSpace);
85 }
86
87 CMCloseProfile(prof);
88
89 return color;
90 }
91
92 #endif // PLATFORM(WIN_OS)
93
94 }
95
96 #endif // PLATFORM(CG)
97