1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/color_profile.h"
7 #import <Cocoa/Cocoa.h>
9 #include "base/mac/mac_util.h"
10 #include "base/mac/scoped_cftyperef.h"
11 #include "ui/gfx/mac/coordinate_conversion.h"
15 NSScreen* GetNSScreenFromBounds(const gfx::Rect& bounds) {
16 NSScreen* screen = nil;
19 for (NSScreen* monitor in [NSScreen screens]) {
20 gfx::Rect monitor_rect = gfx::ScreenRectFromNSRect([monitor frame]);
21 gfx::Rect overlap_rect = gfx::IntersectRects(monitor_rect, bounds);
22 int overlap_size = overlap_rect.width() * overlap_rect.height();
23 if (overlap_size > overlap) {
24 overlap = overlap_size;
36 bool GetDisplayColorProfile(const gfx::Rect& bounds,
37 std::vector<char>* profile) {
38 DCHECK(profile->empty());
40 NSScreen* screen = GetNSScreenFromBounds(bounds);
41 if (!screen || bounds.IsEmpty())
43 NSColorSpace* color_space = [screen colorSpace];
47 if ([color_space isEqual:[NSColorSpace sRGBColorSpace]])
49 NSData* profile_data = [color_space ICCProfileData];
50 const char* data = static_cast<const char*>([profile_data bytes]);
51 size_t length = [profile_data length];
52 if (data && !gfx::InvalidColorProfileLength(length))
53 profile->assign(data, data + length);
57 GFX_EXPORT bool GetDisplayColorProfile(gfx::NativeWindow window,
58 std::vector<char>* profile) {
59 DCHECK(profile->empty());
61 NSColorSpace* color_space = [window colorSpace];
62 if (!color_space || NSIsEmptyRect([window frame]))
65 if ([color_space isEqual:[NSColorSpace sRGBColorSpace]])
67 NSData* profile_data = [color_space ICCProfileData];
68 const char* data = static_cast<const char*>([profile_data bytes]);
69 size_t length = [profile_data length];
70 if (data && !gfx::InvalidColorProfileLength(length))
71 profile->assign(data, data + length);
75 void ReadColorProfile(std::vector<char>* profile) {
76 CGColorSpaceRef monitor_color_space(base::mac::GetSystemColorSpace());
77 base::ScopedCFTypeRef<CFDataRef> icc_profile(
78 CGColorSpaceCopyICCProfile(monitor_color_space));
81 size_t length = CFDataGetLength(icc_profile);
82 if (gfx::InvalidColorProfileLength(length))
84 const unsigned char* data = CFDataGetBytePtr(icc_profile);
85 profile->assign(data, data + length);