Skip to content

Commit 5421c76

Browse files
committed
Copy color code to WooFoundation
1 parent f05a632 commit 5421c76

File tree

5 files changed

+747
-0
lines changed

5 files changed

+747
-0
lines changed

WooFoundation/WooFoundation.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
26AF1F5328B8362800937BA9 /* UIColor+SemanticColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26AF1F4F28B8362800937BA9 /* UIColor+SemanticColors.swift */; };
11+
26AF1F5428B8362800937BA9 /* ColorStudio.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26AF1F5028B8362800937BA9 /* ColorStudio.swift */; };
12+
26AF1F5528B8362800937BA9 /* UIColor+ColorStudio.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26AF1F5128B8362800937BA9 /* UIColor+ColorStudio.swift */; };
13+
26AF1F5628B8362800937BA9 /* UIColor+SystemColors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 26AF1F5228B8362800937BA9 /* UIColor+SystemColors.swift */; };
1014
397702D42D32EAEEEA3B29FC /* Pods_WooFoundationTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 532D4D63493CE8FA6F13C85B /* Pods_WooFoundationTests.framework */; };
1115
686BE912288EE0D300967C86 /* TypedPredicates.swift in Sources */ = {isa = PBXBuildFile; fileRef = 686BE911288EE0D300967C86 /* TypedPredicates.swift */; };
1216
686BE915288EE2CA00967C86 /* TypedPredicateTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 686BE914288EE2CA00967C86 /* TypedPredicateTests.swift */; };
@@ -35,6 +39,10 @@
3539
/* End PBXContainerItemProxy section */
3640

3741
/* Begin PBXFileReference section */
42+
26AF1F4F28B8362800937BA9 /* UIColor+SemanticColors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+SemanticColors.swift"; sourceTree = "<group>"; };
43+
26AF1F5028B8362800937BA9 /* ColorStudio.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ColorStudio.swift; sourceTree = "<group>"; };
44+
26AF1F5128B8362800937BA9 /* UIColor+ColorStudio.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+ColorStudio.swift"; sourceTree = "<group>"; };
45+
26AF1F5228B8362800937BA9 /* UIColor+SystemColors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIColor+SystemColors.swift"; sourceTree = "<group>"; };
3846
532D4D63493CE8FA6F13C85B /* Pods_WooFoundationTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_WooFoundationTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3947
5BC355411C0A805BF29F38A6 /* Pods-WooFoundationTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-WooFoundationTests.release.xcconfig"; path = "Target Support Files/Pods-WooFoundationTests/Pods-WooFoundationTests.release.xcconfig"; sourceTree = "<group>"; };
4048
686BE911288EE0D300967C86 /* TypedPredicates.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TypedPredicates.swift; sourceTree = "<group>"; };
@@ -83,6 +91,17 @@
8391
/* End PBXFrameworksBuildPhase section */
8492

