convenient extension for using SwiftUI Color
//
// ContentView.swift
//
// Created by : Tomoaki Yagishita on 2020/10/24
// © 2020 SmallDeskSoftware
//
import SwiftUI
import SwiftUIColorNames
struct ContentView: View {
var body: some View {
List( Color.AdditionalColors.allCases, id:\.self) { color in
ZStack {
Color(hex: color.rawValue)
Text("\(Color.mapValueToName[color.rawValue] ?? "invalid Color")")
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
You can specify the color with its name (in W3C/ApplePalletColor) like
let favoriteColor = Color.gold // :)
let nextFavColor = Color.ApplePalletBrown
Note: to avoid name conflict with SwiftUI, name of all W3C colors starts with lowercase character.
for color in apple pallet, it starts with "ApplePallet" prefix.
2 dictionaries are there.
convert between "ColorPallet/Apple" to "RGB values" like
let rgbValue: UInt32 = ApplePalletColorMapNameToValue["Brown"]
let anotherRgbValue = ...
let colorName = ApplePalletColorMapNameToValue[anotherRgbValue] ?? "undefined in pallet"
Note: what is "ColorPallet/Apple"? -> please refer to followings.
note: Colors in "ColorPallet Apple" is different from SwiftUI Colors.
convert between "W3C named color" to "RGB values" like
let rgbValue: UInt32 = W3CColorMapNameToValue["brown"]
let anotherRgbValue = ...
let colorName = W3CColorMapValueToName[anotherRgbValue] ?? "undefined in W3C"
color name and RGB values comes from https://www.w3.org/TR/css-color-4/#named-colors
to prevent duplicate key, following colors are disabled.
You can use Color.red (comes from SwiftUI) and Color.Red (comes from this extension) even both means same color.
link |
Stars: 5 |
Last commit: 1 year ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics