Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / ios / chrome / browser / ui / background_generator.mm
bloba115d830106b0a99bacda307e2caf5a9e5f01d5f
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 #include "ios/chrome/browser/ui/background_generator.h"
7 #import <QuartzCore/QuartzCore.h>
9 #include "base/mac/bundle_locations.h"
10 #include "base/mac/foundation_util.h"
11 #include "base/mac/scoped_cftyperef.h"
12 #import "base/mac/scoped_nsobject.h"
13 #import "ios/chrome/browser/ui/ui_util.h"
15 // This is a utility function that may be used as a standalone helper function
16 // to generate a radial gradient UIImage.
17 UIImage* GetRadialGradient(CGRect backgroundRect,
18                            CGPoint centerPoint,
19                            CGFloat radius,
20                            CGFloat centerColor,
21                            CGFloat outsideColor,
22                            UIImage* tileImage,
23                            UIImage* logoImage) {
24   UIGraphicsBeginImageContextWithOptions(backgroundRect.size, YES, 0);
25   CGContextRef context = UIGraphicsGetCurrentContext();
26   CGFloat gradient_colors[4] = {centerColor, 1.0, outsideColor, 1.0};
27   const size_t kColorCount = 2;
28   base::ScopedCFTypeRef<CGColorSpaceRef> grey_space(
29       CGColorSpaceCreateDeviceGray());
30   DCHECK_EQ(2u, CGColorSpaceGetNumberOfComponents(grey_space));
31   base::ScopedCFTypeRef<CGGradientRef> gradient(
32       CGGradientCreateWithColorComponents(grey_space, gradient_colors, nullptr,
33                                           kColorCount));
34   CGContextDrawRadialGradient(context, gradient, centerPoint, 0, centerPoint,
35                               radius, kCGGradientDrawsAfterEndLocation);
36   if (tileImage)
37     [tileImage drawAsPatternInRect:backgroundRect];
38   if (logoImage) {
39     CGPoint corner = AlignPointToPixel(
40         CGPointMake(centerPoint.x - logoImage.size.width / 2.0,
41                     centerPoint.y - logoImage.size.height / 2.0));
42     [logoImage drawAtPoint:corner];
43   }
44   UIImage* background = UIGraphicsGetImageFromCurrentImageContext();
45   UIGraphicsEndImageContext();
46   return background;
49 void InstallBackgroundInView(UIView* view) {
50   UIImageView* imageView =
51       [[[UIImageView alloc] initWithFrame:view.bounds] autorelease];
52   imageView.image = [UIImage imageNamed:@"stack_view_background_noise.jpg"];
53   imageView.contentMode = UIViewContentModeScaleAspectFill;
54   imageView.autoresizingMask =
55       UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
56   [view insertSubview:imageView atIndex:0];