Skip to content

Latest commit

 

History

History
235 lines (210 loc) · 4.27 KB

README.md

File metadata and controls

235 lines (210 loc) · 4.27 KB

SwiftUIColorNames

convenient extension for using SwiftUI Color

screen capture (of example app)

screen capture

example app/code

//
//  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()
  }
}

Main functionalities

use color with name

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.

able to convert between "color name" and "its RGB value"

2 dictionaries are there.

ApplePalletColorMapValueToName, ApplePalletColorMapNameToValue

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. screen capture

note: Colors in "ColorPallet Apple" is different from SwiftUI Colors.

W3CColorMapValueToName, W3CColorMapNameToValue

convert between "W3C named color" to "RGB values" like

let rgbValue: UInt32 = W3CColorMapNameToValue["brown"]
let anotherRgbValue = ...
let colorName = W3CColorMapValueToName[anotherRgbValue] ?? "undefined in W3C"

where comes from?

color name and RGB values comes from https://www.w3.org/TR/css-color-4/#named-colors

any restriction?

to prevent duplicate key, following colors are disabled.

  • Grey (and other Greys)
    • use Gray
  • Aqua
    • use Cyan
  • Fuchsia
    • use Magenta

Note: SwiftUI definition is different from this extension

You can use Color.red (comes from SwiftUI) and Color.Red (comes from this extension) even both means same color.

Color Name list

  1. Aliceblue
  2. Antiquewhite
  3. Aquamarine
  4. Azure
  5. Beige
  6. Bisque
  7. Black
  8. Blanchedalmond
  9. Blue
  10. Blueviolet
  11. Brown
  12. Burlywood
  13. Cadetblue
  14. Chartreuse
  15. Chocolate
  16. Coral
  17. Cornflowerblue
  18. Cornsilk
  19. Crimson
  20. Cyan
  21. Darkblue
  22. Darkcyan
  23. Darkgoldenrod
  24. Darkgray
  25. Darkgreen
  26. Darkkhaki
  27. Darkmagenta
  28. Darkolivegreen
  29. Darkorange
  30. Darkorchid
  31. Darkred
  32. Darksalmon
  33. Darkseagreen
  34. Darkslateblue
  35. Darkslategray
  36. Darkturquoise
  37. Darkviolet
  38. Deeppink
  39. Deepskyblue
  40. Dimgray
  41. Dodgerblue
  42. Firebrick
  43. Floralwhite
  44. Forestgreen
  45. Gainsboro
  46. Ghostwhite
  47. Gold
  48. Goldenrod
  49. Gray
  50. Green
  51. Greenyellow
  52. Honeydew
  53. Hotpink
  54. Indianred
  55. Indigo
  56. Ivory
  57. Khaki
  58. Lavender
  59. Lavenderblush
  60. Lawngreen
  61. Lemonchiffon
  62. Lightblue
  63. Lightcoral
  64. Lightcyan
  65. Lightgoldenrodyellow
  66. Lightgray
  67. Lightgreen
  68. Lightpink
  69. Lightsalmon
  70. Lightseagreen
  71. Lightskyblue
  72. Lightslategray
  73. Lightsteelblue
  74. Lightyellow
  75. Lime
  76. Limegreen
  77. Linen
  78. Magenta
  79. Maroon
  80. Mediumaquamarine
  81. Mediumblue
  82. Mediumorchid
  83. Mediumpurple
  84. Mediumseagreen
  85. Mediumslateblue
  86. Mediumspringgreen
  87. Mediumturquoise
  88. Mediumvioletred
  89. Midnightblue
  90. Mintcream
  91. Mistyrose
  92. Moccasin
  93. Navajowhite
  94. Navy
  95. Oldlace
  96. Olive
  97. Olivedrab
  98. Orange
  99. Orangered
  100. Orchid
  101. Palegoldenrod
  102. Palegreen
  103. Paleturquoise
  104. Palevioletred
  105. Papayawhip
  106. Peachpuff
  107. Peru
  108. Pink
  109. Plum
  110. Powderblue
  111. Purple
  112. Rebeccapurple
  113. Red
  114. Rosybrown
  115. Royalblue
  116. Saddlebrown
  117. Salmon
  118. Sandybrown
  119. Seagreen
  120. Seashell
  121. Sienna
  122. Silver
  123. Skyblue
  124. Slateblue
  125. Slategray
  126. Snow
  127. Springgreen
  128. Steelblue
  129. Tan
  130. Teal
  131. Thistle
  132. Tomato
  133. Turquoise
  134. Violet
  135. Wheat
  136. White
  137. Whitesmoke
  138. Yellow
  139. Yellowgreen