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.
- Grey (and other Greys)
- use Gray
- Aqua
- use Cyan
- Fuchsia
- use Magenta
You can use Color.red (comes from SwiftUI) and Color.Red (comes from this extension) even both means same color.
- Aliceblue
- Antiquewhite
- Aquamarine
- Azure
- Beige
- Bisque
- Black
- Blanchedalmond
- Blue
- Blueviolet
- Brown
- Burlywood
- Cadetblue
- Chartreuse
- Chocolate
- Coral
- Cornflowerblue
- Cornsilk
- Crimson
- Cyan
- Darkblue
- Darkcyan
- Darkgoldenrod
- Darkgray
- Darkgreen
- Darkkhaki
- Darkmagenta
- Darkolivegreen
- Darkorange
- Darkorchid
- Darkred
- Darksalmon
- Darkseagreen
- Darkslateblue
- Darkslategray
- Darkturquoise
- Darkviolet
- Deeppink
- Deepskyblue
- Dimgray
- Dodgerblue
- Firebrick
- Floralwhite
- Forestgreen
- Gainsboro
- Ghostwhite
- Gold
- Goldenrod
- Gray
- Green
- Greenyellow
- Honeydew
- Hotpink
- Indianred
- Indigo
- Ivory
- Khaki
- Lavender
- Lavenderblush
- Lawngreen
- Lemonchiffon
- Lightblue
- Lightcoral
- Lightcyan
- Lightgoldenrodyellow
- Lightgray
- Lightgreen
- Lightpink
- Lightsalmon
- Lightseagreen
- Lightskyblue
- Lightslategray
- Lightsteelblue
- Lightyellow
- Lime
- Limegreen
- Linen
- Magenta
- Maroon
- Mediumaquamarine
- Mediumblue
- Mediumorchid
- Mediumpurple
- Mediumseagreen
- Mediumslateblue
- Mediumspringgreen
- Mediumturquoise
- Mediumvioletred
- Midnightblue
- Mintcream
- Mistyrose
- Moccasin
- Navajowhite
- Navy
- Oldlace
- Olive
- Olivedrab
- Orange
- Orangered
- Orchid
- Palegoldenrod
- Palegreen
- Paleturquoise
- Palevioletred
- Papayawhip
- Peachpuff
- Peru
- Pink
- Plum
- Powderblue
- Purple
- Rebeccapurple
- Red
- Rosybrown
- Royalblue
- Saddlebrown
- Salmon
- Sandybrown
- Seagreen
- Seashell
- Sienna
- Silver
- Skyblue
- Slateblue
- Slategray
- Snow
- Springgreen
- Steelblue
- Tan
- Teal
- Thistle
- Tomato
- Turquoise
- Violet
- Wheat
- White
- Whitesmoke
- Yellow
- Yellowgreen