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 "ui/gfx/mac/coordinate_conversion.h"
14 NSScreen* GetNSScreenFromBounds(const gfx::Rect& bounds) {
15 NSScreen* screen = nil;
18 for (NSScreen* monitor in [NSScreen screens]) {
19 gfx::Rect monitor_rect = gfx::ScreenRectFromNSRect([monitor frame]);
20 gfx::Rect overlap_rect = gfx::IntersectRects(monitor_rect, bounds);
21 int overlap_size = overlap_rect.width() * overlap_rect.height();
22 if (overlap_size > overlap) {
23 overlap = overlap_size;
35 bool GetDisplayColorProfile(const gfx::Rect& bounds,
36 std::vector<char>* profile) {
37 DCHECK(profile->empty());
39 NSScreen* screen = GetNSScreenFromBounds(bounds);
40 if (!screen || bounds.IsEmpty())
42 NSColorSpace* color_space = [screen colorSpace];
46 if ([color_space isEqual:[NSColorSpace sRGBColorSpace]])
48 NSData* profile_data = [color_space ICCProfileData];
49 const char* data = static_cast<const char*>([profile_data bytes]);
50 size_t length = [profile_data length];
51 if (data && !gfx::InvalidColorProfileLength(length))
52 profile->assign(data, data + length);
56 void ReadColorProfile(std::vector<char>* profile) {
57 CGColorSpaceRef monitor_color_space(base::mac::GetSystemColorSpace());
58 base::ScopedCFTypeRef<CFDataRef> icc_profile(
59 CGColorSpaceCopyICCProfile(monitor_color_space));
62 size_t length = CFDataGetLength(icc_profile);
63 if (gfx::InvalidColorProfileLength(length))
65 const unsigned char* data = CFDataGetBytePtr(icc_profile);
66 profile->assign(data, data + length);