Skip to content
This repository was archived by the owner on Sep 19, 2024. It is now read-only.

Commit ce03cde

Browse files
committed
ColorResource for color assets
1 parent 594965c commit ce03cde

File tree

5 files changed

+112
-40
lines changed

5 files changed

+112
-40
lines changed

Diff for: Library/Core/ColorPaletteItemResource.swift

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//
2+
// ColorPaletteItemResource.swift
3+
// R.swift.Library
4+
//
5+
// Created by Tom Lokhorst on 2016-03-13.
6+
// From: https://github.com/mac-cain13/R.swift.Library
7+
// License: MIT License
8+
//
9+
10+
import Foundation
11+
12+
public protocol ColorPaletteItemResourceType {
13+
14+
/// Name of the color
15+
var name: String { get }
16+
17+
/// Red componenent of color
18+
var red: CGFloat { get }
19+
20+
/// Green componenent of color
21+
var green: CGFloat { get }
22+
23+
/// Blue componenent of color
24+
var blue: CGFloat { get }
25+
26+
/// Alpha componenent of color
27+
var alpha: CGFloat { get }
28+
}
29+
30+
@available(*, deprecated: 11, message: "Use color assets instead")
31+
public struct ColorPaletteItemResource: ColorPaletteItemResourceType {
32+
33+
/// Name of the color
34+
public let name: String
35+
36+
/// Red componenent of color
37+
public let red: CGFloat
38+
39+
/// Green componenent of color
40+
public let green: CGFloat
41+
42+
/// Blue componenent of color
43+
public let blue: CGFloat
44+
45+
/// Alpha componenent of color
46+
public let alpha: CGFloat
47+
48+
public init(name: String, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
49+
self.name = name
50+
self.red = red
51+
self.green = green
52+
self.blue = blue
53+
self.alpha = alpha
54+
}
55+
}

Diff for: Library/Core/ColorResource.swift

+8-29
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,23 @@ import Foundation
1111

1212
public protocol ColorResourceType {
1313

14+
/// Bundle this color is in
15+
var bundle: Bundle { get }
16+
1417
/// Name of the color
1518
var name: String { get }
16-
17-
/// Red componenent of color
18-
var red: CGFloat { get }
19-
20-
/// Green componenent of color
21-
var green: CGFloat { get }
22-
23-
/// Blue componenent of color
24-
var blue: CGFloat { get }
25-
26-
/// Alpha componenent of color
27-
var alpha: CGFloat { get }
2819
}
2920

