[Swift通天遁地]五、高级扩展,5获取互补色、渐变色、以及图片主题颜色

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)

➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/

➤GitHub地址:https://github.com/strengthen/LeetCode

➤原文地址:

➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。

➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

目录:[Swift]通天遁地Swift

本文将演示第三方类库“变色龙”创建互补色、渐变色、以及图片主题颜色。

首先确保在项目中已经安装了所需的第三方库。

点击【Podfile】,查看安装配置文件。

1 platform :ios, '12.0'
2 use_frameworks!
3 
4 target 'DemoApp' do
5     source 'https://github.com/CocoaPods/Specs.git'
6     pod 'ChameleonFramework/Swift', :git => 'https://github.com/ViccAlexander/Chameleon.git'
7 end

根据配置文件中的相关配置,安装第三方库。

然后点击打开【DemoApp.xcworkspace】项目文件。

在项目导航区,打开视图控制器的代码文件【ViewController.swift】

现在开始编写代码,获取互补色、渐变色、以及图片主题颜色。

  1 import UIKit
  2 //在当前的类文件中,引入已经安装的第三方类库
  3 import ChameleonFramework
  4 
  5 class ViewController: UIViewController {
  6 
  7     override func viewDidLoad() {
  8         super.viewDidLoad()
  9         // Do any additional setup after loading the view, typically from a nib.
 10 
 11         //创建互补色或对比色
 12         complementaryAndcontrasting()
 13 
 14         //用于创建渐变颜色
 15         gradientColors()
 16 
 17         //获得图片的主题色
 18         colorFromeImage()
 19     }
 20     
 21     //添加一个方法,用来生成互补色或对比色
 22     func complementaryAndcontrasting()
 23     {
 24         //初始化一个标签对象,其显示区域和根视图相同。
 25         //该标签对象的字体颜色将用来显示其背景色
 26         let label = UILabel(frame: self.view.frame)
 27         //将其放置在根视图的中心位置
 28         label.center = self.view.center
 29         //设置标签的文字内容
 30         label.text = "Complementary Color"
 31         //设置文字的对齐方式为居中对齐
 32         label.textAlignment = .center
 33         //设置标签的背景颜色为橙色
 34         label.backgroundColor = UIColor.orange
 35         
 36         //设置标签的字体颜色,为其背景颜色的互补色
 37         //label.textColor = UIColor(complementaryFlatColorOf:label.backgroundColor!)
 38         
 39         //设置标签的字体颜色,为其背景颜色的对比色
 40         label.textColor = UIColor(contrastingBlackOrWhiteColorOn: label.backgroundColor!, isFlat: true)
 41         
 42         //将标签视图添加到根视图
 43         self.view.addSubview(label)
 44     }
 45     
 46      //添加一个方法,用于创建渐变颜色
 47     func gradientColors()
 48     {
 49         //初始化一个视图对象,并设置视图 对象的显示区域
 50         let view = UIView(frame: self.view.frame)
 51         //设置视图的背景颜色为渐变颜色
 52         view.backgroundColor = UIColor(gradientStyle: .radial,//径向渐变
 53                                        withFrame: self.view.frame, 
 54                                        andColors: [.yellow, .orange])//由黄到橙
 55         view.backgroundColor = UIColor(gradientStyle: .leftToRight,
 56                                        withFrame: self.view.frame, 
 57                                        andColors: [.yellow, .orange])
 58         //将视图对象添加到根视图
 59         self.view.addSubview(view)
 60     }
 61     
 62     //添加一个方法,获得图片的主题色
 63     func colorFromeImage()
 64     {
 65         //读取项目中的一张图片素材
 66         let image = UIImage(named: "Pic")
 67         //初始化一个图像视图,用来显示图片素材
 68         let imageView = UIImageView(frame: CGRect(x: 0,
 69                                     y: 20, 
 70                                     width: 320,
 71                                     height: 220))
 72         //将图片素材赋予图像视图
 73         imageView.image = image
 74         //将图像视图添加到根视图
 75         self.view.addSubview(imageView)
 76         
 77         //从图像视图中获得一份扁平化的配色方案
 78         //let colors = ColorsFromImage(image!, withFlatScheme: true)
 79 
 80         //从图像视图中获得一份非扁平化的配色方案
 81         let colors = ColorsFromImage(image!, withFlatScheme: false)
 82         
 83         //初始化一个数值为0的变量
 84         var dy = 0
 85         //对配色方案中的颜色进行遍历,从而通过相同数量的视图
 86         //显示配色方案中的各种颜色。
 87         for color in colors
 88         {
 89             //初始化一个指定显示区域的视图
 90             dy += 40
 91             let frame = CGRect(x: 20, y: 220+dy, width: 280, height: 40)
 92             let view = UIView(frame: frame)
 93             //设置视图的背景颜色,为配色方案中的颜色
 94             view.backgroundColor = color
 95             //并将视图添加到根视图中
 96             self.view.addSubview(view)
 97         }
 98         
 99         //获得图像中的平均颜色
100         let averageColor = UIColor(averageColorFrom: image!)
101         //初始化另一个视图对象,用来显示平均颜色
102         let view = UIView(frame: CGRect(x: 20, y: 500, width: 280, height: 40))
103         //设置视图的背景颜色为平均颜色
104         view.backgroundColor = averageColor
105         //将视图添加到根视图中
106         self.view.addSubview(view)
107     }
108     
109     override func didReceiveMemoryWarning() {
110         super.didReceiveMemoryWarning()
111         // Dispose of any resources that can be recreated.
112     }
113 }