8593
/* Begin PBXGroup section */
94+
26AF1F4E28B8362800937BA9 /* Colors */ = {
95+
isa = PBXGroup;
96+
children = (
97+
26AF1F4F28B8362800937BA9 /* UIColor+SemanticColors.swift */,
98+
26AF1F5028B8362800937BA9 /* ColorStudio.swift */,
99+
26AF1F5128B8362800937BA9 /* UIColor+ColorStudio.swift */,
100+
26AF1F5228B8362800937BA9 /* UIColor+SystemColors.swift */,
101+
);
102+
path = Colors;
103+
sourceTree = "<group>";
104+
};
86105
686BE910288EE09B00967C86 /* Utilities */ = {
87106
isa = PBXGroup;
88107
children = (
@@ -138,6 +157,7 @@
138157
B9C9C636283E703C001B879F /* WooFoundation */ = {
139158
isa = PBXGroup;
140159
children = (
160+
26AF1F4E28B8362800937BA9 /* Colors */,
141161
689D11D12891B7B100F6A83F /* Mocks */,
142162
689D11CE2891B37400F6A83F /* Protocols */,
143163
686BE910288EE09B00967C86 /* Utilities */,
@@ -387,15 +407,19 @@
387407
buildActionMask = 2147483647;
388408
files = (
389409
B9C9C659283E7195001B879F /* NSDecimalNumber+Helpers.swift in Sources */,
410+
26AF1F5328B8362800937BA9 /* UIColor+SemanticColors.swift in Sources */,
390411
B9C9C663283E7296001B879F /* Logging.swift in Sources */,
391412
6874E81428998AD300074A97 /* LogErrorAndExit.swift in Sources */,
392413
B9C9C65D283E71C8001B879F /* CurrencyFormatter.swift in Sources */,
414+
26AF1F5628B8362800937BA9 /* UIColor+SystemColors.swift in Sources */,
393415
689D11D32891B7D800F6A83F /* MockCrashLogger.swift in Sources */,
394416
B987B06F284540D300C53CF6 /* CurrencyCode.swift in Sources */,
395417
B9C9C660283E71F4001B879F /* Double+Woo.swift in Sources */,
396418
689D11D02891B3A300F6A83F /* CrashLogger.swift in Sources */,
397419
686BE912288EE0D300967C86 /* TypedPredicates.swift in Sources */,
398420
B9C9C65E283E71C8001B879F /* CurrencySettings.swift in Sources */,
421+
26AF1F5528B8362800937BA9 /* UIColor+ColorStudio.swift in Sources */,
422+
26AF1F5428B8362800937BA9 /* ColorStudio.swift in Sources */,
399423
68FBC5B328926B2C00A05461 /* Collection+Extensions.swift in Sources */,
400424
);
401425
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/// Generates the names of the named colors in the ColorPalette.xcasset
2+
enum ColorStudioName: String, CustomStringConvertible {
3+
// MARK: - Base colors
4+
case blue
5+
case celadon
6+
case jetpackGreen
7+
case gray
8+
case green
9+
case orange
10+
case pink
11+
case purple
12+
case red
13+
case yellow
14+
case wooCommercePurple
15+
16+
var description: String {
17+
// can't use .capitalized because it lowercases the C and P in "wooCommercePurple"
18+
return rawValue.prefix(1).uppercased() + rawValue.dropFirst()
19+
}
20+
}
21+
22+
/// Value of a Muriel color's shade
23+
///
24+
/// Note: There are a finite number of acceptable values. Not just any Int works.
25+
/// Also, enum cases cannot begin with a number, thus the `shade` prefix.
26+
enum ColorStudioShade: Int, CustomStringConvertible {
27+
case shade0 = 0
28+
case shade5 = 5
29+
case shade10 = 10
30+
case shade20 = 20
31+
case shade30 = 30
32+
case shade40 = 40
33+
case shade50 = 50
34+
case shade60 = 60
35+
case shade70 = 70
36+
case shade80 = 80
37+
case shade90 = 90
38+
case shade100 = 100
39+
40+
var description: String {
41+
return "\(rawValue)"
42+
}
43+
}
44+
45+
46+
/// Conformance to CaseIterable will be useful for testing.
47+
extension ColorStudioShade: CaseIterable { }
48+
49+
50+
/// A specific color and shade from Color Studio
51+
struct ColorStudio {
52+
let name: ColorStudioName
53+
let shade: ColorStudioShade
54+
55+
init(name: ColorStudioName, shade: ColorStudioShade = .shade50) {
56+
self.name = name
57+
self.shade = shade
58+
}
59+
60+
init(from identifier: ColorStudio, shade: ColorStudioShade) {
61+
self.name = identifier.name
62+
self.shade = shade
63+
}
64+
65+
// MARK: - Muriel's semantic colors
66+
static let pink = ColorStudio(name: .pink)
67+
static let wooCommercePurple = ColorStudio(name: .wooCommercePurple)
68+
static let brand = ColorStudio(name: .wooCommercePurple, shade: .shade60)
69+
static let red = ColorStudio(name: .red)
70+
static let gray = ColorStudio(name: .gray)
71+
static let blue = ColorStudio(name: .blue)
72+
static let jetpackGreen = ColorStudio(name: .jetpackGreen)
73+
static let green = ColorStudio(name: .green)
74+
static let yellow = ColorStudio(name: .yellow)
75+
static let orange = ColorStudio(name: .orange)
76+
static let celadon = ColorStudio(name: .celadon)
77+
78+
/// The full name of the color, with required shade value
79+
func assetName() -> String {
80+
return "\(name)\(shade)"
81+
}
82+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import UIKit
2+
3+
extension UIColor {
4+
/// Get a UIColor from the Color Studio color palette
5+
///
6+
/// - Parameters:
7+
/// - color: an instance of a ColorStudio
8+
/// - Returns: UIColor. Red in cases of error
9+
class func withColorStudio(_ colorStudio: ColorStudio) -> UIColor {
10+
let assetName = colorStudio.assetName()
11+
let color = UIColor(named: assetName)
12+
13+
guard let unwrappedColor = color else {
14+
return .red
15+
}
16+
17+
return unwrappedColor
18+
}
19+
/// Get a UIColor from the Color Studio color palette, adjusted to a given shade
20+
/// - Parameter color: an instance of a ColorStudio
21+
/// - Parameter shade: a ColorStudioShade
22+
class func withColorStudio(_ colorStudio: ColorStudio, shade: ColorStudioShade) -> UIColor {
23+
let newColor = ColorStudio(from: colorStudio, shade: shade)
24+
return withColorStudio(newColor)
25+
}
26+
}
27+
28+
29+
extension UIColor {
30+
// A way to create dynamic colors that's compatible with iOS 11 & 12
31+
convenience init(light: UIColor, dark: UIColor) {
32+
self.init { traitCollection in
33+
if traitCollection.userInterfaceStyle == .dark {
34+
return dark
35+
} else {
36+
return light
37+
}
38+
}
39+
}
40+
41+
convenience init(color: UIColor) {
42+
var r: CGFloat = 0
43+
var g: CGFloat = 0
44+
var b: CGFloat = 0
45+
var a: CGFloat = 0
46+
47+
color.getRed(&r, green: &g, blue: &b, alpha: &a)
48+
self.init(red: r, green: g, blue: b, alpha: a)
49+
}
50+
}
51+
52+
extension UIColor {
53+
func color(for trait: UITraitCollection?) -> UIColor {
54+
if let trait = trait {
55+
return resolvedColor(with: trait)
56+
}
57+
return self
58+
}
59+
}

0 commit comments

Comments
 (0)