3021
public struct ColorResource: ColorResourceType {
3122

23+
/// Bundle this color is in
24+
public let bundle: Bundle
25+
3226
/// Name of the color
3327
public let name: String
3428

35-
/// Red componenent of color
36-
public let red: CGFloat
37-
38-
/// Green componenent of color
39-
public let green: CGFloat
40-
41-
/// Blue componenent of color
42-
public let blue: CGFloat
43-
44-
/// Alpha componenent of color
45-
public let alpha: CGFloat
46-
47-
public init(name: String, red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
29+
public init(bundle: Bundle, name: String) {
30+
self.bundle = bundle
4831
self.name = name
49-
self.red = red
50-
self.green = green
51-
self.blue = blue
52-
self.alpha = alpha
5332
}
5433
}

Diff for: Library/UIKit/ColorResource+UIKit.swift renamed to Library/UIKit/ColorPaletteItemResource+UIKit.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// ColorResource+UIKit.swift
2+
// ColorPaletteItemResource+UIKit.swift
33
// R.swift.Library
44
//
55
// Created by Tom Lokhorst on 2016-04-23.
@@ -9,11 +9,11 @@
99

1010
import UIKit
1111

12-
public extension ColorResourceType {
12+
public extension ColorPaletteItemResourceType {
1313
/**
1414
Returns the a UIColor
1515

16-
- returns: A UIColor for this color resource
16+
- returns: A UIColor for this color palette item resource
1717
*/
1818
func color() -> UIColor {
1919
return UIColor(red: red, green: green, blue: blue, alpha: alpha)

Diff for: Library/UIKit/UIColor+ColorResource.swift

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//
2+
// UIColor+ColorResource.swift
3+
// R.swift.Library
4+
//
5+
// Created by Tom Lokhorst on 2017-06-06.
6+
// From: https://github.com/mac-cain13/R.swift.Library
7+
// License: MIT License
8+
//
9+
10+
import UIKit
11+
12+
@available(iOS 11.0, *)
13+
@available(tvOS 11.0, *)
14+
public extension UIColor {
15+
/**
16+
Returns the color from this resource (R.color.*) that is compatible with the trait collection.
17+
18+
- parameter resource: The resource you want the image of (R.color.*)
19+
- parameter traitCollection: Traits that describe the desired color to retrieve, pass nil to use traits that describe the main screen.
20+
21+
- returns: A color that exactly or best matches the desired traits with the given resource (R.color.*), or nil if no suitable color was found.
22+
*/
23+
public convenience init?(resource: ColorResourceType, compatibleWith traitCollection: UITraitCollection? = nil) {
24+
self.init(named: resource.name, in: resource.bundle, compatibleWith: traitCollection)
25+
}
26+
}

Diff for: R.swift.Library.xcodeproj/project.pbxproj

+20-8
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,17 @@
6363
E20F34A71C92B44100338F81 /* Data+FileResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E20F34A61C92B44100338F81 /* Data+FileResource.swift */; };
6464
E22D43671C95EEA100692FFF /* ColorResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22D43661C95EEA100692FFF /* ColorResource.swift */; };
6565
E24720CA1C96B4D100DF291D /* ColorResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E22D43661C95EEA100692FFF /* ColorResource.swift */; };
66-
E250BE941CCBCEB100CC71DE /* ColorResource+UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = E250BE931CCBCEB100CC71DE /* ColorResource+UIKit.swift */; };
67-
E250BE951CCBF58200CC71DE /* ColorResource+UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = E250BE931CCBCEB100CC71DE /* ColorResource+UIKit.swift */; };
6866
E250BE971CCBF60300CC71DE /* StringResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E250BE961CCBF60300CC71DE /* StringResource.swift */; };
6967
E250BE991CCBF7E900CC71DE /* StringResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E250BE961CCBF60300CC71DE /* StringResource.swift */; };
7068
E2B0AF361D8142A400A7196C /* Core+Migration.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2B0AF351D8142A400A7196C /* Core+Migration.swift */; };
7169
E2B0AF381D8142BF00A7196C /* UIKit+Migration.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2B0AF371D8142BF00A7196C /* UIKit+Migration.swift */; };
7270
E2B0AF3A1D81483900A7196C /* Foundation+Migration.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2B0AF391D81483900A7196C /* Foundation+Migration.swift */; };
71+
E2CA68EB1EE75151009C4DB4 /* ColorPaletteItemResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2CA68EA1EE75151009C4DB4 /* ColorPaletteItemResource.swift */; };
72+
E2CA68ED1EE751A9009C4DB4 /* ColorPaletteItemResource+UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2CA68EC1EE751A9009C4DB4 /* ColorPaletteItemResource+UIKit.swift */; };
73+
E2CA68EF1EE75992009C4DB4 /* UIColor+ColorResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2CA68EE1EE75992009C4DB4 /* UIColor+ColorResource.swift */; };
74+
E2CA68F01EE759C3009C4DB4 /* ColorPaletteItemResource+UIKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2CA68EC1EE751A9009C4DB4 /* ColorPaletteItemResource+UIKit.swift */; };
75+
E2CA68F11EE75A02009C4DB4 /* ColorPaletteItemResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2CA68EA1EE75151009C4DB4 /* ColorPaletteItemResource.swift */; };
76+
E2CA68F21EE75A49009C4DB4 /* UIColor+ColorResource.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2CA68EE1EE75992009C4DB4 /* UIColor+ColorResource.swift */; };
7377
/* End PBXBuildFile section */
7478

