Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / ui / image_util.mm
blobe1f297b47d51d28551e22daee88d2e2f3d536ea6
1 // Copyright 2012 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 #import <UIKit/UIKit.h>
7 #import "ios/chrome/browser/ui/image_util.h"
9 #include "ui/gfx/color_analysis.h"
10 #include "ui/gfx/image/image.h"
12 UIColor* DominantColorForImage(const gfx::Image& image, CGFloat opacity) {
13   SkColor color = color_utils::CalculateKMeanColorOfBitmap(*image.ToSkBitmap());
14   UIColor* result = [UIColor colorWithRed:SkColorGetR(color) / 255.0
15                                     green:SkColorGetG(color) / 255.0
16                                      blue:SkColorGetB(color) / 255.0
17                                     alpha:opacity];
18   return result;
21 UIImage* StretchableImageFromUIImage(UIImage* image,
22                                      NSInteger left_cap_width,
23                                      NSInteger top_cap_height) {
24   UIEdgeInsets insets =
25       UIEdgeInsetsMake(top_cap_height, left_cap_width,
26                        image.size.height - top_cap_height + 1.0,
27                        image.size.width - left_cap_width + 1.0);
28   return [image resizableImageWithCapInsets:insets];
31 UIImage* StretchableImageNamed(NSString* name) {
32   UIImage* image = [UIImage imageNamed:name];
33   if (!image)
34     return nil;
35   // Returns a copy of |image| configured to stretch at the center pixel.
36   return StretchableImageFromUIImage(image, floor(image.size.width / 2.0),
37                                      floor(image.size.height / 2.0));
40 UIImage* StretchableImageNamed(NSString* name,
41                                NSInteger left_cap_width,
42                                NSInteger top_cap_height) {
43   UIImage* image = [UIImage imageNamed:name];
44   if (!image)
45     return nil;
47   return StretchableImageFromUIImage(image, left_cap_width, top_cap_height);