-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mob 35767 fix color generation #59
Changes from 10 commits
193e428
91cd304
f0ba50a
77b0f36
d75ee6b
5f0f935
0ea3556
1f613bf
b90082d
b62700c
6cb306c
d197305
1db8052
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ final class DefaultTokensResolver: TokensResolver { | |
return hexFloat / 255.0 | ||
} | ||
|
||
private func makeColor(hex: String, alpha: CGFloat) throws -> Color { | ||
private func makeColor(hex: String, alpha: CGFloat, tokenName: String) throws -> Color { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да, спасибо! Я хотела прокинуть в ошибку .invalidHEXComponent tokenName, но оказывается по code style нельзя только в 1 case enum-a прокинуть второй параметр. И я удалила этот код. Но часть забыла. |
||
let hex = hex | ||
.replacingOccurrences(of: "#", with: "") | ||
.trimmingCharacters(in: .whitespacesAndNewlines) | ||
|
@@ -60,18 +60,18 @@ final class DefaultTokensResolver: TokensResolver { | |
} | ||
} | ||
|
||
private func resolveColorValue(_ value: String, tokenValues: TokenValues) throws -> Color { | ||
private func resolveColorValue(_ value: String, tokenValues: TokenValues, theme: Theme) throws -> Color { | ||
if value.hasPrefix("rgba") { | ||
return try resolveRGBAColorValue(value, tokenValues: tokenValues) | ||
return try resolveRGBAColorValue(value, tokenValues: tokenValues, theme: theme) | ||
} | ||
|
||
return try makeColor(hex: value, alpha: 1.0) | ||
return try makeColor(hex: value, alpha: 1.0, tokenName: value) | ||
} | ||
|
||
// MARK: - TokensResolver | ||
|
||
func resolveValue(_ value: String, tokenValues: TokenValues) throws -> String { | ||
let allTokens = tokenValues.all | ||
func resolveValue(_ value: String, tokenValues: TokenValues, theme: Theme) throws -> String { | ||
let themeTokens = tokenValues.getThemeTokenValues(theme: theme) | ||
|
||
let resolvedValue = try value.replacingOccurrences(matchingPattern: #"\{.*?\}"#) { referenceName in | ||
let referenceName = String( | ||
|
@@ -80,22 +80,22 @@ final class DefaultTokensResolver: TokensResolver { | |
.dropLast() | ||
) | ||
|
||
guard let token = allTokens.first(where: { $0.name == referenceName }) else { | ||
guard let token = themeTokens.first(where: { $0.name == referenceName }) else { | ||
throw TokensGeneratorError(code: .referenceNotFound(name: referenceName)) | ||
} | ||
|
||
guard let value = token.type.stringValue else { | ||
throw TokensGeneratorError(code: .unexpectedTokenValueType(name: referenceName)) | ||
} | ||
|
||
return try resolveValue(value, tokenValues: tokenValues) | ||
return try resolveValue(value, tokenValues: tokenValues, theme: theme) | ||
} | ||
|
||
return evaluteValue(resolvedValue) | ||
} | ||
|
||
func resolveRGBAColorValue(_ value: String, tokenValues: TokenValues) throws -> Color { | ||
let components = try resolveValue(value, tokenValues: tokenValues) | ||
func resolveRGBAColorValue(_ value: String, tokenValues: TokenValues, theme: Theme) throws -> Color { | ||
let components = try resolveValue(value, tokenValues: tokenValues, theme: theme) | ||
.slice(from: "(", to: ")", includingBounds: false)? | ||
.components(separatedBy: ", ") | ||
|
||
|
@@ -110,21 +110,20 @@ final class DefaultTokensResolver: TokensResolver { | |
throw TokensGeneratorError(code: .invalidAlphaComponent(alpha: alphaPercent)) | ||
} | ||
|
||
return try makeColor(hex: hex, alpha: alpha / 100.0) | ||
return try makeColor(hex: hex, alpha: alpha / 100.0, tokenName: value) | ||
} | ||
|
||
func resolveHexColorValue(_ value: String, tokenValues: TokenValues) throws -> String { | ||
let resolvedValue = try resolveValue(value, tokenValues: tokenValues) | ||
func resolveHexColorValue(_ value: String, tokenValues: TokenValues, theme: Theme) throws -> String { | ||
let resolvedValue = try resolveValue(value, tokenValues: tokenValues, theme: theme) | ||
|
||
if resolvedValue.hasPrefix("#") { | ||
return resolvedValue | ||
} | ||
|
||
return try resolveColorValue(resolvedValue, tokenValues: tokenValues).hexString | ||
return try resolveColorValue(resolvedValue, tokenValues: tokenValues, theme: theme).hexString | ||
} | ||
|
||
func resolveLinearGradientValue(_ value: String, tokenValues: TokenValues) throws -> LinearGradient { | ||
let value = try resolveValue(value, tokenValues: tokenValues) | ||
func resolveLinearGradientValue(_ value: String, tokenValues: TokenValues, theme: Theme) throws -> LinearGradient { | ||
let value = try resolveValue(value, tokenValues: tokenValues, theme: theme) | ||
|
||
guard let startFunctionIndex = value.firstIndex(of: "("), let endFunctionIndex = value.lastIndex(of: ")") else { | ||
throw TokensGeneratorError(code: .failedToExtractLinearGradientParams(linearGradient: value)) | ||
|
@@ -148,7 +147,7 @@ final class DefaultTokensResolver: TokensResolver { | |
|
||
let percentage = String(rawColorStop[separatorRange.upperBound...]) | ||
let rawColor = String(rawColorStop[...separatorRange.lowerBound]) | ||
let color = try resolveColorValue(rawColor, tokenValues: tokenValues) | ||
let color = try resolveColorValue(rawColor, tokenValues: tokenValues, theme: theme) | ||
|
||
return LinearGradient.LinearColorStop(color: color, percentage: percentage) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
|
||
enum Theme: Codable { | ||
case day | ||
case night | ||
case undefined | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,18 @@ struct TokenValues: Codable, Hashable { | |
|
||
// MARK: - Instance Properties | ||
|
||
var all: [TokenValue] { | ||
[core, semantic, colors, typography, day, night].flatMap { $0 } | ||
// Возвращает набор токенов для определенной темы. | ||
// Для undefined возвращается полный набор токенов. Нужен для Spacer, Font и других независимых от темы параметров. | ||
func getThemeTokenValues(theme: Theme) -> [TokenValue] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Нужно три слэша, тогда это будет документация метода: /// Возвращает набор токенов для определенной темы.
/// Для undefined возвращается полный набор токенов. Нужен для Spacer, Font и других независимых от темы параметров.
func getThemeTokenValues(theme: Theme) -> [TokenValue] { |
||
switch theme { | ||
case .day: | ||
return [day, core, semantic, colors, typography].flatMap { $0 } | ||
|
||
case .night: | ||
return [night, core, semantic, colors, typography].flatMap { $0 } | ||
|
||
case .undefined: | ||
return [core, semantic, colors, typography, day, night].flatMap { $0 } | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А зачем тут эта проверка?
path[0] != "gradient"
проверяет и так, что значение не начинается сgradient
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Там появились новые токены у которых gradient только в value, ранее проверялся только name