7579
/* Begin PBXContainerItemProxy section */
@@ -123,11 +127,13 @@
123127
D5E435AC1C3D00770091090C /* FileResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FileResource.swift; sourceTree = "<group>"; };
124128
E20F34A61C92B44100338F81 /* Data+FileResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Data+FileResource.swift"; sourceTree = "<group>"; };
125129
E22D43661C95EEA100692FFF /* ColorResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorResource.swift; sourceTree = "<group>"; };
126-
E250BE931CCBCEB100CC71DE /* ColorResource+UIKit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ColorResource+UIKit.swift"; sourceTree = "<group>"; };
127130
E250BE961CCBF60300CC71DE /* StringResource.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringResource.swift; sourceTree = "<group>"; };
128131
E2B0AF351D8142A400A7196C /* Core+Migration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Core+Migration.swift"; sourceTree = "<group>"; };
129132
E2B0AF371D8142BF00A7196C /* UIKit+Migration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIKit+Migration.swift"; sourceTree = "<group>"; };
130133
E2B0AF391D81483900A7196C /* Foundation+Migration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Foundation+Migration.swift"; sourceTree = "<group>"; };
134+
E2CA68EA1EE75151009C4DB4 /* ColorPaletteItemResource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ColorPaletteItemResource.swift; sourceTree = "<group>"; };
135+
E2CA68EC1EE751A9009C4DB4 /* ColorPaletteItemResource+UIKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ColorPaletteItemResource+UIKit.swift"; sourceTree = "<group>"; };
136+
E2CA68EE1EE75992009C4DB4 /* UIColor+ColorResource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+ColorResource.swift"; sourceTree = "<group>"; };
131137
/* End PBXFileReference section */
132138

