@@ -29,6 +29,7 @@ import UIKit
2929
3030 @IBInspectable open var numberOfPages : Int = 0 {
3131 didSet {
32+ populateTintColors ( )
3233 updateNumberOfPages ( numberOfPages)
3334 self . isHidden = hidesForSinglePage && numberOfPages <= 1
3435 }
@@ -83,6 +84,15 @@ import UIKit
8384 setNeedsLayout ( )
8485 }
8586 }
87+
88+ open var tintColors : [ UIColor ] = [ ] {
89+ didSet {
90+ guard tintColors. count == numberOfPages else {
91+ fatalError ( " The number of tint colors needs to be the same as the number of page " )
92+ }
93+ setNeedsLayout ( )
94+ }
95+ }
8696
8797 @IBInspectable open var currentPageTintColor : UIColor ? {
8898 didSet {
@@ -122,6 +132,35 @@ import UIKit
122132 }
123133 }
124134
135+ func tintColor( position: Int ) -> UIColor {
136+ if tintColors. count < numberOfPages {
137+ return tintColor
138+ } else {
139+ return tintColors [ position]
140+ }
141+ }
142+
143+ open func insertTintColor( _ color: UIColor , position: Int ) {
144+ if tintColors. count < numberOfPages {
145+ setupTintColors ( )
146+ }
147+ tintColors [ position] = color
148+ }
149+
150+ private func setupTintColors( ) {
151+ tintColors = Array < UIColor > ( repeating: tintColor, count: numberOfPages)
152+ }
153+
154+ private func populateTintColors( ) {
155+ guard tintColors. count > 0 else { return }
156+
157+ if tintColors. count > numberOfPages {
158+ tintColors = Array ( tintColors. prefix ( numberOfPages) )
159+ } else if tintColors. count < numberOfPages {
160+ tintColors. append ( contentsOf: Array < UIColor > ( repeating: tintColor, count: numberOfPages - tintColors. count) )
161+ }
162+ }
163+
125164 func animate( ) {
126165 guard let moveToProgress = self . moveToProgress else { return }
127166
@@ -159,3 +198,17 @@ import UIKit
159198 fatalError ( " Should be implemented in child class " )
160199 }
161200}
201+
202+ extension CHIBasePageControl {
203+ internal func blend( color1: UIColor , color2: UIColor , progress: CGFloat ) -> UIColor {
204+ let l1 = 1 - progress
205+ let l2 = progress
206+ var ( r1, g1, b1, a1) : ( CGFloat , CGFloat , CGFloat , CGFloat ) = ( 0 , 0 , 0 , 0 )
207+ var ( r2, g2, b2, a2) : ( CGFloat , CGFloat , CGFloat , CGFloat ) = ( 0 , 0 , 0 , 0 )
208+
209+ color1. getRed ( & r1, green: & g1, blue: & b1, alpha: & a1)
210+ color2. getRed ( & r2, green: & g2, blue: & b2, alpha: & a2)
211+
212+ return UIColor ( red: l1*r1 + l2*r2, green: l1*g1 + l2*g2, blue: l1*b1 + l2*b2, alpha: l1*a1 + l2*a2)
213+ }
214+ }
0 commit comments