133139
/* Begin PBXFrameworksBuildPhase section */
@@ -167,28 +173,31 @@
167173
D543F9C21C14987000D16A0C /* UIKit */ = {
168174
isa = PBXGroup;
169175
children = (
170-
E250BE931CCBCEB100CC71DE /* ColorResource+UIKit.swift */,
176+
E2CA68EC1EE751A9009C4DB4 /* ColorPaletteItemResource+UIKit.swift */,
171177
D5E435A81C3CFB460091090C /* NibResource+UIKit.swift */,
172178
D57E1EB81C3E4C1A00DDA68F /* StoryboardResourceWithInitialController+UIKit.swift */,
173179
D543F9CE1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift */,
174180
D543F9C51C14992000D16A0C /* UICollectionView+ReuseIdentifierProtocol.swift */,
181+
E2CA68EE1EE75992009C4DB4 /* UIColor+ColorResource.swift */,
175182
D57E1EB41C3D774000DDA68F /* UIFont+FontResource.swift */,
176183
D553F5861C44170E00885232 /* UIImage+ImageResource.swift */,
184+
E2B0AF371D8142BF00A7196C /* UIKit+Migration.swift */,
177185
D5588CAA1C3F9DBE00912F97 /* UINib+NibResource.swift */,
178186
D57E1EBA1C3E4C4300DDA68F /* UIStoryboard+StoryboardResource.swift */,
179187
D51335281C95A79B0014C9D4 /* UIStoryboard+StoryboardViewControllerResource.swift */,
180188
D543F9C31C1498FB00D16A0C /* UITableView+ReuseIdentifierProtocol.swift */,
181189
D543F9C71C14995800D16A0C /* UIViewController+NibResource.swift */,
182190
D543F9C91C14998800D16A0C /* UIViewController+StoryboardSegueIdentifierProtocol.swift */,
183-
E2B0AF371D8142BF00A7196C /* UIKit+Migration.swift */,
184191
);
185192
path = UIKit;
186193
sourceTree = "<group>";
187194
};
188195
D543F9CD1C1499CF00D16A0C /* Core */ = {
189196
isa = PBXGroup;
190197
children = (
198+
E2CA68EA1EE75151009C4DB4 /* ColorPaletteItemResource.swift */,
191199
E22D43661C95EEA100692FFF /* ColorResource.swift */,
200+
E2B0AF351D8142A400A7196C /* Core+Migration.swift */,
192201
D5E435AC1C3D00770091090C /* FileResource.swift */,
193202
D57E1EB21C3D762300DDA68F /* FontResource.swift */,
194203
D543F9BA1C1497EB00D16A0C /* Identifier.swift */,
@@ -200,7 +209,6 @@
200209
D51335261C959DF20014C9D4 /* StoryboardViewControllerResource.swift */,
201210
E250BE961CCBF60300CC71DE /* StringResource.swift */,
202211
D53F19231C229D7200AE2FAD /* Validatable.swift */,
203-
E2B0AF351D8142A400A7196C /* Core+Migration.swift */,
204212
);
205213
path = Core;
206214
sourceTree = "<group>";
@@ -441,6 +449,7 @@
441449
D513352B1C95B7620014C9D4 /* UIStoryboard+StoryboardViewControllerResource.swift in Sources */,
442450
D513352C1C95C61E0014C9D4 /* Data+FileResource.swift in Sources */,
443451
806E69AD1C42BDDA00DE3A8B /* ReuseIdentifierProtocol.swift in Sources */,
452+
E2CA68F21EE75A49009C4DB4 /* UIColor+ColorResource.swift in Sources */,
444453
806E69B61C42BDE000DE3A8B /* UINib+NibResource.swift in Sources */,
445454
806E69AA1C42BDDA00DE3A8B /* FontResource.swift in Sources */,
446455
806E69B41C42BDE000DE3A8B /* UICollectionView+ReuseIdentifierProtocol.swift in Sources */,
@@ -459,10 +468,11 @@
459468
806E69B01C42BDDA00DE3A8B /* Validatable.swift in Sources */,
460469
806E69B31C42BDE000DE3A8B /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift in Sources */,
461470
E250BE991CCBF7E900CC71DE /* StringResource.swift in Sources */,
471+
E2CA68F01EE759C3009C4DB4 /* ColorPaletteItemResource+UIKit.swift in Sources */,
462472
D5728B321C4D541500E38168 /* Bundle+FileResource.swift in Sources */,
463-
E250BE951CCBF58200CC71DE /* ColorResource+UIKit.swift in Sources */,
464473
806E69B91C42BDE000DE3A8B /* UITableView+ReuseIdentifierProtocol.swift in Sources */,
465474
806E69AE1C42BDDA00DE3A8B /* StoryboardResource.swift in Sources */,
475+
E2CA68F11EE75A02009C4DB4 /* ColorPaletteItemResource.swift in Sources */,
466476
806E69A91C42BDDA00DE3A8B /* FileResource.swift in Sources */,
467477
);
468478
runOnlyForDeploymentPostprocessing = 0;
@@ -482,13 +492,16 @@
482492
D543F9CA1C14998800D16A0C /* UIViewController+StoryboardSegueIdentifierProtocol.swift in Sources */,
483493
D51335291C95A79B0014C9D4 /* UIStoryboard+StoryboardViewControllerResource.swift in Sources */,
484494
D5E435AD1C3D00770091090C /* FileResource.swift in Sources */,
495+
E2CA68EB1EE75151009C4DB4 /* ColorPaletteItemResource.swift in Sources */,
485496
D543F9BB1C1497EB00D16A0C /* Identifier.swift in Sources */,
497+
E2CA68ED1EE751A9009C4DB4 /* ColorPaletteItemResource+UIKit.swift in Sources */,
486498
D57E1EB71C3E482A00DDA68F /* StoryboardResource.swift in Sources */,
487499
D57E1EBB1C3E4C4300DDA68F /* UIStoryboard+StoryboardResource.swift in Sources */,
488500
D57E1EB31C3D762300DDA68F /* FontResource.swift in Sources */,
489501
D57E1EB91C3E4C1A00DDA68F /* StoryboardResourceWithInitialController+UIKit.swift in Sources */,
490502
D543F9BD1C14980600D16A0C /* ReuseIdentifierProtocol.swift in Sources */,
491503
D543F9C11C14984300D16A0C /* NibResource.swift in Sources */,
504+
E2CA68EF1EE75992009C4DB4 /* UIColor+ColorResource.swift in Sources */,
492505
D553F5851C44157000885232 /* ImageResource.swift in Sources */,
493506
E2B0AF381D8142BF00A7196C /* UIKit+Migration.swift in Sources */,
494507
E20F34A71C92B44100338F81 /* Data+FileResource.swift in Sources */,
@@ -505,7 +518,6 @@
505518
E2B0AF361D8142A400A7196C /* Core+Migration.swift in Sources */,
506519
E250BE971CCBF60300CC71DE /* StringResource.swift in Sources */,
507520
D543F9CF1C149C0A00D16A0C /* TypedStoryboardSegueInfo+UIStoryboardSegue.swift in Sources */,
508-
E250BE941CCBCEB100CC71DE /* ColorResource+UIKit.swift in Sources */,
509521
D543F9C41C1498FB00D16A0C /* UITableView+ReuseIdentifierProtocol.swift in Sources */,
510522
D56DC7731C42B65C00623437 /* Bundle+FileResource.swift in Sources */,
511523
D53F19241C229D7200AE2FAD /* Validatable.swift in Sources */,

0 commit comments

Comments
 (0)