Skip to content

Instantly share code, notes, and snippets.

@nbasham
Last active December 18, 2023 21:04

Revisions

  1. nbasham revised this gist Mar 22, 2021. No changes.
  2. nbasham revised this gist Nov 17, 2018. 1 changed file with 17 additions and 17 deletions.
    34 changes: 17 additions & 17 deletions UIColor.swift
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // previously Color+HexAndCSSColorNames.swift
    //
    // Created by Norman Basham on 12/8/15.
    // Copyright © 2018 Black Labs. All rights reserved.
    // Copyright ©2018 Black Labs. All rights reserved.
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    @@ -26,12 +26,12 @@
    import UIKit

    public extension UIColor {

    /**
    Creates an immuatble UIColor instance specified by a hex string, CSS color name, or nil.

    - parameter hexString: A case insensitive String? representing a hex or CSS value e.g.

    - **"abc"**
    - **"abc7"**
    - **"#abc7"**
    @@ -50,16 +50,16 @@ public extension UIColor {
    Scanner(string: normalizedHexString).scanHexInt32(&c)
    self.init(red:UIColorMasks.redValue(c), green:UIColorMasks.greenValue(c), blue:UIColorMasks.blueValue(c), alpha:UIColorMasks.alphaValue(c))
    }

    /**
    Returns a hex equivalent of this UIColor.

    - Parameter includeAlpha: Optional parameter to include the alpha hex.

    color.hexDescription() -> "ff0000"

    color.hexDescription(true) -> "ff0000aa"

    - Returns: A new string with `String` with the color's hexidecimal value.
    */
    public func hexDescription(_ includeAlpha: Bool = false) -> String {
    @@ -74,30 +74,30 @@ public extension UIColor {
    }
    return color
    }

    fileprivate enum UIColorMasks: CUnsignedInt {
    case redMask = 0xff000000
    case greenMask = 0x00ff0000
    case blueMask = 0x0000ff00
    case alphaMask = 0x000000ff

    static func redValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & redMask.rawValue) >> 24) / 255.0
    }

    static func greenValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & greenMask.rawValue) >> 16) / 255.0
    }

    static func blueValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & blueMask.rawValue) >> 8) / 255.0
    }

    static func alphaValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat(value & alphaMask.rawValue) / 255.0
    }
    }

    fileprivate static func normalize(_ hex: String?) -> String {
    guard var hexString = hex else {
    return "00000000"
    @@ -117,7 +117,7 @@ public extension UIColor {
    }
    return hexString
    }

    /**
    All modern browsers support the following 140 color names (see http://www.w3schools.com/cssref/css_colornames.asp)
    */
    @@ -128,7 +128,7 @@ public extension UIColor {
    }
    return cssName
    }

    fileprivate static let cssToHexDictionairy: [String: String] = [
    "CLEAR": "00000000",
    "TRANSPARENT": "00000000",
  3. nbasham revised this gist Nov 17, 2018. 1 changed file with 173 additions and 173 deletions.
    346 changes: 173 additions & 173 deletions UIColor.swift
    Original file line number Diff line number Diff line change
    @@ -26,12 +26,12 @@
    import UIKit

    public extension UIColor {

    /**
    Creates an immuatble UIColor instance specified by a hex string, CSS color name, or nil.

    - parameter hex: A case insensitive String? representing a hex or CSS value e.g.

    - parameter hexString: A case insensitive String? representing a hex or CSS value e.g.
    - **"abc"**
    - **"abc7"**
    - **"#abc7"**
    @@ -50,16 +50,16 @@ public extension UIColor {
    Scanner(string: normalizedHexString).scanHexInt32(&c)
    self.init(red:UIColorMasks.redValue(c), green:UIColorMasks.greenValue(c), blue:UIColorMasks.blueValue(c), alpha:UIColorMasks.alphaValue(c))
    }

    /**
    Returns a hex equivalent of this UIColor.

    - Parameter includeAlpha: Optional parameter to include the alpha hex.

    color.hexDescription() -> "ff0000"

    color.hexDescription(true) -> "ff0000aa"

    - Returns: A new string with `String` with the color's hexidecimal value.
    */
    public func hexDescription(_ includeAlpha: Bool = false) -> String {
    @@ -74,50 +74,50 @@ public extension UIColor {
    }
    return color
    }

    fileprivate enum UIColorMasks: CUnsignedInt {
    case redMask = 0xff000000
    case greenMask = 0x00ff0000
    case blueMask = 0x0000ff00
    case alphaMask = 0x000000ff

    static func redValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & redMask.rawValue) >> 24) / 255.0
    }

    static func greenValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & greenMask.rawValue) >> 16) / 255.0
    }

    static func blueValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & blueMask.rawValue) >> 8) / 255.0
    }

    static func alphaValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat(value & alphaMask.rawValue) / 255.0
    }
    }

    fileprivate static func normalize(_ hex: String?) -> String {
    guard var hexString = hex else {
    return "00000000"
    }
    if let cssColor = cssToHexDictionairy[hexString.uppercased()] {
    return cssColor.count == 8 ? cssColor : cssColor + "ff"
    }
    if (hexString.hasPrefix("#")) {
    if hexString.hasPrefix("#") {
    hexString = String(hexString.dropFirst())
    }
    if hexString.count == 3 || hexString.count == 4 {
    hexString = hexString.map{ "\($0)\($0)" }.joined()
    hexString = hexString.map { "\($0)\($0)" } .joined()
    }
    let hasAlpha = hexString.count > 7
    if (!hasAlpha) {
    hexString = hexString + "ff"
    if !hasAlpha {
    hexString += "ff"
    }
    return hexString;
    return hexString
    }

    /**
    All modern browsers support the following 140 color names (see http://www.w3schools.com/cssref/css_colornames.asp)
    */
    @@ -128,157 +128,157 @@ public extension UIColor {
    }
    return cssName
    }

    fileprivate static let cssToHexDictionairy : Dictionary<String, String> = [
    "CLEAR" : "00000000",
    "TRANSPARENT" : "00000000",
    "" : "00000000",
    "ALICEBLUE" : "F0F8FF",
    "ANTIQUEWHITE" : "FAEBD7",
    "AQUA" : "00FFFF",
    "AQUAMARINE" : "7FFFD4",
    "AZURE" : "F0FFFF",
    "BEIGE" : "F5F5DC",
    "BISQUE" : "FFE4C4",
    "BLACK" : "000000",
    "BLANCHEDALMOND" : "FFEBCD",
    "BLUE" : "0000FF",
    "BLUEVIOLET" : "8A2BE2",
    "BROWN" : "A52A2A",
    "BURLYWOOD" : "DEB887",
    "CADETBLUE" : "5F9EA0",
    "CHARTREUSE" : "7FFF00",
    "CHOCOLATE" : "D2691E",
    "CORAL" : "FF7F50",
    "CORNFLOWERBLUE" : "6495ED",
    "CORNSILK" : "FFF8DC",
    "CRIMSON" : "DC143C",
    "CYAN" : "00FFFF",
    "DARKBLUE" : "00008B",
    "DARKCYAN" : "008B8B",
    "DARKGOLDENROD" : "B8860B",
    "DARKGRAY" : "A9A9A9",
    "DARKGREY" : "A9A9A9",
    "DARKGREEN" : "006400",
    "DARKKHAKI" : "BDB76B",
    "DARKMAGENTA" : "8B008B",
    "DARKOLIVEGREEN" : "556B2F",
    "DARKORANGE" : "FF8C00",
    "DARKORCHID" : "9932CC",
    "DARKRED" : "8B0000",
    "DARKSALMON" : "E9967A",
    "DARKSEAGREEN" : "8FBC8F",
    "DARKSLATEBLUE" : "483D8B",
    "DARKSLATEGRAY" : "2F4F4F",
    "DARKSLATEGREY" : "2F4F4F",
    "DARKTURQUOISE" : "00CED1",
    "DARKVIOLET" : "9400D3",
    "DEEPPINK" : "FF1493",
    "DEEPSKYBLUE" : "00BFFF",
    "DIMGRAY" : "696969",
    "DIMGREY" : "696969",
    "DODGERBLUE" : "1E90FF",
    "FIREBRICK" : "B22222",
    "FLORALWHITE" : "FFFAF0",
    "FORESTGREEN" : "228B22",
    "FUCHSIA" : "FF00FF",
    "GAINSBORO" : "DCDCDC",
    "GHOSTWHITE" : "F8F8FF",
    "GOLD" : "FFD700",
    "GOLDENROD" : "DAA520",
    "GRAY" : "808080",
    "GREY" : "808080",
    "GREEN" : "008000",
    "GREENYELLOW" : "ADFF2F",
    "HONEYDEW" : "F0FFF0",
    "HOTPINK" : "FF69B4",
    "INDIANRED" : "CD5C5C",
    "INDIGO" : "4B0082",
    "IVORY" : "FFFFF0",
    "KHAKI" : "F0E68C",
    "LAVENDER" : "E6E6FA",
    "LAVENDERBLUSH" : "FFF0F5",
    "LAWNGREEN" : "7CFC00",
    "LEMONCHIFFON" : "FFFACD",
    "LIGHTBLUE" : "ADD8E6",
    "LIGHTCORAL" : "F08080",
    "LIGHTCYAN" : "E0FFFF",
    "LIGHTGOLDENRODYELLOW" : "FAFAD2",
    "LIGHTGRAY" : "D3D3D3",
    "LIGHTGREY" : "D3D3D3",
    "LIGHTGREEN" : "90EE90",
    "LIGHTPINK" : "FFB6C1",
    "LIGHTSALMON" : "FFA07A",
    "LIGHTSEAGREEN" : "20B2AA",
    "LIGHTSKYBLUE" : "87CEFA",
    "LIGHTSLATEGRAY" : "778899",
    "LIGHTSLATEGREY" : "778899",
    "LIGHTSTEELBLUE" : "B0C4DE",
    "LIGHTYELLOW" : "FFFFE0",
    "LIME" : "00FF00",
    "LIMEGREEN" : "32CD32",
    "LINEN" : "FAF0E6",
    "MAGENTA" : "FF00FF",
    "MAROON" : "800000",
    "MEDIUMAQUAMARINE" : "66CDAA",
    "MEDIUMBLUE" : "0000CD",
    "MEDIUMORCHID" : "BA55D3",
    "MEDIUMPURPLE" : "9370DB",
    "MEDIUMSEAGREEN" : "3CB371",
    "MEDIUMSLATEBLUE" : "7B68EE",
    "MEDIUMSPRINGGREEN" : "00FA9A",
    "MEDIUMTURQUOISE" : "48D1CC",
    "MEDIUMVIOLETRED" : "C71585",
    "MIDNIGHTBLUE" : "191970",
    "MINTCREAM" : "F5FFFA",
    "MISTYROSE" : "FFE4E1",
    "MOCCASIN" : "FFE4B5",
    "NAVAJOWHITE" : "FFDEAD",
    "NAVY" : "000080",
    "OLDLACE" : "FDF5E6",
    "OLIVE" : "808000",
    "OLIVEDRAB" : "6B8E23",
    "ORANGE" : "FFA500",
    "ORANGERED" : "FF4500",
    "ORCHID" : "DA70D6",
    "PALEGOLDENROD" : "EEE8AA",
    "PALEGREEN" : "98FB98",
    "PALETURQUOISE" : "AFEEEE",
    "PALEVIOLETRED" : "DB7093",
    "PAPAYAWHIP" : "FFEFD5",
    "PEACHPUFF" : "FFDAB9",
    "PERU" : "CD853F",
    "PINK" : "FFC0CB",
    "PLUM" : "DDA0DD",
    "POWDERBLUE" : "B0E0E6",
    "PURPLE" : "800080",
    "RED" : "FF0000",
    "ROSYBROWN" : "BC8F8F",
    "ROYALBLUE" : "4169E1",
    "SADDLEBROWN" : "8B4513",
    "SALMON" : "FA8072",
    "SANDYBROWN" : "F4A460",
    "SEAGREEN" : "2E8B57",
    "SEASHELL" : "FFF5EE",
    "SIENNA" : "A0522D",
    "SILVER" : "C0C0C0",
    "SKYBLUE" : "87CEEB",
    "SLATEBLUE" : "6A5ACD",
    "SLATEGRAY" : "708090",
    "SLATEGREY" : "708090",
    "SNOW" : "FFFAFA",
    "SPRINGGREEN" : "00FF7F",
    "STEELBLUE" : "4682B4",
    "TAN" : "D2B48C",
    "TEAL" : "008080",
    "THISTLE" : "D8BFD8",
    "TOMATO" : "FF6347",
    "TURQUOISE" : "40E0D0",
    "VIOLET" : "EE82EE",
    "WHEAT" : "F5DEB3",
    "WHITE" : "FFFFFF",
    "WHITESMOKE" : "F5F5F5",
    "YELLOW" : "FFFF00",
    "YELLOWGREEN" : "9ACD32"
    fileprivate static let cssToHexDictionairy: [String: String] = [
    "CLEAR": "00000000",
    "TRANSPARENT": "00000000",
    "": "00000000",
    "ALICEBLUE": "F0F8FF",
    "ANTIQUEWHITE": "FAEBD7",
    "AQUA": "00FFFF",
    "AQUAMARINE": "7FFFD4",
    "AZURE": "F0FFFF",
    "BEIGE": "F5F5DC",
    "BISQUE": "FFE4C4",
    "BLACK": "000000",
    "BLANCHEDALMOND": "FFEBCD",
    "BLUE": "0000FF",
    "BLUEVIOLET": "8A2BE2",
    "BROWN": "A52A2A",
    "BURLYWOOD": "DEB887",
    "CADETBLUE": "5F9EA0",
    "CHARTREUSE": "7FFF00",
    "CHOCOLATE": "D2691E",
    "CORAL": "FF7F50",
    "CORNFLOWERBLUE": "6495ED",
    "CORNSILK": "FFF8DC",
    "CRIMSON": "DC143C",
    "CYAN": "00FFFF",
    "DARKBLUE": "00008B",
    "DARKCYAN": "008B8B",
    "DARKGOLDENROD": "B8860B",
    "DARKGRAY": "A9A9A9",
    "DARKGREY": "A9A9A9",
    "DARKGREEN": "006400",
    "DARKKHAKI": "BDB76B",
    "DARKMAGENTA": "8B008B",
    "DARKOLIVEGREEN": "556B2F",
    "DARKORANGE": "FF8C00",
    "DARKORCHID": "9932CC",
    "DARKRED": "8B0000",
    "DARKSALMON": "E9967A",
    "DARKSEAGREEN": "8FBC8F",
    "DARKSLATEBLUE": "483D8B",
    "DARKSLATEGRAY": "2F4F4F",
    "DARKSLATEGREY": "2F4F4F",
    "DARKTURQUOISE": "00CED1",
    "DARKVIOLET": "9400D3",
    "DEEPPINK": "FF1493",
    "DEEPSKYBLUE": "00BFFF",
    "DIMGRAY": "696969",
    "DIMGREY": "696969",
    "DODGERBLUE": "1E90FF",
    "FIREBRICK": "B22222",
    "FLORALWHITE": "FFFAF0",
    "FORESTGREEN": "228B22",
    "FUCHSIA": "FF00FF",
    "GAINSBORO": "DCDCDC",
    "GHOSTWHITE": "F8F8FF",
    "GOLD": "FFD700",
    "GOLDENROD": "DAA520",
    "GRAY": "808080",
    "GREY": "808080",
    "GREEN": "008000",
    "GREENYELLOW": "ADFF2F",
    "HONEYDEW": "F0FFF0",
    "HOTPINK": "FF69B4",
    "INDIANRED": "CD5C5C",
    "INDIGO": "4B0082",
    "IVORY": "FFFFF0",
    "KHAKI": "F0E68C",
    "LAVENDER": "E6E6FA",
    "LAVENDERBLUSH": "FFF0F5",
    "LAWNGREEN": "7CFC00",
    "LEMONCHIFFON": "FFFACD",
    "LIGHTBLUE": "ADD8E6",
    "LIGHTCORAL": "F08080",
    "LIGHTCYAN": "E0FFFF",
    "LIGHTGOLDENRODYELLOW": "FAFAD2",
    "LIGHTGRAY": "D3D3D3",
    "LIGHTGREY": "D3D3D3",
    "LIGHTGREEN": "90EE90",
    "LIGHTPINK": "FFB6C1",
    "LIGHTSALMON": "FFA07A",
    "LIGHTSEAGREEN": "20B2AA",
    "LIGHTSKYBLUE": "87CEFA",
    "LIGHTSLATEGRAY": "778899",
    "LIGHTSLATEGREY": "778899",
    "LIGHTSTEELBLUE": "B0C4DE",
    "LIGHTYELLOW": "FFFFE0",
    "LIME": "00FF00",
    "LIMEGREEN": "32CD32",
    "LINEN": "FAF0E6",
    "MAGENTA": "FF00FF",
    "MAROON": "800000",
    "MEDIUMAQUAMARINE": "66CDAA",
    "MEDIUMBLUE": "0000CD",
    "MEDIUMORCHID": "BA55D3",
    "MEDIUMPURPLE": "9370DB",
    "MEDIUMSEAGREEN": "3CB371",
    "MEDIUMSLATEBLUE": "7B68EE",
    "MEDIUMSPRINGGREEN": "00FA9A",
    "MEDIUMTURQUOISE": "48D1CC",
    "MEDIUMVIOLETRED": "C71585",
    "MIDNIGHTBLUE": "191970",
    "MINTCREAM": "F5FFFA",
    "MISTYROSE": "FFE4E1",
    "MOCCASIN": "FFE4B5",
    "NAVAJOWHITE": "FFDEAD",
    "NAVY": "000080",
    "OLDLACE": "FDF5E6",
    "OLIVE": "808000",
    "OLIVEDRAB": "6B8E23",
    "ORANGE": "FFA500",
    "ORANGERED": "FF4500",
    "ORCHID": "DA70D6",
    "PALEGOLDENROD": "EEE8AA",
    "PALEGREEN": "98FB98",
    "PALETURQUOISE": "AFEEEE",
    "PALEVIOLETRED": "DB7093",
    "PAPAYAWHIP": "FFEFD5",
    "PEACHPUFF": "FFDAB9",
    "PERU": "CD853F",
    "PINK": "FFC0CB",
    "PLUM": "DDA0DD",
    "POWDERBLUE": "B0E0E6",
    "PURPLE": "800080",
    "RED": "FF0000",
    "ROSYBROWN": "BC8F8F",
    "ROYALBLUE": "4169E1",
    "SADDLEBROWN": "8B4513",
    "SALMON": "FA8072",
    "SANDYBROWN": "F4A460",
    "SEAGREEN": "2E8B57",
    "SEASHELL": "FFF5EE",
    "SIENNA": "A0522D",
    "SILVER": "C0C0C0",
    "SKYBLUE": "87CEEB",
    "SLATEBLUE": "6A5ACD",
    "SLATEGRAY": "708090",
    "SLATEGREY": "708090",
    "SNOW": "FFFAFA",
    "SPRINGGREEN": "00FF7F",
    "STEELBLUE": "4682B4",
    "TAN": "D2B48C",
    "TEAL": "008080",
    "THISTLE": "D8BFD8",
    "TOMATO": "FF6347",
    "TURQUOISE": "40E0D0",
    "VIOLET": "EE82EE",
    "WHEAT": "F5DEB3",
    "WHITE": "FFFFFF",
    "WHITESMOKE": "F5F5F5",
    "YELLOW": "FFFF00",
    "YELLOWGREEN": "9ACD32"
    ]
    }
  4. nbasham revised this gist Nov 17, 2018. 1 changed file with 163 additions and 163 deletions.
    326 changes: 163 additions & 163 deletions UIColor.swift
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    // previously Color+HexAndCSSColorNames.swift
    //
    // Created by Norman Basham on 12/8/15.
    // Copyright © 2017 Black Labs. All rights reserved.
    // Copyright © 2018 Black Labs. All rights reserved.
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    @@ -30,7 +30,7 @@ public extension UIColor {
    /**
    Creates an immuatble UIColor instance specified by a hex string, CSS color name, or nil.

    - parameter hexString: A case insensitive String? representing a hex or CSS value e.g.
    - parameter hex: A case insensitive String? representing a hex or CSS value e.g.

    - **"abc"**
    - **"abc7"**
    @@ -103,19 +103,19 @@ public extension UIColor {
    return "00000000"
    }
    if let cssColor = cssToHexDictionairy[hexString.uppercased()] {
    return cssColor.characters.count == 8 ? cssColor : cssColor + "ff"
    return cssColor.count == 8 ? cssColor : cssColor + "ff"
    }
    if hexString.hasPrefix("#") {
    hexString = String(hexString.characters.dropFirst())
    if (hexString.hasPrefix("#")) {
    hexString = String(hexString.dropFirst())
    }
    if hexString.characters.count == 3 || hexString.characters.count == 4 {
    hexString = hexString.characters.map { "\($0)\($0)" } .joined()
    if hexString.count == 3 || hexString.count == 4 {
    hexString = hexString.map{ "\($0)\($0)" }.joined()
    }
    let hasAlpha = hexString.characters.count > 7
    if !hasAlpha {
    hexString += "ff"
    let hasAlpha = hexString.count > 7
    if (!hasAlpha) {
    hexString = hexString + "ff"
    }
    return hexString
    return hexString;
    }

    /**
    @@ -129,156 +129,156 @@ public extension UIColor {
    return cssName
    }

    fileprivate static let cssToHexDictionairy: [String: String] = [
    "CLEAR": "00000000",
    "TRANSPARENT": "00000000",
    "": "00000000",
    "ALICEBLUE": "F0F8FF",
    "ANTIQUEWHITE": "FAEBD7",
    "AQUA": "00FFFF",
    "AQUAMARINE": "7FFFD4",
    "AZURE": "F0FFFF",
    "BEIGE": "F5F5DC",
    "BISQUE": "FFE4C4",
    "BLACK": "000000",
    "BLANCHEDALMOND": "FFEBCD",
    "BLUE": "0000FF",
    "BLUEVIOLET": "8A2BE2",
    "BROWN": "A52A2A",
    "BURLYWOOD": "DEB887",
    "CADETBLUE": "5F9EA0",
    "CHARTREUSE": "7FFF00",
    "CHOCOLATE": "D2691E",
    "CORAL": "FF7F50",
    "CORNFLOWERBLUE": "6495ED",
    "CORNSILK": "FFF8DC",
    "CRIMSON": "DC143C",
    "CYAN": "00FFFF",
    "DARKBLUE": "00008B",
    "DARKCYAN": "008B8B",
    "DARKGOLDENROD": "B8860B",
    "DARKGRAY": "A9A9A9",
    "DARKGREY": "A9A9A9",
    "DARKGREEN": "006400",
    "DARKKHAKI": "BDB76B",
    "DARKMAGENTA": "8B008B",
    "DARKOLIVEGREEN": "556B2F",
    "DARKORANGE": "FF8C00",
    "DARKORCHID": "9932CC",
    "DARKRED": "8B0000",
    "DARKSALMON": "E9967A",
    "DARKSEAGREEN": "8FBC8F",
    "DARKSLATEBLUE": "483D8B",
    "DARKSLATEGRAY": "2F4F4F",
    "DARKSLATEGREY": "2F4F4F",
    "DARKTURQUOISE": "00CED1",
    "DARKVIOLET": "9400D3",
    "DEEPPINK": "FF1493",
    "DEEPSKYBLUE": "00BFFF",
    "DIMGRAY": "696969",
    "DIMGREY": "696969",
    "DODGERBLUE": "1E90FF",
    "FIREBRICK": "B22222",
    "FLORALWHITE": "FFFAF0",
    "FORESTGREEN": "228B22",
    "FUCHSIA": "FF00FF",
    "GAINSBORO": "DCDCDC",
    "GHOSTWHITE": "F8F8FF",
    "GOLD": "FFD700",
    "GOLDENROD": "DAA520",
    "GRAY": "808080",
    "GREY": "808080",
    "GREEN": "008000",
    "GREENYELLOW": "ADFF2F",
    "HONEYDEW": "F0FFF0",
    "HOTPINK": "FF69B4",
    "INDIANRED": "CD5C5C",
    "INDIGO": "4B0082",
    "IVORY": "FFFFF0",
    "KHAKI": "F0E68C",
    "LAVENDER": "E6E6FA",
    "LAVENDERBLUSH": "FFF0F5",
    "LAWNGREEN": "7CFC00",
    "LEMONCHIFFON": "FFFACD",
    "LIGHTBLUE": "ADD8E6",
    "LIGHTCORAL": "F08080",
    "LIGHTCYAN": "E0FFFF",
    "LIGHTGOLDENRODYELLOW": "FAFAD2",
    "LIGHTGRAY": "D3D3D3",
    "LIGHTGREY": "D3D3D3",
    "LIGHTGREEN": "90EE90",
    "LIGHTPINK": "FFB6C1",
    "LIGHTSALMON": "FFA07A",
    "LIGHTSEAGREEN": "20B2AA",
    "LIGHTSKYBLUE": "87CEFA",
    "LIGHTSLATEGRAY": "778899",
    "LIGHTSLATEGREY": "778899",
    "LIGHTSTEELBLUE": "B0C4DE",
    "LIGHTYELLOW": "FFFFE0",
    "LIME": "00FF00",
    "LIMEGREEN": "32CD32",
    "LINEN": "FAF0E6",
    "MAGENTA": "FF00FF",
    "MAROON": "800000",
    "MEDIUMAQUAMARINE": "66CDAA",
    "MEDIUMBLUE": "0000CD",
    "MEDIUMORCHID": "BA55D3",
    "MEDIUMPURPLE": "9370DB",
    "MEDIUMSEAGREEN": "3CB371",
    "MEDIUMSLATEBLUE": "7B68EE",
    "MEDIUMSPRINGGREEN": "00FA9A",
    "MEDIUMTURQUOISE": "48D1CC",
    "MEDIUMVIOLETRED": "C71585",
    "MIDNIGHTBLUE": "191970",
    "MINTCREAM": "F5FFFA",
    "MISTYROSE": "FFE4E1",
    "MOCCASIN": "FFE4B5",
    "NAVAJOWHITE": "FFDEAD",
    "NAVY": "000080",
    "OLDLACE": "FDF5E6",
    "OLIVE": "808000",
    "OLIVEDRAB": "6B8E23",
    "ORANGE": "FFA500",
    "ORANGERED": "FF4500",
    "ORCHID": "DA70D6",
    "PALEGOLDENROD": "EEE8AA",
    "PALEGREEN": "98FB98",
    "PALETURQUOISE": "AFEEEE",
    "PALEVIOLETRED": "DB7093",
    "PAPAYAWHIP": "FFEFD5",
    "PEACHPUFF": "FFDAB9",
    "PERU": "CD853F",
    "PINK": "FFC0CB",
    "PLUM": "DDA0DD",
    "POWDERBLUE": "B0E0E6",
    "PURPLE": "800080",
    "RED": "FF0000",
    "ROSYBROWN": "BC8F8F",
    "ROYALBLUE": "4169E1",
    "SADDLEBROWN": "8B4513",
    "SALMON": "FA8072",
    "SANDYBROWN": "F4A460",
    "SEAGREEN": "2E8B57",
    "SEASHELL": "FFF5EE",
    "SIENNA": "A0522D",
    "SILVER": "C0C0C0",
    "SKYBLUE": "87CEEB",
    "SLATEBLUE": "6A5ACD",
    "SLATEGRAY": "708090",
    "SLATEGREY": "708090",
    "SNOW": "FFFAFA",
    "SPRINGGREEN": "00FF7F",
    "STEELBLUE": "4682B4",
    "TAN": "D2B48C",
    "TEAL": "008080",
    "THISTLE": "D8BFD8",
    "TOMATO": "FF6347",
    "TURQUOISE": "40E0D0",
    "VIOLET": "EE82EE",
    "WHEAT": "F5DEB3",
    "WHITE": "FFFFFF",
    "WHITESMOKE": "F5F5F5",
    "YELLOW": "FFFF00",
    "YELLOWGREEN": "9ACD32"
    fileprivate static let cssToHexDictionairy : Dictionary<String, String> = [
    "CLEAR" : "00000000",
    "TRANSPARENT" : "00000000",
    "" : "00000000",
    "ALICEBLUE" : "F0F8FF",
    "ANTIQUEWHITE" : "FAEBD7",
    "AQUA" : "00FFFF",
    "AQUAMARINE" : "7FFFD4",
    "AZURE" : "F0FFFF",
    "BEIGE" : "F5F5DC",
    "BISQUE" : "FFE4C4",
    "BLACK" : "000000",
    "BLANCHEDALMOND" : "FFEBCD",
    "BLUE" : "0000FF",
    "BLUEVIOLET" : "8A2BE2",
    "BROWN" : "A52A2A",
    "BURLYWOOD" : "DEB887",
    "CADETBLUE" : "5F9EA0",
    "CHARTREUSE" : "7FFF00",
    "CHOCOLATE" : "D2691E",
    "CORAL" : "FF7F50",
    "CORNFLOWERBLUE" : "6495ED",
    "CORNSILK" : "FFF8DC",
    "CRIMSON" : "DC143C",
    "CYAN" : "00FFFF",
    "DARKBLUE" : "00008B",
    "DARKCYAN" : "008B8B",
    "DARKGOLDENROD" : "B8860B",
    "DARKGRAY" : "A9A9A9",
    "DARKGREY" : "A9A9A9",
    "DARKGREEN" : "006400",
    "DARKKHAKI" : "BDB76B",
    "DARKMAGENTA" : "8B008B",
    "DARKOLIVEGREEN" : "556B2F",
    "DARKORANGE" : "FF8C00",
    "DARKORCHID" : "9932CC",
    "DARKRED" : "8B0000",
    "DARKSALMON" : "E9967A",
    "DARKSEAGREEN" : "8FBC8F",
    "DARKSLATEBLUE" : "483D8B",
    "DARKSLATEGRAY" : "2F4F4F",
    "DARKSLATEGREY" : "2F4F4F",
    "DARKTURQUOISE" : "00CED1",
    "DARKVIOLET" : "9400D3",
    "DEEPPINK" : "FF1493",
    "DEEPSKYBLUE" : "00BFFF",
    "DIMGRAY" : "696969",
    "DIMGREY" : "696969",
    "DODGERBLUE" : "1E90FF",
    "FIREBRICK" : "B22222",
    "FLORALWHITE" : "FFFAF0",
    "FORESTGREEN" : "228B22",
    "FUCHSIA" : "FF00FF",
    "GAINSBORO" : "DCDCDC",
    "GHOSTWHITE" : "F8F8FF",
    "GOLD" : "FFD700",
    "GOLDENROD" : "DAA520",
    "GRAY" : "808080",
    "GREY" : "808080",
    "GREEN" : "008000",
    "GREENYELLOW" : "ADFF2F",
    "HONEYDEW" : "F0FFF0",
    "HOTPINK" : "FF69B4",
    "INDIANRED" : "CD5C5C",
    "INDIGO" : "4B0082",
    "IVORY" : "FFFFF0",
    "KHAKI" : "F0E68C",
    "LAVENDER" : "E6E6FA",
    "LAVENDERBLUSH" : "FFF0F5",
    "LAWNGREEN" : "7CFC00",
    "LEMONCHIFFON" : "FFFACD",
    "LIGHTBLUE" : "ADD8E6",
    "LIGHTCORAL" : "F08080",
    "LIGHTCYAN" : "E0FFFF",
    "LIGHTGOLDENRODYELLOW" : "FAFAD2",
    "LIGHTGRAY" : "D3D3D3",
    "LIGHTGREY" : "D3D3D3",
    "LIGHTGREEN" : "90EE90",
    "LIGHTPINK" : "FFB6C1",
    "LIGHTSALMON" : "FFA07A",
    "LIGHTSEAGREEN" : "20B2AA",
    "LIGHTSKYBLUE" : "87CEFA",
    "LIGHTSLATEGRAY" : "778899",
    "LIGHTSLATEGREY" : "778899",
    "LIGHTSTEELBLUE" : "B0C4DE",
    "LIGHTYELLOW" : "FFFFE0",
    "LIME" : "00FF00",
    "LIMEGREEN" : "32CD32",
    "LINEN" : "FAF0E6",
    "MAGENTA" : "FF00FF",
    "MAROON" : "800000",
    "MEDIUMAQUAMARINE" : "66CDAA",
    "MEDIUMBLUE" : "0000CD",
    "MEDIUMORCHID" : "BA55D3",
    "MEDIUMPURPLE" : "9370DB",
    "MEDIUMSEAGREEN" : "3CB371",
    "MEDIUMSLATEBLUE" : "7B68EE",
    "MEDIUMSPRINGGREEN" : "00FA9A",
    "MEDIUMTURQUOISE" : "48D1CC",
    "MEDIUMVIOLETRED" : "C71585",
    "MIDNIGHTBLUE" : "191970",
    "MINTCREAM" : "F5FFFA",
    "MISTYROSE" : "FFE4E1",
    "MOCCASIN" : "FFE4B5",
    "NAVAJOWHITE" : "FFDEAD",
    "NAVY" : "000080",
    "OLDLACE" : "FDF5E6",
    "OLIVE" : "808000",
    "OLIVEDRAB" : "6B8E23",
    "ORANGE" : "FFA500",
    "ORANGERED" : "FF4500",
    "ORCHID" : "DA70D6",
    "PALEGOLDENROD" : "EEE8AA",
    "PALEGREEN" : "98FB98",
    "PALETURQUOISE" : "AFEEEE",
    "PALEVIOLETRED" : "DB7093",
    "PAPAYAWHIP" : "FFEFD5",
    "PEACHPUFF" : "FFDAB9",
    "PERU" : "CD853F",
    "PINK" : "FFC0CB",
    "PLUM" : "DDA0DD",
    "POWDERBLUE" : "B0E0E6",
    "PURPLE" : "800080",
    "RED" : "FF0000",
    "ROSYBROWN" : "BC8F8F",
    "ROYALBLUE" : "4169E1",
    "SADDLEBROWN" : "8B4513",
    "SALMON" : "FA8072",
    "SANDYBROWN" : "F4A460",
    "SEAGREEN" : "2E8B57",
    "SEASHELL" : "FFF5EE",
    "SIENNA" : "A0522D",
    "SILVER" : "C0C0C0",
    "SKYBLUE" : "87CEEB",
    "SLATEBLUE" : "6A5ACD",
    "SLATEGRAY" : "708090",
    "SLATEGREY" : "708090",
    "SNOW" : "FFFAFA",
    "SPRINGGREEN" : "00FF7F",
    "STEELBLUE" : "4682B4",
    "TAN" : "D2B48C",
    "TEAL" : "008080",
    "THISTLE" : "D8BFD8",
    "TOMATO" : "FF6347",
    "TURQUOISE" : "40E0D0",
    "VIOLET" : "EE82EE",
    "WHEAT" : "F5DEB3",
    "WHITE" : "FFFFFF",
    "WHITESMOKE" : "F5F5F5",
    "YELLOW" : "FFFF00",
    "YELLOWGREEN" : "9ACD32"
    ]
    }
    }
  5. Norman Basham revised this gist Jul 9, 2017. 1 changed file with 173 additions and 173 deletions.
    346 changes: 173 additions & 173 deletions UIColor.swift
    Original file line number Diff line number Diff line change
    @@ -26,12 +26,12 @@
    import UIKit

    public extension UIColor {

    /**
    Creates an immuatble UIColor instance specified by a hex string, CSS color name, or nil.

    - parameter hexString: A case insensitive String? representing a hex or CSS value e.g.

    - **"abc"**
    - **"abc7"**
    - **"#abc7"**
    @@ -50,16 +50,16 @@ public extension UIColor {
    Scanner(string: normalizedHexString).scanHexInt32(&c)
    self.init(red:UIColorMasks.redValue(c), green:UIColorMasks.greenValue(c), blue:UIColorMasks.blueValue(c), alpha:UIColorMasks.alphaValue(c))
    }

    /**
    Returns a hex equivalent of this UIColor.

    - Parameter includeAlpha: Optional parameter to include the alpha hex.

    color.hexDescription() -> "ff0000"

    color.hexDescription(true) -> "ff0000aa"

    - Returns: A new string with `String` with the color's hexidecimal value.
    */
    public func hexDescription(_ includeAlpha: Bool = false) -> String {
    @@ -74,50 +74,50 @@ public extension UIColor {
    }
    return color
    }

    fileprivate enum UIColorMasks: CUnsignedInt {
    case redMask = 0xff000000
    case greenMask = 0x00ff0000
    case blueMask = 0x0000ff00
    case alphaMask = 0x000000ff

    static func redValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & redMask.rawValue) >> 24) / 255.0
    }

    static func greenValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & greenMask.rawValue) >> 16) / 255.0
    }

    static func blueValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat((value & blueMask.rawValue) >> 8) / 255.0
    }

    static func alphaValue(_ value: CUnsignedInt) -> CGFloat {
    return CGFloat(value & alphaMask.rawValue) / 255.0
    }
    }

    fileprivate static func normalize(_ hex: String?) -> String {
    guard var hexString = hex else {
    return "00000000"
    }
    if let cssColor = cssToHexDictionairy[hexString.uppercased()] {
    return cssColor.characters.count == 8 ? cssColor : cssColor + "ff"
    }
    if (hexString.hasPrefix("#")) {
    if hexString.hasPrefix("#") {
    hexString = String(hexString.characters.dropFirst())
    }
    if hexString.characters.count == 3 || hexString.characters.count == 4 {
    hexString = hexString.characters.map{ "\($0)\($0)" }.joined()
    hexString = hexString.characters.map { "\($0)\($0)" } .joined()
    }
    let hasAlpha = hexString.characters.count > 7
    if (!hasAlpha) {
    hexString = hexString + "ff"
    if !hasAlpha {
    hexString += "ff"
    }
    return hexString;
    return hexString
    }

    /**
    All modern browsers support the following 140 color names (see http://www.w3schools.com/cssref/css_colornames.asp)
    */
    @@ -128,157 +128,157 @@ public extension UIColor {
    }
    return cssName
    }
    fileprivate static let cssToHexDictionairy : Dictionary<String, String> = [
    "CLEAR" : "00000000",
    "TRANSPARENT" : "00000000",
    "" : "00000000",
    "ALICEBLUE" : "F0F8FF",
    "ANTIQUEWHITE" : "FAEBD7",
    "AQUA" : "00FFFF",
    "AQUAMARINE" : "7FFFD4",
    "AZURE" : "F0FFFF",
    "BEIGE" : "F5F5DC",
    "BISQUE" : "FFE4C4",
    "BLACK" : "000000",
    "BLANCHEDALMOND" : "FFEBCD",
    "BLUE" : "0000FF",
    "BLUEVIOLET" : "8A2BE2",
    "BROWN" : "A52A2A",
    "BURLYWOOD" : "DEB887",
    "CADETBLUE" : "5F9EA0",
    "CHARTREUSE" : "7FFF00",
    "CHOCOLATE" : "D2691E",
    "CORAL" : "FF7F50",
    "CORNFLOWERBLUE" : "6495ED",
    "CORNSILK" : "FFF8DC",
    "CRIMSON" : "DC143C",
    "CYAN" : "00FFFF",
    "DARKBLUE" : "00008B",
    "DARKCYAN" : "008B8B",
    "DARKGOLDENROD" : "B8860B",
    "DARKGRAY" : "A9A9A9",
    "DARKGREY" : "A9A9A9",
    "DARKGREEN" : "006400",
    "DARKKHAKI" : "BDB76B",
    "DARKMAGENTA" : "8B008B",
    "DARKOLIVEGREEN" : "556B2F",
    "DARKORANGE" : "FF8C00",
    "DARKORCHID" : "9932CC",
    "DARKRED" : "8B0000",
    "DARKSALMON" : "E9967A",
    "DARKSEAGREEN" : "8FBC8F",
    "DARKSLATEBLUE" : "483D8B",
    "DARKSLATEGRAY" : "2F4F4F",
    "DARKSLATEGREY" : "2F4F4F",
    "DARKTURQUOISE" : "00CED1",
    "DARKVIOLET" : "9400D3",
    "DEEPPINK" : "FF1493",
    "DEEPSKYBLUE" : "00BFFF",
    "DIMGRAY" : "696969",
    "DIMGREY" : "696969",
    "DODGERBLUE" : "1E90FF",
    "FIREBRICK" : "B22222",
    "FLORALWHITE" : "FFFAF0",
    "FORESTGREEN" : "228B22",
    "FUCHSIA" : "FF00FF",
    "GAINSBORO" : "DCDCDC",
    "GHOSTWHITE" : "F8F8FF",
    "GOLD" : "FFD700",
    "GOLDENROD" : "DAA520",
    "GRAY" : "808080",
    "GREY" : "808080",
    "GREEN" : "008000",
    "GREENYELLOW" : "ADFF2F",
    "HONEYDEW" : "F0FFF0",
    "HOTPINK" : "FF69B4",
    "INDIANRED" : "CD5C5C",
    "INDIGO" : "4B0082",
    "IVORY" : "FFFFF0",
    "KHAKI" : "F0E68C",
    "LAVENDER" : "E6E6FA",
    "LAVENDERBLUSH" : "FFF0F5",
    "LAWNGREEN" : "7CFC00",
    "LEMONCHIFFON" : "FFFACD",
    "LIGHTBLUE" : "ADD8E6",
    "LIGHTCORAL" : "F08080",
    "LIGHTCYAN" : "E0FFFF",
    "LIGHTGOLDENRODYELLOW" : "FAFAD2",
    "LIGHTGRAY" : "D3D3D3",
    "LIGHTGREY" : "D3D3D3",
    "LIGHTGREEN" : "90EE90",
    "LIGHTPINK" : "FFB6C1",
    "LIGHTSALMON" : "FFA07A",
    "LIGHTSEAGREEN" : "20B2AA",
    "LIGHTSKYBLUE" : "87CEFA",
    "LIGHTSLATEGRAY" : "778899",
    "LIGHTSLATEGREY" : "778899",
    "LIGHTSTEELBLUE" : "B0C4DE",
    "LIGHTYELLOW" : "FFFFE0",
    "LIME" : "00FF00",
    "LIMEGREEN" : "32CD32",
    "LINEN" : "FAF0E6",
    "MAGENTA" : "FF00FF",
    "MAROON" : "800000",
    "MEDIUMAQUAMARINE" : "66CDAA",
    "MEDIUMBLUE" : "0000CD",
    "MEDIUMORCHID" : "BA55D3",
    "MEDIUMPURPLE" : "9370DB",
    "MEDIUMSEAGREEN" : "3CB371",
    "MEDIUMSLATEBLUE" : "7B68EE",
    "MEDIUMSPRINGGREEN" : "00FA9A",
    "MEDIUMTURQUOISE" : "48D1CC",
    "MEDIUMVIOLETRED" : "C71585",
    "MIDNIGHTBLUE" : "191970",
    "MINTCREAM" : "F5FFFA",
    "MISTYROSE" : "FFE4E1",
    "MOCCASIN" : "FFE4B5",
    "NAVAJOWHITE" : "FFDEAD",
    "NAVY" : "000080",
    "OLDLACE" : "FDF5E6",
    "OLIVE" : "808000",
    "OLIVEDRAB" : "6B8E23",
    "ORANGE" : "FFA500",
    "ORANGERED" : "FF4500",
    "ORCHID" : "DA70D6",
    "PALEGOLDENROD" : "EEE8AA",
    "PALEGREEN" : "98FB98",
    "PALETURQUOISE" : "AFEEEE",
    "PALEVIOLETRED" : "DB7093",
    "PAPAYAWHIP" : "FFEFD5",
    "PEACHPUFF" : "FFDAB9",
    "PERU" : "CD853F",
    "PINK" : "FFC0CB",
    "PLUM" : "DDA0DD",
    "POWDERBLUE" : "B0E0E6",
    "PURPLE" : "800080",
    "RED" : "FF0000",
    "ROSYBROWN" : "BC8F8F",
    "ROYALBLUE" : "4169E1",
    "SADDLEBROWN" : "8B4513",
    "SALMON" : "FA8072",
    "SANDYBROWN" : "F4A460",
    "SEAGREEN" : "2E8B57",
    "SEASHELL" : "FFF5EE",
    "SIENNA" : "A0522D",
    "SILVER" : "C0C0C0",
    "SKYBLUE" : "87CEEB",
    "SLATEBLUE" : "6A5ACD",
    "SLATEGRAY" : "708090",
    "SLATEGREY" : "708090",
    "SNOW" : "FFFAFA",
    "SPRINGGREEN" : "00FF7F",
    "STEELBLUE" : "4682B4",
    "TAN" : "D2B48C",
    "TEAL" : "008080",
    "THISTLE" : "D8BFD8",
    "TOMATO" : "FF6347",
    "TURQUOISE" : "40E0D0",
    "VIOLET" : "EE82EE",
    "WHEAT" : "F5DEB3",
    "WHITE" : "FFFFFF",
    "WHITESMOKE" : "F5F5F5",
    "YELLOW" : "FFFF00",
    "YELLOWGREEN" : "9ACD32"

    fileprivate static let cssToHexDictionairy: [String: String] = [
    "CLEAR": "00000000",
    "TRANSPARENT": "00000000",
    "": "00000000",
    "ALICEBLUE": "F0F8FF",
    "ANTIQUEWHITE": "FAEBD7",
    "AQUA": "00FFFF",
    "AQUAMARINE": "7FFFD4",
    "AZURE": "F0FFFF",
    "BEIGE": "F5F5DC",
    "BISQUE": "FFE4C4",
    "BLACK": "000000",
    "BLANCHEDALMOND": "FFEBCD",
    "BLUE": "0000FF",
    "BLUEVIOLET": "8A2BE2",
    "BROWN": "A52A2A",
    "BURLYWOOD": "DEB887",
    "CADETBLUE": "5F9EA0",
    "CHARTREUSE": "7FFF00",
    "CHOCOLATE": "D2691E",
    "CORAL": "FF7F50",
    "CORNFLOWERBLUE": "6495ED",
    "CORNSILK": "FFF8DC",
    "CRIMSON": "DC143C",
    "CYAN": "00FFFF",
    "DARKBLUE": "00008B",
    "DARKCYAN": "008B8B",
    "DARKGOLDENROD": "B8860B",
    "DARKGRAY": "A9A9A9",
    "DARKGREY": "A9A9A9",
    "DARKGREEN": "006400",
    "DARKKHAKI": "BDB76B",
    "DARKMAGENTA": "8B008B",
    "DARKOLIVEGREEN": "556B2F",
    "DARKORANGE": "FF8C00",
    "DARKORCHID": "9932CC",
    "DARKRED": "8B0000",
    "DARKSALMON": "E9967A",
    "DARKSEAGREEN": "8FBC8F",
    "DARKSLATEBLUE": "483D8B",
    "DARKSLATEGRAY": "2F4F4F",
    "DARKSLATEGREY": "2F4F4F",
    "DARKTURQUOISE": "00CED1",
    "DARKVIOLET": "9400D3",
    "DEEPPINK": "FF1493",
    "DEEPSKYBLUE": "00BFFF",
    "DIMGRAY": "696969",
    "DIMGREY": "696969",
    "DODGERBLUE": "1E90FF",
    "FIREBRICK": "B22222",
    "FLORALWHITE": "FFFAF0",
    "FORESTGREEN": "228B22",
    "FUCHSIA": "FF00FF",
    "GAINSBORO": "DCDCDC",
    "GHOSTWHITE": "F8F8FF",
    "GOLD": "FFD700",
    "GOLDENROD": "DAA520",
    "GRAY": "808080",
    "GREY": "808080",
    "GREEN": "008000",
    "GREENYELLOW": "ADFF2F",
    "HONEYDEW": "F0FFF0",
    "HOTPINK": "FF69B4",
    "INDIANRED": "CD5C5C",
    "INDIGO": "4B0082",
    "IVORY": "FFFFF0",
    "KHAKI": "F0E68C",
    "LAVENDER": "E6E6FA",
    "LAVENDERBLUSH": "FFF0F5",
    "LAWNGREEN": "7CFC00",
    "LEMONCHIFFON": "FFFACD",
    "LIGHTBLUE": "ADD8E6",
    "LIGHTCORAL": "F08080",
    "LIGHTCYAN": "E0FFFF",
    "LIGHTGOLDENRODYELLOW": "FAFAD2",
    "LIGHTGRAY": "D3D3D3",
    "LIGHTGREY": "D3D3D3",
    "LIGHTGREEN": "90EE90",
    "LIGHTPINK": "FFB6C1",
    "LIGHTSALMON": "FFA07A",
    "LIGHTSEAGREEN": "20B2AA",
    "LIGHTSKYBLUE": "87CEFA",
    "LIGHTSLATEGRAY": "778899",
    "LIGHTSLATEGREY": "778899",
    "LIGHTSTEELBLUE": "B0C4DE",
    "LIGHTYELLOW": "FFFFE0",
    "LIME": "00FF00",
    "LIMEGREEN": "32CD32",
    "LINEN": "FAF0E6",
    "MAGENTA": "FF00FF",
    "MAROON": "800000",
    "MEDIUMAQUAMARINE": "66CDAA",
    "MEDIUMBLUE": "0000CD",
    "MEDIUMORCHID": "BA55D3",
    "MEDIUMPURPLE": "9370DB",
    "MEDIUMSEAGREEN": "3CB371",
    "MEDIUMSLATEBLUE": "7B68EE",
    "MEDIUMSPRINGGREEN": "00FA9A",
    "MEDIUMTURQUOISE": "48D1CC",
    "MEDIUMVIOLETRED": "C71585",
    "MIDNIGHTBLUE": "191970",
    "MINTCREAM": "F5FFFA",
    "MISTYROSE": "FFE4E1",
    "MOCCASIN": "FFE4B5",
    "NAVAJOWHITE": "FFDEAD",
    "NAVY": "000080",
    "OLDLACE": "FDF5E6",
    "OLIVE": "808000",
    "OLIVEDRAB": "6B8E23",
    "ORANGE": "FFA500",
    "ORANGERED": "FF4500",
    "ORCHID": "DA70D6",
    "PALEGOLDENROD": "EEE8AA",
    "PALEGREEN": "98FB98",
    "PALETURQUOISE": "AFEEEE",
    "PALEVIOLETRED": "DB7093",
    "PAPAYAWHIP": "FFEFD5",
    "PEACHPUFF": "FFDAB9",
    "PERU": "CD853F",
    "PINK": "FFC0CB",
    "PLUM": "DDA0DD",
    "POWDERBLUE": "B0E0E6",
    "PURPLE": "800080",
    "RED": "FF0000",
    "ROSYBROWN": "BC8F8F",
    "ROYALBLUE": "4169E1",
    "SADDLEBROWN": "8B4513",
    "SALMON": "FA8072",
    "SANDYBROWN": "F4A460",
    "SEAGREEN": "2E8B57",
    "SEASHELL": "FFF5EE",
    "SIENNA": "A0522D",
    "SILVER": "C0C0C0",
    "SKYBLUE": "87CEEB",
    "SLATEBLUE": "6A5ACD",
    "SLATEGRAY": "708090",
    "SLATEGREY": "708090",
    "SNOW": "FFFAFA",
    "SPRINGGREEN": "00FF7F",
    "STEELBLUE": "4682B4",
    "TAN": "D2B48C",
    "TEAL": "008080",
    "THISTLE": "D8BFD8",
    "TOMATO": "FF6347",
    "TURQUOISE": "40E0D0",
    "VIOLET": "EE82EE",
    "WHEAT": "F5DEB3",
    "WHITE": "FFFFFF",
    "WHITESMOKE": "F5F5F5",
    "YELLOW": "FFFF00",
    "YELLOWGREEN": "9ACD32"
    ]
    }
    }
  6. Norman Basham revised this gist Mar 19, 2017. 1 changed file with 11 additions and 27 deletions.
    38 changes: 11 additions & 27 deletions UIColor.swift
    Original file line number Diff line number Diff line change
    @@ -66,21 +66,13 @@ public extension UIColor {
    guard self.cgColor.numberOfComponents == 4 else {
    return "Color not RGB."
    }
    if self.cgColor.numberOfComponents == 4 {
    if let comps = self.cgColor.components {
    let r = Int(comps[0] * 255)
    let g = Int(comps[1] * 255)
    let b = Int(comps[2] * 255)
    let color = String.init(format: "%02x%02x%02x", r, g, b)
    if includeAlpha {
    let a = Int(comps[3] * 255)
    let alpha = String.init(format: "%02x", a)
    return "\(color)\(alpha)"
    }
    return color
    }
    let a = self.cgColor.components!.map { Int($0 * CGFloat(255)) }
    let color = String.init(format: "%02x%02x%02x", a[0], a[1], a[2])
    if includeAlpha {
    let alpha = String.init(format: "%02x", a[3])
    return "\(color)\(alpha)"
    }
    return "Unknown error."
    return color
    }

    fileprivate enum UIColorMasks: CUnsignedInt {
    @@ -90,27 +82,19 @@ public extension UIColor {
    case alphaMask = 0x000000ff

    static func redValue(_ value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & redMask.rawValue) >> 24
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    return CGFloat((value & redMask.rawValue) >> 24) / 255.0
    }

    static func greenValue(_ value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & greenMask.rawValue) >> 16
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    return CGFloat((value & greenMask.rawValue) >> 16) / 255.0
    }

    static func blueValue(_ value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & blueMask.rawValue) >> 8
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    return CGFloat((value & blueMask.rawValue) >> 8) / 255.0
    }

    static func alphaValue(_ value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = value & alphaMask.rawValue
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    return CGFloat(value & alphaMask.rawValue) / 255.0
    }
    }

    @@ -297,4 +281,4 @@ public extension UIColor {
    "YELLOW" : "FFFF00",
    "YELLOWGREEN" : "9ACD32"
    ]
    }
    }
  7. Norman Basham revised this gist Mar 19, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion UIColor.swift
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    //
    // Color+HexAndCSSColorNames.swift
    // UIColor.swift
    // previously Color+HexAndCSSColorNames.swift
    //
    // Created by Norman Basham on 12/8/15.
    // Copyright © 2017 Black Labs. All rights reserved.
  8. Norman Basham revised this gist Mar 19, 2017. 1 changed file with 17 additions and 22 deletions.
    39 changes: 17 additions & 22 deletions UIColor.swift
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,5 @@
    //
    // UIColor.swift
    // previously Color+HexAndCSSColorNames.swift
    // Color+HexAndCSSColorNames.swift
    //
    // Created by Norman Basham on 12/8/15.
    // Copyright © 2017 Black Labs. All rights reserved.
    @@ -63,20 +62,24 @@ public extension UIColor {
    - Returns: A new string with `String` with the color's hexidecimal value.
    */
    public func hexDescription(_ includeAlpha: Bool = false) -> String {
    guard self.cgColor.numberOfComponents == 4 else {
    return "Color not RGB."
    }
    if self.cgColor.numberOfComponents == 4 {
    let components = self.cgColor.components
    let red = Float((components?[0])!) * 255.0
    let green = Float((components?[1])!) * 255.0
    let blue = Float((components?[2])!) * 255.0
    let alpha = Float((components?[3])!) * 255.0
    if (includeAlpha) {
    return String.init(format: "%02x%02x%02x%02x", Int(red), Int(green), Int(blue), Int(alpha))
    } else {
    return String.init(format: "%02x%02x%02x", Int(red), Int(green), Int(blue))
    if let comps = self.cgColor.components {
    let r = Int(comps[0] * 255)
    let g = Int(comps[1] * 255)
    let b = Int(comps[2] * 255)
    let color = String.init(format: "%02x%02x%02x", r, g, b)
    if includeAlpha {
    let a = Int(comps[3] * 255)
    let alpha = String.init(format: "%02x", a)
    return "\(color)\(alpha)"
    }
    return color
    }
    } else {
    return "Color not RGB."
    }
    return "Unknown error."
    }

    fileprivate enum UIColorMasks: CUnsignedInt {
    @@ -121,15 +124,7 @@ public extension UIColor {
    hexString = String(hexString.characters.dropFirst())
    }
    if hexString.characters.count == 3 || hexString.characters.count == 4 {
    let redHex = hexString.substring(to: hexString.characters.index(hexString.startIndex, offsetBy: 1))
    let greenHex = hexString.substring(with: Range<String.Index>(hexString.characters.index(hexString.startIndex, offsetBy: 1) ..< hexString.characters.index(hexString.startIndex, offsetBy: 2)))
    let blueHex = hexString.substring(with: Range<String.Index>(hexString.characters.index(hexString.startIndex, offsetBy: 2) ..< hexString.characters.index(hexString.startIndex, offsetBy: 3)))
    var alphaHex = ""
    if hexString.characters.count == 4 {
    alphaHex = hexString.substring(from: hexString.characters.index(hexString.startIndex, offsetBy: 3))
    }

    hexString = redHex + redHex + greenHex + greenHex + blueHex + blueHex + alphaHex + alphaHex
    hexString = hexString.characters.map{ "\($0)\($0)" }.joined()
    }
    let hasAlpha = hexString.characters.count > 7
    if (!hasAlpha) {
  9. Norman Basham renamed this gist Mar 19, 2017. 1 changed file with 10 additions and 13 deletions.
    23 changes: 10 additions & 13 deletions Color+HexAndCSSColorNames.swift → UIColor.swift
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    //
    // Color+HexAndCSSColorNames.swift
    // UIColor.swift
    // previously Color+HexAndCSSColorNames.swift
    //
    // Created by Norman Basham on 12/8/15.
    // Copyright © 2015 Black Labs. All rights reserved.
    // Copyright © 2017 Black Labs. All rights reserved.
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    @@ -113,14 +114,11 @@ public extension UIColor {
    guard var hexString = hex else {
    return "00000000"
    }
    let cssColor = cssToHexDictionairy[hexString.uppercased()]
    if cssColor != nil {
    return cssColor! + "ff"
    if let cssColor = cssToHexDictionairy[hexString.uppercased()] {
    return cssColor.characters.count == 8 ? cssColor : cssColor + "ff"
    }
    let hasHash : Bool = hexString.hasPrefix("#")
    if (hasHash) {
    if (hexString.hasPrefix("#")) {
    hexString = String(hexString.characters.dropFirst())

    }
    if hexString.characters.count == 3 || hexString.characters.count == 4 {
    let redHex = hexString.substring(to: hexString.characters.index(hexString.startIndex, offsetBy: 1))
    @@ -145,13 +143,12 @@ public extension UIColor {
    */
    fileprivate static func hexFromCssName(_ cssName: String) -> String {
    let key = cssName.uppercased()
    let hex = cssToHexDictionairy[key]
    if hex == nil {
    return cssName
    if let hex = cssToHexDictionairy[key] {
    return hex
    }
    return hex!
    return cssName
    }

    fileprivate static let cssToHexDictionairy : Dictionary<String, String> = [
    "CLEAR" : "00000000",
    "TRANSPARENT" : "00000000",
  10. Norman Basham revised this gist Jan 22, 2017. 1 changed file with 33 additions and 33 deletions.
    66 changes: 33 additions & 33 deletions Color+HexAndCSSColorNames.swift
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // Color+HexAndCSSColorNames.swift
    //
    // Created by Norman Basham on 12/8/15.
    // Copyright © 2016 Black Labs. All rights reserved.
    // Copyright © 2015 Black Labs. All rights reserved.
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    @@ -46,7 +46,7 @@ public extension UIColor {
    public convenience init(hex: String?) {
    let normalizedHexString: String = UIColor.normalize(hex)
    var c: CUnsignedInt = 0
    NSScanner(string: normalizedHexString).scanHexInt(&c)
    Scanner(string: normalizedHexString).scanHexInt32(&c)
    self.init(red:UIColorMasks.redValue(c), green:UIColorMasks.greenValue(c), blue:UIColorMasks.blueValue(c), alpha:UIColorMasks.alphaValue(c))
    }

    @@ -61,13 +61,13 @@ public extension UIColor {

    - Returns: A new string with `String` with the color's hexidecimal value.
    */
    public func hexDescription(includeAlpha: Bool = false) -> String {
    if CGColorGetNumberOfComponents(self.CGColor) == 4 {
    let components = CGColorGetComponents(self.CGColor)
    let red = Float(components[0]) * 255.0
    let green = Float(components[1]) * 255.0
    let blue = Float(components[2]) * 255.0
    let alpha = Float(components[3]) * 255.0
    public func hexDescription(_ includeAlpha: Bool = false) -> String {
    if self.cgColor.numberOfComponents == 4 {
    let components = self.cgColor.components
    let red = Float((components?[0])!) * 255.0
    let green = Float((components?[1])!) * 255.0
    let blue = Float((components?[2])!) * 255.0
    let alpha = Float((components?[3])!) * 255.0
    if (includeAlpha) {
    return String.init(format: "%02x%02x%02x%02x", Int(red), Int(green), Int(blue), Int(alpha))
    } else {
    @@ -78,42 +78,42 @@ public extension UIColor {
    }
    }

    private enum UIColorMasks: CUnsignedInt {
    case RedMask = 0xff000000
    case GreenMask = 0x00ff0000
    case BlueMask = 0x0000ff00
    case AlphaMask = 0x000000ff
    fileprivate enum UIColorMasks: CUnsignedInt {
    case redMask = 0xff000000
    case greenMask = 0x00ff0000
    case blueMask = 0x0000ff00
    case alphaMask = 0x000000ff

    static func redValue(value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & RedMask.rawValue) >> 24
    static func redValue(_ value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & redMask.rawValue) >> 24
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    }

    static func greenValue(value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & GreenMask.rawValue) >> 16
    static func greenValue(_ value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & greenMask.rawValue) >> 16
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    }

    static func blueValue(value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & BlueMask.rawValue) >> 8
    static func blueValue(_ value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & blueMask.rawValue) >> 8
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    }

    static func alphaValue(value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = value & AlphaMask.rawValue
    static func alphaValue(_ value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = value & alphaMask.rawValue
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    }
    }

    private static func normalize(hex: String?) -> String {
    fileprivate static func normalize(_ hex: String?) -> String {
    guard var hexString = hex else {
    return "00000000"
    }
    let cssColor = cssToHexDictionairy[hexString.uppercaseString]
    let cssColor = cssToHexDictionairy[hexString.uppercased()]
    if cssColor != nil {
    return cssColor! + "ff"
    }
    @@ -123,12 +123,12 @@ public extension UIColor {

    }
    if hexString.characters.count == 3 || hexString.characters.count == 4 {
    let redHex = hexString.substringToIndex(hexString.startIndex.advancedBy(1))
    let greenHex = hexString.substringWithRange(Range<String.Index>(hexString.startIndex.advancedBy(1) ..< hexString.startIndex.advancedBy(2)))
    let blueHex = hexString.substringWithRange(Range<String.Index>(hexString.startIndex.advancedBy(2) ..< hexString.startIndex.advancedBy(3)))
    let redHex = hexString.substring(to: hexString.characters.index(hexString.startIndex, offsetBy: 1))
    let greenHex = hexString.substring(with: Range<String.Index>(hexString.characters.index(hexString.startIndex, offsetBy: 1) ..< hexString.characters.index(hexString.startIndex, offsetBy: 2)))
    let blueHex = hexString.substring(with: Range<String.Index>(hexString.characters.index(hexString.startIndex, offsetBy: 2) ..< hexString.characters.index(hexString.startIndex, offsetBy: 3)))
    var alphaHex = ""
    if hexString.characters.count == 4 {
    alphaHex = hexString.substringFromIndex(hexString.startIndex.advancedBy(3))
    alphaHex = hexString.substring(from: hexString.characters.index(hexString.startIndex, offsetBy: 3))
    }

    hexString = redHex + redHex + greenHex + greenHex + blueHex + blueHex + alphaHex + alphaHex
    @@ -143,16 +143,16 @@ public extension UIColor {
    /**
    All modern browsers support the following 140 color names (see http://www.w3schools.com/cssref/css_colornames.asp)
    */
    private static func hexFromCssName(cssName: String) -> String {
    let key = cssName.uppercaseString
    fileprivate static func hexFromCssName(_ cssName: String) -> String {
    let key = cssName.uppercased()
    let hex = cssToHexDictionairy[key]
    if hex == nil {
    return cssName
    }
    return hex!
    }
    private static let cssToHexDictionairy : Dictionary<String, String> = [

    fileprivate static let cssToHexDictionairy : Dictionary<String, String> = [
    "CLEAR" : "00000000",
    "TRANSPARENT" : "00000000",
    "" : "00000000",
    @@ -304,4 +304,4 @@ public extension UIColor {
    "YELLOW" : "FFFF00",
    "YELLOWGREEN" : "9ACD32"
    ]
    }
    }
  11. Norman Basham revised this gist Aug 8, 2016. No changes.
  12. Norman Basham revised this gist Aug 8, 2016. 1 changed file with 8 additions and 5 deletions.
    13 changes: 8 additions & 5 deletions Color+HexAndCSSColorNames.swift
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    // Color+HexAndCSSColorNames.swift
    //
    // Created by Norman Basham on 12/8/15.
    // Copyright © 2015 Black Labs. All rights reserved.
    // Copyright © 2016 Black Labs. All rights reserved.
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    @@ -113,6 +113,10 @@ public extension UIColor {
    guard var hexString = hex else {
    return "00000000"
    }
    let cssColor = cssToHexDictionairy[hexString.uppercaseString]
    if cssColor != nil {
    return cssColor! + "ff"
    }
    let hasHash : Bool = hexString.hasPrefix("#")
    if (hasHash) {
    hexString = String(hexString.characters.dropFirst())
    @@ -129,7 +133,6 @@ public extension UIColor {

    hexString = redHex + redHex + greenHex + greenHex + blueHex + blueHex + alphaHex + alphaHex
    }
    hexString = UIColor.hexFromCssName(hexString)
    let hasAlpha = hexString.characters.count > 7
    if (!hasAlpha) {
    hexString = hexString + "ff"
    @@ -142,14 +145,14 @@ public extension UIColor {
    */
    private static func hexFromCssName(cssName: String) -> String {
    let key = cssName.uppercaseString
    let hex = cssToHexDictionary[key]
    let hex = cssToHexDictionairy[key]
    if hex == nil {
    return cssName
    }
    return hex!
    }

    private static let cssToHexDictionary : Dictionary<String, String> = [
    private static let cssToHexDictionairy : Dictionary<String, String> = [
    "CLEAR" : "00000000",
    "TRANSPARENT" : "00000000",
    "" : "00000000",
  13. Norman Basham revised this gist Aug 8, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Color+HexAndCSSColorNames.swift
    Original file line number Diff line number Diff line change
    @@ -144,7 +144,7 @@ public extension UIColor {
    let key = cssName.uppercaseString
    let hex = cssToHexDictionary[key]
    if hex == nil {
    return cssToHexDictionary["CLEAR"]!
    return cssName
    }
    return hex!
    }
  14. Norman Basham revised this gist Aug 6, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions Color+HexAndCSSColorNames.swift
    Original file line number Diff line number Diff line change
    @@ -142,14 +142,14 @@ public extension UIColor {
    */
    private static func hexFromCssName(cssName: String) -> String {
    let key = cssName.uppercaseString
    let hex = cssToHexDictionairy[key]
    let hex = cssToHexDictionary[key]
    if hex == nil {
    return cssToHexDictionairy["CLEAR"]!
    return cssToHexDictionary["CLEAR"]!
    }
    return hex!
    }

    private static let cssToHexDictionairy : Dictionary<String, String> = [
    private static let cssToHexDictionary : Dictionary<String, String> = [
    "CLEAR" : "00000000",
    "TRANSPARENT" : "00000000",
    "" : "00000000",
  15. Norman Basham revised this gist Aug 6, 2016. 1 changed file with 162 additions and 306 deletions.
    468 changes: 162 additions & 306 deletions Color+HexAndCSSColorNames.swift
    Original file line number Diff line number Diff line change
    @@ -108,7 +108,7 @@ public extension UIColor {
    return f;
    }
    }

    private static func normalize(hex: String?) -> String {
    guard var hexString = hex else {
    return "00000000"
    @@ -120,8 +120,8 @@ public extension UIColor {
    }
    if hexString.characters.count == 3 || hexString.characters.count == 4 {
    let redHex = hexString.substringToIndex(hexString.startIndex.advancedBy(1))
    let greenHex = hexString.substringWithRange(Range<String.Index>(start: hexString.startIndex.advancedBy(1), end: hexString.startIndex.advancedBy(2)))
    let blueHex = hexString.substringWithRange(Range<String.Index>(start: hexString.startIndex.advancedBy(2), end: hexString.startIndex.advancedBy(3)))
    let greenHex = hexString.substringWithRange(Range<String.Index>(hexString.startIndex.advancedBy(1) ..< hexString.startIndex.advancedBy(2)))
    let blueHex = hexString.substringWithRange(Range<String.Index>(hexString.startIndex.advancedBy(2) ..< hexString.startIndex.advancedBy(3)))
    var alphaHex = ""
    if hexString.characters.count == 4 {
    alphaHex = hexString.substringFromIndex(hexString.startIndex.advancedBy(3))
    @@ -141,308 +141,164 @@ public extension UIColor {
    All modern browsers support the following 140 color names (see http://www.w3schools.com/cssref/css_colornames.asp)
    */
    private static func hexFromCssName(cssName: String) -> String {
    var s: String = cssName
    if (cssName.caseInsensitiveCompare("Clear") == .OrderedSame) {
    s = "00000000"
    } else if (cssName.caseInsensitiveCompare("Transparent") == .OrderedSame) {
    s = "00000000"
    } else if (cssName.caseInsensitiveCompare("") == .OrderedSame) {
    s = "00000000"
    } else if(cssName.caseInsensitiveCompare("AliceBlue") == .OrderedSame) {
    s = "F0F8FF"
    } else if(cssName.caseInsensitiveCompare("AntiqueWhite") == .OrderedSame) {
    s = "FAEBD7"
    } else if(cssName.caseInsensitiveCompare("Aqua") == .OrderedSame) {
    s = "00FFFF"
    } else if(cssName.caseInsensitiveCompare("Aquamarine") == .OrderedSame) {
    s = "7FFFD4"
    } else if(cssName.caseInsensitiveCompare("Azure") == .OrderedSame) {
    s = "F0FFFF"
    } else if(cssName.caseInsensitiveCompare("Beige") == .OrderedSame) {
    s = "F5F5DC"
    } else if(cssName.caseInsensitiveCompare("Bisque") == .OrderedSame) {
    s = "FFE4C4"
    } else if(cssName.caseInsensitiveCompare("Black") == .OrderedSame) {
    s = "000000"
    } else if(cssName.caseInsensitiveCompare("BlanchedAlmond") == .OrderedSame) {
    s = "FFEBCD"
    } else if(cssName.caseInsensitiveCompare("Blue") == .OrderedSame) {
    s = "0000FF"
    } else if(cssName.caseInsensitiveCompare("BlueViolet") == .OrderedSame) {
    s = "8A2BE2"
    } else if(cssName.caseInsensitiveCompare("Brown") == .OrderedSame) {
    s = "A52A2A"
    } else if(cssName.caseInsensitiveCompare("BurlyWood") == .OrderedSame) {
    s = "DEB887"
    } else if(cssName.caseInsensitiveCompare("CadetBlue") == .OrderedSame) {
    s = "5F9EA0"
    } else if(cssName.caseInsensitiveCompare("Chartreuse") == .OrderedSame) {
    s = "7FFF00"
    } else if(cssName.caseInsensitiveCompare("Chocolate") == .OrderedSame) {
    s = "D2691E"
    } else if(cssName.caseInsensitiveCompare("Coral") == .OrderedSame) {
    s = "FF7F50"
    } else if(cssName.caseInsensitiveCompare("CornflowerBlue") == .OrderedSame) {
    s = "6495ED"
    } else if(cssName.caseInsensitiveCompare("Cornsilk") == .OrderedSame) {
    s = "FFF8DC"
    } else if(cssName.caseInsensitiveCompare("Crimson") == .OrderedSame) {
    s = "DC143C"
    } else if(cssName.caseInsensitiveCompare("Cyan") == .OrderedSame) {
    s = "00FFFF"
    } else if(cssName.caseInsensitiveCompare("DarkBlue") == .OrderedSame) {
    s = "00008B"
    } else if(cssName.caseInsensitiveCompare("DarkCyan") == .OrderedSame) {
    s = "008B8B"
    } else if(cssName.caseInsensitiveCompare("DarkGoldenRod") == .OrderedSame) {
    s = "B8860B"
    } else if(cssName.caseInsensitiveCompare("DarkGray") == .OrderedSame) {
    s = "A9A9A9"
    } else if(cssName.caseInsensitiveCompare("DarkGrey") == .OrderedSame) {
    s = "A9A9A9"
    } else if(cssName.caseInsensitiveCompare("DarkGreen") == .OrderedSame) {
    s = "006400"
    } else if(cssName.caseInsensitiveCompare("DarkKhaki") == .OrderedSame) {
    s = "BDB76B"
    } else if(cssName.caseInsensitiveCompare("DarkMagenta") == .OrderedSame) {
    s = "8B008B"
    } else if(cssName.caseInsensitiveCompare("DarkOliveGreen") == .OrderedSame) {
    s = "556B2F"
    } else if(cssName.caseInsensitiveCompare("DarkOrange") == .OrderedSame) {
    s = "FF8C00"
    } else if(cssName.caseInsensitiveCompare("DarkOrchid") == .OrderedSame) {
    s = "9932CC"
    } else if(cssName.caseInsensitiveCompare("DarkRed") == .OrderedSame) {
    s = "8B0000"
    } else if(cssName.caseInsensitiveCompare("DarkSalmon") == .OrderedSame) {
    s = "E9967A"
    } else if(cssName.caseInsensitiveCompare("DarkSeaGreen") == .OrderedSame) {
    s = "8FBC8F"
    } else if(cssName.caseInsensitiveCompare("DarkSlateBlue") == .OrderedSame) {
    s = "483D8B"
    } else if(cssName.caseInsensitiveCompare("DarkSlateGray") == .OrderedSame) {
    s = "2F4F4F"
    } else if(cssName.caseInsensitiveCompare("DarkSlateGrey") == .OrderedSame) {
    s = "2F4F4F"
    } else if(cssName.caseInsensitiveCompare("DarkTurquoise") == .OrderedSame) {
    s = "00CED1"
    } else if(cssName.caseInsensitiveCompare("DarkViolet") == .OrderedSame) {
    s = "9400D3"
    } else if(cssName.caseInsensitiveCompare("DeepPink") == .OrderedSame) {
    s = "FF1493"
    } else if(cssName.caseInsensitiveCompare("DeepSkyBlue") == .OrderedSame) {
    s = "00BFFF"
    } else if(cssName.caseInsensitiveCompare("DimGray") == .OrderedSame) {
    s = "696969"
    } else if(cssName.caseInsensitiveCompare("DimGrey") == .OrderedSame) {
    s = "696969"
    } else if(cssName.caseInsensitiveCompare("DodgerBlue") == .OrderedSame) {
    s = "1E90FF"
    } else if(cssName.caseInsensitiveCompare("FireBrick") == .OrderedSame) {
    s = "B22222"
    } else if(cssName.caseInsensitiveCompare("FloralWhite") == .OrderedSame) {
    s = "FFFAF0"
    } else if(cssName.caseInsensitiveCompare("ForestGreen") == .OrderedSame) {
    s = "228B22"
    } else if(cssName.caseInsensitiveCompare("Fuchsia") == .OrderedSame) {
    s = "FF00FF"
    } else if(cssName.caseInsensitiveCompare("Gainsboro") == .OrderedSame) {
    s = "DCDCDC"
    } else if(cssName.caseInsensitiveCompare("GhostWhite") == .OrderedSame) {
    s = "F8F8FF"
    } else if(cssName.caseInsensitiveCompare("Gold") == .OrderedSame) {
    s = "FFD700"
    } else if(cssName.caseInsensitiveCompare("GoldenRod") == .OrderedSame) {
    s = "DAA520"
    } else if(cssName.caseInsensitiveCompare("Gray") == .OrderedSame) {
    s = "808080"
    } else if(cssName.caseInsensitiveCompare("Grey") == .OrderedSame) {
    s = "808080"
    } else if(cssName.caseInsensitiveCompare("Green") == .OrderedSame) {
    s = "008000"
    } else if(cssName.caseInsensitiveCompare("GreenYellow") == .OrderedSame) {
    s = "ADFF2F"
    } else if(cssName.caseInsensitiveCompare("HoneyDew") == .OrderedSame) {
    s = "F0FFF0"
    } else if(cssName.caseInsensitiveCompare("HotPink") == .OrderedSame) {
    s = "FF69B4"
    } else if(cssName.caseInsensitiveCompare("IndianRed") == .OrderedSame) {
    s = "CD5C5C"
    } else if(cssName.caseInsensitiveCompare("Indigo") == .OrderedSame) {
    s = "4B0082"
    } else if(cssName.caseInsensitiveCompare("Ivory") == .OrderedSame) {
    s = "FFFFF0"
    } else if(cssName.caseInsensitiveCompare("Khaki") == .OrderedSame) {
    s = "F0E68C"
    } else if(cssName.caseInsensitiveCompare("Lavender") == .OrderedSame) {
    s = "E6E6FA"
    } else if(cssName.caseInsensitiveCompare("LavenderBlush") == .OrderedSame) {
    s = "FFF0F5"
    } else if(cssName.caseInsensitiveCompare("LawnGreen") == .OrderedSame) {
    s = "7CFC00"
    } else if(cssName.caseInsensitiveCompare("LemonChiffon") == .OrderedSame) {
    s = "FFFACD"
    } else if(cssName.caseInsensitiveCompare("LightBlue") == .OrderedSame) {
    s = "ADD8E6"
    } else if(cssName.caseInsensitiveCompare("LightCoral") == .OrderedSame) {
    s = "F08080"
    } else if(cssName.caseInsensitiveCompare("LightCyan") == .OrderedSame) {
    s = "E0FFFF"
    } else if(cssName.caseInsensitiveCompare("LightGoldenRodYellow") == .OrderedSame) {
    s = "FAFAD2"
    } else if(cssName.caseInsensitiveCompare("LightGray") == .OrderedSame) {
    s = "D3D3D3"
    } else if(cssName.caseInsensitiveCompare("LightGrey") == .OrderedSame) {
    s = "D3D3D3"
    } else if(cssName.caseInsensitiveCompare("LightGreen") == .OrderedSame) {
    s = "90EE90"
    } else if(cssName.caseInsensitiveCompare("LightPink") == .OrderedSame) {
    s = "FFB6C1"
    } else if(cssName.caseInsensitiveCompare("LightSalmon") == .OrderedSame) {
    s = "FFA07A"
    } else if(cssName.caseInsensitiveCompare("LightSeaGreen") == .OrderedSame) {
    s = "20B2AA"
    } else if(cssName.caseInsensitiveCompare("LightSkyBlue") == .OrderedSame) {
    s = "87CEFA"
    } else if(cssName.caseInsensitiveCompare("LightSlateGray") == .OrderedSame) {
    s = "778899"
    } else if(cssName.caseInsensitiveCompare("LightSlateGrey") == .OrderedSame) {
    s = "778899"
    } else if(cssName.caseInsensitiveCompare("LightSteelBlue") == .OrderedSame) {
    s = "B0C4DE"
    } else if(cssName.caseInsensitiveCompare("LightYellow") == .OrderedSame) {
    s = "FFFFE0"
    } else if(cssName.caseInsensitiveCompare("Lime") == .OrderedSame) {
    s = "00FF00"
    } else if(cssName.caseInsensitiveCompare("LimeGreen") == .OrderedSame) {
    s = "32CD32"
    } else if(cssName.caseInsensitiveCompare("Linen") == .OrderedSame) {
    s = "FAF0E6"
    } else if(cssName.caseInsensitiveCompare("Magenta") == .OrderedSame) {
    s = "FF00FF"
    } else if(cssName.caseInsensitiveCompare("Maroon") == .OrderedSame) {
    s = "800000"
    } else if(cssName.caseInsensitiveCompare("MediumAquaMarine") == .OrderedSame) {
    s = "66CDAA"
    } else if(cssName.caseInsensitiveCompare("MediumBlue") == .OrderedSame) {
    s = "0000CD"
    } else if(cssName.caseInsensitiveCompare("MediumOrchid") == .OrderedSame) {
    s = "BA55D3"
    } else if(cssName.caseInsensitiveCompare("MediumPurple") == .OrderedSame) {
    s = "9370DB"
    } else if(cssName.caseInsensitiveCompare("MediumSeaGreen") == .OrderedSame) {
    s = "3CB371"
    } else if(cssName.caseInsensitiveCompare("MediumSlateBlue") == .OrderedSame) {
    s = "7B68EE"
    } else if(cssName.caseInsensitiveCompare("MediumSpringGreen") == .OrderedSame) {
    s = "00FA9A"
    } else if(cssName.caseInsensitiveCompare("MediumTurquoise") == .OrderedSame) {
    s = "48D1CC"
    } else if(cssName.caseInsensitiveCompare("MediumVioletRed") == .OrderedSame) {
    s = "C71585"
    } else if(cssName.caseInsensitiveCompare("MidnightBlue") == .OrderedSame) {
    s = "191970"
    } else if(cssName.caseInsensitiveCompare("MintCream") == .OrderedSame) {
    s = "F5FFFA"
    } else if(cssName.caseInsensitiveCompare("MistyRose") == .OrderedSame) {
    s = "FFE4E1"
    } else if(cssName.caseInsensitiveCompare("Moccasin") == .OrderedSame) {
    s = "FFE4B5"
    } else if(cssName.caseInsensitiveCompare("NavajoWhite") == .OrderedSame) {
    s = "FFDEAD"
    } else if(cssName.caseInsensitiveCompare("Navy") == .OrderedSame) {
    s = "000080"
    } else if(cssName.caseInsensitiveCompare("OldLace") == .OrderedSame) {
    s = "FDF5E6"
    } else if(cssName.caseInsensitiveCompare("Olive") == .OrderedSame) {
    s = "808000"
    } else if(cssName.caseInsensitiveCompare("OliveDrab") == .OrderedSame) {
    s = "6B8E23"
    } else if(cssName.caseInsensitiveCompare("Orange") == .OrderedSame) {
    s = "FFA500"
    } else if(cssName.caseInsensitiveCompare("OrangeRed") == .OrderedSame) {
    s = "FF4500"
    } else if(cssName.caseInsensitiveCompare("Orchid") == .OrderedSame) {
    s = "DA70D6"
    } else if(cssName.caseInsensitiveCompare("PaleGoldenRod") == .OrderedSame) {
    s = "EEE8AA"
    } else if(cssName.caseInsensitiveCompare("PaleGreen") == .OrderedSame) {
    s = "98FB98"
    } else if(cssName.caseInsensitiveCompare("PaleTurquoise") == .OrderedSame) {
    s = "AFEEEE"
    } else if(cssName.caseInsensitiveCompare("PaleVioletRed") == .OrderedSame) {
    s = "DB7093"
    } else if(cssName.caseInsensitiveCompare("PapayaWhip") == .OrderedSame) {
    s = "FFEFD5"
    } else if(cssName.caseInsensitiveCompare("PeachPuff") == .OrderedSame) {
    s = "FFDAB9"
    } else if(cssName.caseInsensitiveCompare("Peru") == .OrderedSame) {
    s = "CD853F"
    } else if(cssName.caseInsensitiveCompare("Pink") == .OrderedSame) {
    s = "FFC0CB"
    } else if(cssName.caseInsensitiveCompare("Plum") == .OrderedSame) {
    s = "DDA0DD"
    } else if(cssName.caseInsensitiveCompare("PowderBlue") == .OrderedSame) {
    s = "B0E0E6"
    } else if(cssName.caseInsensitiveCompare("Purple") == .OrderedSame) {
    s = "800080"
    } else if(cssName.caseInsensitiveCompare("Red") == .OrderedSame) {
    s = "FF0000"
    } else if(cssName.caseInsensitiveCompare("RosyBrown") == .OrderedSame) {
    s = "BC8F8F"
    } else if(cssName.caseInsensitiveCompare("RoyalBlue") == .OrderedSame) {
    s = "4169E1"
    } else if(cssName.caseInsensitiveCompare("SaddleBrown") == .OrderedSame) {
    s = "8B4513"
    } else if(cssName.caseInsensitiveCompare("Salmon") == .OrderedSame) {
    s = "FA8072"
    } else if(cssName.caseInsensitiveCompare("SandyBrown") == .OrderedSame) {
    s = "F4A460"
    } else if(cssName.caseInsensitiveCompare("SeaGreen") == .OrderedSame) {
    s = "2E8B57"
    } else if(cssName.caseInsensitiveCompare("SeaShell") == .OrderedSame) {
    s = "FFF5EE"
    } else if(cssName.caseInsensitiveCompare("Sienna") == .OrderedSame) {
    s = "A0522D"
    } else if(cssName.caseInsensitiveCompare("Silver") == .OrderedSame) {
    s = "C0C0C0"
    } else if(cssName.caseInsensitiveCompare("SkyBlue") == .OrderedSame) {
    s = "87CEEB"
    } else if(cssName.caseInsensitiveCompare("SlateBlue") == .OrderedSame) {
    s = "6A5ACD"
    } else if(cssName.caseInsensitiveCompare("SlateGray") == .OrderedSame) {
    s = "708090"
    } else if(cssName.caseInsensitiveCompare("SlateGrey") == .OrderedSame) {
    s = "708090"
    } else if(cssName.caseInsensitiveCompare("Snow") == .OrderedSame) {
    s = "FFFAFA"
    } else if(cssName.caseInsensitiveCompare("SpringGreen") == .OrderedSame) {
    s = "00FF7F"
    } else if(cssName.caseInsensitiveCompare("SteelBlue") == .OrderedSame) {
    s = "4682B4"
    } else if(cssName.caseInsensitiveCompare("Tan") == .OrderedSame) {
    s = "D2B48C"
    } else if(cssName.caseInsensitiveCompare("Teal") == .OrderedSame) {
    s = "008080"
    } else if(cssName.caseInsensitiveCompare("Thistle") == .OrderedSame) {
    s = "D8BFD8"
    } else if(cssName.caseInsensitiveCompare("Tomato") == .OrderedSame) {
    s = "FF6347"
    } else if(cssName.caseInsensitiveCompare("Turquoise") == .OrderedSame) {
    s = "40E0D0"
    } else if(cssName.caseInsensitiveCompare("Violet") == .OrderedSame) {
    s = "EE82EE"
    } else if(cssName.caseInsensitiveCompare("Wheat") == .OrderedSame) {
    s = "F5DEB3"
    } else if(cssName.caseInsensitiveCompare("White") == .OrderedSame) {
    s = "FFFFFF"
    } else if(cssName.caseInsensitiveCompare("WhiteSmoke") == .OrderedSame) {
    s = "F5F5F5"
    } else if(cssName.caseInsensitiveCompare("Yellow") == .OrderedSame) {
    s = "FFFF00"
    } else if(cssName.caseInsensitiveCompare("YellowGreen") == .OrderedSame) {
    s = "9ACD32"
    let key = cssName.uppercaseString
    let hex = cssToHexDictionairy[key]
    if hex == nil {
    return cssToHexDictionairy["CLEAR"]!
    }
    return s
    return hex!
    }
    }

    private static let cssToHexDictionairy : Dictionary<String, String> = [
    "CLEAR" : "00000000",
    "TRANSPARENT" : "00000000",
    "" : "00000000",
    "ALICEBLUE" : "F0F8FF",
    "ANTIQUEWHITE" : "FAEBD7",
    "AQUA" : "00FFFF",
    "AQUAMARINE" : "7FFFD4",
    "AZURE" : "F0FFFF",
    "BEIGE" : "F5F5DC",
    "BISQUE" : "FFE4C4",
    "BLACK" : "000000",
    "BLANCHEDALMOND" : "FFEBCD",
    "BLUE" : "0000FF",
    "BLUEVIOLET" : "8A2BE2",
    "BROWN" : "A52A2A",
    "BURLYWOOD" : "DEB887",
    "CADETBLUE" : "5F9EA0",
    "CHARTREUSE" : "7FFF00",
    "CHOCOLATE" : "D2691E",
    "CORAL" : "FF7F50",
    "CORNFLOWERBLUE" : "6495ED",
    "CORNSILK" : "FFF8DC",
    "CRIMSON" : "DC143C",
    "CYAN" : "00FFFF",
    "DARKBLUE" : "00008B",
    "DARKCYAN" : "008B8B",
    "DARKGOLDENROD" : "B8860B",
    "DARKGRAY" : "A9A9A9",
    "DARKGREY" : "A9A9A9",
    "DARKGREEN" : "006400",
    "DARKKHAKI" : "BDB76B",
    "DARKMAGENTA" : "8B008B",
    "DARKOLIVEGREEN" : "556B2F",
    "DARKORANGE" : "FF8C00",
    "DARKORCHID" : "9932CC",
    "DARKRED" : "8B0000",
    "DARKSALMON" : "E9967A",
    "DARKSEAGREEN" : "8FBC8F",
    "DARKSLATEBLUE" : "483D8B",
    "DARKSLATEGRAY" : "2F4F4F",
    "DARKSLATEGREY" : "2F4F4F",
    "DARKTURQUOISE" : "00CED1",
    "DARKVIOLET" : "9400D3",
    "DEEPPINK" : "FF1493",
    "DEEPSKYBLUE" : "00BFFF",
    "DIMGRAY" : "696969",
    "DIMGREY" : "696969",
    "DODGERBLUE" : "1E90FF",
    "FIREBRICK" : "B22222",
    "FLORALWHITE" : "FFFAF0",
    "FORESTGREEN" : "228B22",
    "FUCHSIA" : "FF00FF",
    "GAINSBORO" : "DCDCDC",
    "GHOSTWHITE" : "F8F8FF",
    "GOLD" : "FFD700",
    "GOLDENROD" : "DAA520",
    "GRAY" : "808080",
    "GREY" : "808080",
    "GREEN" : "008000",
    "GREENYELLOW" : "ADFF2F",
    "HONEYDEW" : "F0FFF0",
    "HOTPINK" : "FF69B4",
    "INDIANRED" : "CD5C5C",
    "INDIGO" : "4B0082",
    "IVORY" : "FFFFF0",
    "KHAKI" : "F0E68C",
    "LAVENDER" : "E6E6FA",
    "LAVENDERBLUSH" : "FFF0F5",
    "LAWNGREEN" : "7CFC00",
    "LEMONCHIFFON" : "FFFACD",
    "LIGHTBLUE" : "ADD8E6",
    "LIGHTCORAL" : "F08080",
    "LIGHTCYAN" : "E0FFFF",
    "LIGHTGOLDENRODYELLOW" : "FAFAD2",
    "LIGHTGRAY" : "D3D3D3",
    "LIGHTGREY" : "D3D3D3",
    "LIGHTGREEN" : "90EE90",
    "LIGHTPINK" : "FFB6C1",
    "LIGHTSALMON" : "FFA07A",
    "LIGHTSEAGREEN" : "20B2AA",
    "LIGHTSKYBLUE" : "87CEFA",
    "LIGHTSLATEGRAY" : "778899",
    "LIGHTSLATEGREY" : "778899",
    "LIGHTSTEELBLUE" : "B0C4DE",
    "LIGHTYELLOW" : "FFFFE0",
    "LIME" : "00FF00",
    "LIMEGREEN" : "32CD32",
    "LINEN" : "FAF0E6",
    "MAGENTA" : "FF00FF",
    "MAROON" : "800000",
    "MEDIUMAQUAMARINE" : "66CDAA",
    "MEDIUMBLUE" : "0000CD",
    "MEDIUMORCHID" : "BA55D3",
    "MEDIUMPURPLE" : "9370DB",
    "MEDIUMSEAGREEN" : "3CB371",
    "MEDIUMSLATEBLUE" : "7B68EE",
    "MEDIUMSPRINGGREEN" : "00FA9A",
    "MEDIUMTURQUOISE" : "48D1CC",
    "MEDIUMVIOLETRED" : "C71585",
    "MIDNIGHTBLUE" : "191970",
    "MINTCREAM" : "F5FFFA",
    "MISTYROSE" : "FFE4E1",
    "MOCCASIN" : "FFE4B5",
    "NAVAJOWHITE" : "FFDEAD",
    "NAVY" : "000080",
    "OLDLACE" : "FDF5E6",
    "OLIVE" : "808000",
    "OLIVEDRAB" : "6B8E23",
    "ORANGE" : "FFA500",
    "ORANGERED" : "FF4500",
    "ORCHID" : "DA70D6",
    "PALEGOLDENROD" : "EEE8AA",
    "PALEGREEN" : "98FB98",
    "PALETURQUOISE" : "AFEEEE",
    "PALEVIOLETRED" : "DB7093",
    "PAPAYAWHIP" : "FFEFD5",
    "PEACHPUFF" : "FFDAB9",
    "PERU" : "CD853F",
    "PINK" : "FFC0CB",
    "PLUM" : "DDA0DD",
    "POWDERBLUE" : "B0E0E6",
    "PURPLE" : "800080",
    "RED" : "FF0000",
    "ROSYBROWN" : "BC8F8F",
    "ROYALBLUE" : "4169E1",
    "SADDLEBROWN" : "8B4513",
    "SALMON" : "FA8072",
    "SANDYBROWN" : "F4A460",
    "SEAGREEN" : "2E8B57",
    "SEASHELL" : "FFF5EE",
    "SIENNA" : "A0522D",
    "SILVER" : "C0C0C0",
    "SKYBLUE" : "87CEEB",
    "SLATEBLUE" : "6A5ACD",
    "SLATEGRAY" : "708090",
    "SLATEGREY" : "708090",
    "SNOW" : "FFFAFA",
    "SPRINGGREEN" : "00FF7F",
    "STEELBLUE" : "4682B4",
    "TAN" : "D2B48C",
    "TEAL" : "008080",
    "THISTLE" : "D8BFD8",
    "TOMATO" : "FF6347",
    "TURQUOISE" : "40E0D0",
    "VIOLET" : "EE82EE",
    "WHEAT" : "F5DEB3",
    "WHITE" : "FFFFFF",
    "WHITESMOKE" : "F5F5F5",
    "YELLOW" : "FFFF00",
    "YELLOWGREEN" : "9ACD32"
    ]
    }
  16. Norman Basham revised this gist Dec 11, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions Color+HexAndCSSColorNames.swift
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,9 @@ public extension UIColor {

    - **"abc"**
    - **"abc7"**
    - **"#abc7"**
    - **"00FFFF"**
    - **"#00FFFF"**
    - **"00FFFF77"**
    - **"Orange", "Azure", "Tomato"** Modern browsers support 140 color names (<http://www.w3schools.com/cssref/css_colornames.asp>)
    - **"Clear"** [UIColor clearColor]
  17. Norman Basham revised this gist Dec 11, 2015. 1 changed file with 33 additions and 6 deletions.
    39 changes: 33 additions & 6 deletions Color+HexAndCSSColorNames.swift
    Original file line number Diff line number Diff line change
    @@ -35,7 +35,7 @@ public extension UIColor {
    - **"abc7"**
    - **"00FFFF"**
    - **"00FFFF77"**
    - **"Orange", "Azure", "Tomato"** All modern browsers support the 140 color names (@link http://www.w3schools.com/cssref/css_colornames.asp)
    - **"Orange", "Azure", "Tomato"** Modern browsers support 140 color names (<http://www.w3schools.com/cssref/css_colornames.asp>)
    - **"Clear"** [UIColor clearColor]
    - **"Transparent"** [UIColor clearColor]
    - **nil** [UIColor clearColor]
    @@ -45,11 +45,7 @@ public extension UIColor {
    let normalizedHexString: String = UIColor.normalize(hex)
    var c: CUnsignedInt = 0
    NSScanner(string: normalizedHexString).scanHexInt(&c)
    let redInt: CUnsignedInt = (c & 0xff000000) >> 24
    let greenInt: CUnsignedInt = (c & 0xff0000) >> 16
    let blueInt: CUnsignedInt = (c & 0xff00) >> 8
    let alphaInt: CUnsignedInt = c & 0xff
    self.init(red:CGFloat(redInt)/255.0, green:CGFloat(greenInt)/255.0, blue:CGFloat(blueInt)/255.0, alpha:CGFloat(alphaInt)/255.0)
    self.init(red:UIColorMasks.redValue(c), green:UIColorMasks.greenValue(c), blue:UIColorMasks.blueValue(c), alpha:UIColorMasks.alphaValue(c))
    }

    /**
    @@ -80,6 +76,37 @@ public extension UIColor {
    }
    }

    private enum UIColorMasks: CUnsignedInt {
    case RedMask = 0xff000000
    case GreenMask = 0x00ff0000
    case BlueMask = 0x0000ff00
    case AlphaMask = 0x000000ff

    static func redValue(value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & RedMask.rawValue) >> 24
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    }

    static func greenValue(value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & GreenMask.rawValue) >> 16
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    }

    static func blueValue(value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = (value & BlueMask.rawValue) >> 8
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    }

    static func alphaValue(value: CUnsignedInt) -> CGFloat {
    let i: CUnsignedInt = value & AlphaMask.rawValue
    let f: CGFloat = CGFloat(i)/255.0;
    return f;
    }
    }

    private static func normalize(hex: String?) -> String {
    guard var hexString = hex else {
    return "00000000"
  18. Norman Basham revised this gist Dec 9, 2015. No changes.
  19. Norman Basham revised this gist Dec 9, 2015. No changes.
  20. Norman Basham created this gist Dec 9, 2015.
    419 changes: 419 additions & 0 deletions Color+HexAndCSSColorNames.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,419 @@
    //
    // Color+HexAndCSSColorNames.swift
    //
    // Created by Norman Basham on 12/8/15.
    // Copyright © 2015 Black Labs. All rights reserved.
    //
    // Permission is hereby granted, free of charge, to any person obtaining a copy
    // of this software and associated documentation files (the "Software"), to deal
    // in the Software without restriction, including without limitation the rights
    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    // copies of the Software, and to permit persons to whom the Software is
    // furnished to do so, subject to the following conditions:
    //
    // The above copyright notice and this permission notice shall be included in
    // all copies or substantial portions of the Software.
    //
    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    // THE SOFTWARE.

    import UIKit

    public extension UIColor {

    /**
    Creates an immuatble UIColor instance specified by a hex string, CSS color name, or nil.

    - parameter hexString: A case insensitive String? representing a hex or CSS value e.g.

    - **"abc"**
    - **"abc7"**
    - **"00FFFF"**
    - **"00FFFF77"**
    - **"Orange", "Azure", "Tomato"** All modern browsers support the 140 color names (@link http://www.w3schools.com/cssref/css_colornames.asp)
    - **"Clear"** [UIColor clearColor]
    - **"Transparent"** [UIColor clearColor]
    - **nil** [UIColor clearColor]
    - **empty string** [UIColor clearColor]
    */
    public convenience init(hex: String?) {
    let normalizedHexString: String = UIColor.normalize(hex)
    var c: CUnsignedInt = 0
    NSScanner(string: normalizedHexString).scanHexInt(&c)
    let redInt: CUnsignedInt = (c & 0xff000000) >> 24
    let greenInt: CUnsignedInt = (c & 0xff0000) >> 16
    let blueInt: CUnsignedInt = (c & 0xff00) >> 8
    let alphaInt: CUnsignedInt = c & 0xff
    self.init(red:CGFloat(redInt)/255.0, green:CGFloat(greenInt)/255.0, blue:CGFloat(blueInt)/255.0, alpha:CGFloat(alphaInt)/255.0)
    }

    /**
    Returns a hex equivalent of this UIColor.

    - Parameter includeAlpha: Optional parameter to include the alpha hex.

    color.hexDescription() -> "ff0000"

    color.hexDescription(true) -> "ff0000aa"

    - Returns: A new string with `String` with the color's hexidecimal value.
    */
    public func hexDescription(includeAlpha: Bool = false) -> String {
    if CGColorGetNumberOfComponents(self.CGColor) == 4 {
    let components = CGColorGetComponents(self.CGColor)
    let red = Float(components[0]) * 255.0
    let green = Float(components[1]) * 255.0
    let blue = Float(components[2]) * 255.0
    let alpha = Float(components[3]) * 255.0
    if (includeAlpha) {
    return String.init(format: "%02x%02x%02x%02x", Int(red), Int(green), Int(blue), Int(alpha))
    } else {
    return String.init(format: "%02x%02x%02x", Int(red), Int(green), Int(blue))
    }
    } else {
    return "Color not RGB."
    }
    }

    private static func normalize(hex: String?) -> String {
    guard var hexString = hex else {
    return "00000000"
    }
    let hasHash : Bool = hexString.hasPrefix("#")
    if (hasHash) {
    hexString = String(hexString.characters.dropFirst())

    }
    if hexString.characters.count == 3 || hexString.characters.count == 4 {
    let redHex = hexString.substringToIndex(hexString.startIndex.advancedBy(1))
    let greenHex = hexString.substringWithRange(Range<String.Index>(start: hexString.startIndex.advancedBy(1), end: hexString.startIndex.advancedBy(2)))
    let blueHex = hexString.substringWithRange(Range<String.Index>(start: hexString.startIndex.advancedBy(2), end: hexString.startIndex.advancedBy(3)))
    var alphaHex = ""
    if hexString.characters.count == 4 {
    alphaHex = hexString.substringFromIndex(hexString.startIndex.advancedBy(3))
    }

    hexString = redHex + redHex + greenHex + greenHex + blueHex + blueHex + alphaHex + alphaHex
    }
    hexString = UIColor.hexFromCssName(hexString)
    let hasAlpha = hexString.characters.count > 7
    if (!hasAlpha) {
    hexString = hexString + "ff"
    }
    return hexString;
    }

    /**
    All modern browsers support the following 140 color names (see http://www.w3schools.com/cssref/css_colornames.asp)
    */
    private static func hexFromCssName(cssName: String) -> String {
    var s: String = cssName
    if (cssName.caseInsensitiveCompare("Clear") == .OrderedSame) {
    s = "00000000"
    } else if (cssName.caseInsensitiveCompare("Transparent") == .OrderedSame) {
    s = "00000000"
    } else if (cssName.caseInsensitiveCompare("") == .OrderedSame) {
    s = "00000000"
    } else if(cssName.caseInsensitiveCompare("AliceBlue") == .OrderedSame) {
    s = "F0F8FF"
    } else if(cssName.caseInsensitiveCompare("AntiqueWhite") == .OrderedSame) {
    s = "FAEBD7"
    } else if(cssName.caseInsensitiveCompare("Aqua") == .OrderedSame) {
    s = "00FFFF"
    } else if(cssName.caseInsensitiveCompare("Aquamarine") == .OrderedSame) {
    s = "7FFFD4"
    } else if(cssName.caseInsensitiveCompare("Azure") == .OrderedSame) {
    s = "F0FFFF"
    } else if(cssName.caseInsensitiveCompare("Beige") == .OrderedSame) {
    s = "F5F5DC"
    } else if(cssName.caseInsensitiveCompare("Bisque") == .OrderedSame) {
    s = "FFE4C4"
    } else if(cssName.caseInsensitiveCompare("Black") == .OrderedSame) {
    s = "000000"
    } else if(cssName.caseInsensitiveCompare("BlanchedAlmond") == .OrderedSame) {
    s = "FFEBCD"
    } else if(cssName.caseInsensitiveCompare("Blue") == .OrderedSame) {
    s = "0000FF"
    } else if(cssName.caseInsensitiveCompare("BlueViolet") == .OrderedSame) {
    s = "8A2BE2"
    } else if(cssName.caseInsensitiveCompare("Brown") == .OrderedSame) {
    s = "A52A2A"
    } else if(cssName.caseInsensitiveCompare("BurlyWood") == .OrderedSame) {
    s = "DEB887"
    } else if(cssName.caseInsensitiveCompare("CadetBlue") == .OrderedSame) {
    s = "5F9EA0"
    } else if(cssName.caseInsensitiveCompare("Chartreuse") == .OrderedSame) {
    s = "7FFF00"
    } else if(cssName.caseInsensitiveCompare("Chocolate") == .OrderedSame) {
    s = "D2691E"
    } else if(cssName.caseInsensitiveCompare("Coral") == .OrderedSame) {
    s = "FF7F50"
    } else if(cssName.caseInsensitiveCompare("CornflowerBlue") == .OrderedSame) {
    s = "6495ED"
    } else if(cssName.caseInsensitiveCompare("Cornsilk") == .OrderedSame) {
    s = "FFF8DC"
    } else if(cssName.caseInsensitiveCompare("Crimson") == .OrderedSame) {
    s = "DC143C"
    } else if(cssName.caseInsensitiveCompare("Cyan") == .OrderedSame) {
    s = "00FFFF"
    } else if(cssName.caseInsensitiveCompare("DarkBlue") == .OrderedSame) {
    s = "00008B"
    } else if(cssName.caseInsensitiveCompare("DarkCyan") == .OrderedSame) {
    s = "008B8B"
    } else if(cssName.caseInsensitiveCompare("DarkGoldenRod") == .OrderedSame) {
    s = "B8860B"
    } else if(cssName.caseInsensitiveCompare("DarkGray") == .OrderedSame) {
    s = "A9A9A9"
    } else if(cssName.caseInsensitiveCompare("DarkGrey") == .OrderedSame) {
    s = "A9A9A9"
    } else if(cssName.caseInsensitiveCompare("DarkGreen") == .OrderedSame) {
    s = "006400"
    } else if(cssName.caseInsensitiveCompare("DarkKhaki") == .OrderedSame) {
    s = "BDB76B"
    } else if(cssName.caseInsensitiveCompare("DarkMagenta") == .OrderedSame) {
    s = "8B008B"
    } else if(cssName.caseInsensitiveCompare("DarkOliveGreen") == .OrderedSame) {
    s = "556B2F"
    } else if(cssName.caseInsensitiveCompare("DarkOrange") == .OrderedSame) {
    s = "FF8C00"
    } else if(cssName.caseInsensitiveCompare("DarkOrchid") == .OrderedSame) {
    s = "9932CC"
    } else if(cssName.caseInsensitiveCompare("DarkRed") == .OrderedSame) {
    s = "8B0000"
    } else if(cssName.caseInsensitiveCompare("DarkSalmon") == .OrderedSame) {
    s = "E9967A"
    } else if(cssName.caseInsensitiveCompare("DarkSeaGreen") == .OrderedSame) {
    s = "8FBC8F"
    } else if(cssName.caseInsensitiveCompare("DarkSlateBlue") == .OrderedSame) {
    s = "483D8B"
    } else if(cssName.caseInsensitiveCompare("DarkSlateGray") == .OrderedSame) {
    s = "2F4F4F"
    } else if(cssName.caseInsensitiveCompare("DarkSlateGrey") == .OrderedSame) {
    s = "2F4F4F"
    } else if(cssName.caseInsensitiveCompare("DarkTurquoise") == .OrderedSame) {
    s = "00CED1"
    } else if(cssName.caseInsensitiveCompare("DarkViolet") == .OrderedSame) {
    s = "9400D3"
    } else if(cssName.caseInsensitiveCompare("DeepPink") == .OrderedSame) {
    s = "FF1493"
    } else if(cssName.caseInsensitiveCompare("DeepSkyBlue") == .OrderedSame) {
    s = "00BFFF"
    } else if(cssName.caseInsensitiveCompare("DimGray") == .OrderedSame) {
    s = "696969"
    } else if(cssName.caseInsensitiveCompare("DimGrey") == .OrderedSame) {
    s = "696969"
    } else if(cssName.caseInsensitiveCompare("DodgerBlue") == .OrderedSame) {
    s = "1E90FF"
    } else if(cssName.caseInsensitiveCompare("FireBrick") == .OrderedSame) {
    s = "B22222"
    } else if(cssName.caseInsensitiveCompare("FloralWhite") == .OrderedSame) {
    s = "FFFAF0"
    } else if(cssName.caseInsensitiveCompare("ForestGreen") == .OrderedSame) {
    s = "228B22"
    } else if(cssName.caseInsensitiveCompare("Fuchsia") == .OrderedSame) {
    s = "FF00FF"
    } else if(cssName.caseInsensitiveCompare("Gainsboro") == .OrderedSame) {
    s = "DCDCDC"
    } else if(cssName.caseInsensitiveCompare("GhostWhite") == .OrderedSame) {
    s = "F8F8FF"
    } else if(cssName.caseInsensitiveCompare("Gold") == .OrderedSame) {
    s = "FFD700"
    } else if(cssName.caseInsensitiveCompare("GoldenRod") == .OrderedSame) {
    s = "DAA520"
    } else if(cssName.caseInsensitiveCompare("Gray") == .OrderedSame) {
    s = "808080"
    } else if(cssName.caseInsensitiveCompare("Grey") == .OrderedSame) {
    s = "808080"
    } else if(cssName.caseInsensitiveCompare("Green") == .OrderedSame) {
    s = "008000"
    } else if(cssName.caseInsensitiveCompare("GreenYellow") == .OrderedSame) {
    s = "ADFF2F"
    } else if(cssName.caseInsensitiveCompare("HoneyDew") == .OrderedSame) {
    s = "F0FFF0"
    } else if(cssName.caseInsensitiveCompare("HotPink") == .OrderedSame) {
    s = "FF69B4"
    } else if(cssName.caseInsensitiveCompare("IndianRed") == .OrderedSame) {
    s = "CD5C5C"
    } else if(cssName.caseInsensitiveCompare("Indigo") == .OrderedSame) {
    s = "4B0082"
    } else if(cssName.caseInsensitiveCompare("Ivory") == .OrderedSame) {
    s = "FFFFF0"
    } else if(cssName.caseInsensitiveCompare("Khaki") == .OrderedSame) {
    s = "F0E68C"
    } else if(cssName.caseInsensitiveCompare("Lavender") == .OrderedSame) {
    s = "E6E6FA"
    } else if(cssName.caseInsensitiveCompare("LavenderBlush") == .OrderedSame) {
    s = "FFF0F5"
    } else if(cssName.caseInsensitiveCompare("LawnGreen") == .OrderedSame) {
    s = "7CFC00"
    } else if(cssName.caseInsensitiveCompare("LemonChiffon") == .OrderedSame) {
    s = "FFFACD"
    } else if(cssName.caseInsensitiveCompare("LightBlue") == .OrderedSame) {
    s = "ADD8E6"
    } else if(cssName.caseInsensitiveCompare("LightCoral") == .OrderedSame) {
    s = "F08080"
    } else if(cssName.caseInsensitiveCompare("LightCyan") == .OrderedSame) {
    s = "E0FFFF"
    } else if(cssName.caseInsensitiveCompare("LightGoldenRodYellow") == .OrderedSame) {
    s = "FAFAD2"
    } else if(cssName.caseInsensitiveCompare("LightGray") == .OrderedSame) {
    s = "D3D3D3"
    } else if(cssName.caseInsensitiveCompare("LightGrey") == .OrderedSame) {
    s = "D3D3D3"
    } else if(cssName.caseInsensitiveCompare("LightGreen") == .OrderedSame) {
    s = "90EE90"
    } else if(cssName.caseInsensitiveCompare("LightPink") == .OrderedSame) {
    s = "FFB6C1"
    } else if(cssName.caseInsensitiveCompare("LightSalmon") == .OrderedSame) {
    s = "FFA07A"
    } else if(cssName.caseInsensitiveCompare("LightSeaGreen") == .OrderedSame) {
    s = "20B2AA"
    } else if(cssName.caseInsensitiveCompare("LightSkyBlue") == .OrderedSame) {
    s = "87CEFA"
    } else if(cssName.caseInsensitiveCompare("LightSlateGray") == .OrderedSame) {
    s = "778899"
    } else if(cssName.caseInsensitiveCompare("LightSlateGrey") == .OrderedSame) {
    s = "778899"
    } else if(cssName.caseInsensitiveCompare("LightSteelBlue") == .OrderedSame) {
    s = "B0C4DE"
    } else if(cssName.caseInsensitiveCompare("LightYellow") == .OrderedSame) {
    s = "FFFFE0"
    } else if(cssName.caseInsensitiveCompare("Lime") == .OrderedSame) {
    s = "00FF00"
    } else if(cssName.caseInsensitiveCompare("LimeGreen") == .OrderedSame) {
    s = "32CD32"
    } else if(cssName.caseInsensitiveCompare("Linen") == .OrderedSame) {
    s = "FAF0E6"
    } else if(cssName.caseInsensitiveCompare("Magenta") == .OrderedSame) {
    s = "FF00FF"
    } else if(cssName.caseInsensitiveCompare("Maroon") == .OrderedSame) {
    s = "800000"
    } else if(cssName.caseInsensitiveCompare("MediumAquaMarine") == .OrderedSame) {
    s = "66CDAA"
    } else if(cssName.caseInsensitiveCompare("MediumBlue") == .OrderedSame) {
    s = "0000CD"
    } else if(cssName.caseInsensitiveCompare("MediumOrchid") == .OrderedSame) {
    s = "BA55D3"
    } else if(cssName.caseInsensitiveCompare("MediumPurple") == .OrderedSame) {
    s = "9370DB"
    } else if(cssName.caseInsensitiveCompare("MediumSeaGreen") == .OrderedSame) {
    s = "3CB371"
    } else if(cssName.caseInsensitiveCompare("MediumSlateBlue") == .OrderedSame) {
    s = "7B68EE"
    } else if(cssName.caseInsensitiveCompare("MediumSpringGreen") == .OrderedSame) {
    s = "00FA9A"
    } else if(cssName.caseInsensitiveCompare("MediumTurquoise") == .OrderedSame) {
    s = "48D1CC"
    } else if(cssName.caseInsensitiveCompare("MediumVioletRed") == .OrderedSame) {
    s = "C71585"
    } else if(cssName.caseInsensitiveCompare("MidnightBlue") == .OrderedSame) {
    s = "191970"
    } else if(cssName.caseInsensitiveCompare("MintCream") == .OrderedSame) {
    s = "F5FFFA"
    } else if(cssName.caseInsensitiveCompare("MistyRose") == .OrderedSame) {
    s = "FFE4E1"
    } else if(cssName.caseInsensitiveCompare("Moccasin") == .OrderedSame) {
    s = "FFE4B5"
    } else if(cssName.caseInsensitiveCompare("NavajoWhite") == .OrderedSame) {
    s = "FFDEAD"
    } else if(cssName.caseInsensitiveCompare("Navy") == .OrderedSame) {
    s = "000080"
    } else if(cssName.caseInsensitiveCompare("OldLace") == .OrderedSame) {
    s = "FDF5E6"
    } else if(cssName.caseInsensitiveCompare("Olive") == .OrderedSame) {
    s = "808000"
    } else if(cssName.caseInsensitiveCompare("OliveDrab") == .OrderedSame) {
    s = "6B8E23"
    } else if(cssName.caseInsensitiveCompare("Orange") == .OrderedSame) {
    s = "FFA500"
    } else if(cssName.caseInsensitiveCompare("OrangeRed") == .OrderedSame) {
    s = "FF4500"
    } else if(cssName.caseInsensitiveCompare("Orchid") == .OrderedSame) {
    s = "DA70D6"
    } else if(cssName.caseInsensitiveCompare("PaleGoldenRod") == .OrderedSame) {
    s = "EEE8AA"
    } else if(cssName.caseInsensitiveCompare("PaleGreen") == .OrderedSame) {
    s = "98FB98"
    } else if(cssName.caseInsensitiveCompare("PaleTurquoise") == .OrderedSame) {
    s = "AFEEEE"
    } else if(cssName.caseInsensitiveCompare("PaleVioletRed") == .OrderedSame) {
    s = "DB7093"
    } else if(cssName.caseInsensitiveCompare("PapayaWhip") == .OrderedSame) {
    s = "FFEFD5"
    } else if(cssName.caseInsensitiveCompare("PeachPuff") == .OrderedSame) {
    s = "FFDAB9"
    } else if(cssName.caseInsensitiveCompare("Peru") == .OrderedSame) {
    s = "CD853F"
    } else if(cssName.caseInsensitiveCompare("Pink") == .OrderedSame) {
    s = "FFC0CB"
    } else if(cssName.caseInsensitiveCompare("Plum") == .OrderedSame) {
    s = "DDA0DD"
    } else if(cssName.caseInsensitiveCompare("PowderBlue") == .OrderedSame) {
    s = "B0E0E6"
    } else if(cssName.caseInsensitiveCompare("Purple") == .OrderedSame) {
    s = "800080"
    } else if(cssName.caseInsensitiveCompare("Red") == .OrderedSame) {
    s = "FF0000"
    } else if(cssName.caseInsensitiveCompare("RosyBrown") == .OrderedSame) {
    s = "BC8F8F"
    } else if(cssName.caseInsensitiveCompare("RoyalBlue") == .OrderedSame) {
    s = "4169E1"
    } else if(cssName.caseInsensitiveCompare("SaddleBrown") == .OrderedSame) {
    s = "8B4513"
    } else if(cssName.caseInsensitiveCompare("Salmon") == .OrderedSame) {
    s = "FA8072"
    } else if(cssName.caseInsensitiveCompare("SandyBrown") == .OrderedSame) {
    s = "F4A460"
    } else if(cssName.caseInsensitiveCompare("SeaGreen") == .OrderedSame) {
    s = "2E8B57"
    } else if(cssName.caseInsensitiveCompare("SeaShell") == .OrderedSame) {
    s = "FFF5EE"
    } else if(cssName.caseInsensitiveCompare("Sienna") == .OrderedSame) {
    s = "A0522D"
    } else if(cssName.caseInsensitiveCompare("Silver") == .OrderedSame) {
    s = "C0C0C0"
    } else if(cssName.caseInsensitiveCompare("SkyBlue") == .OrderedSame) {
    s = "87CEEB"
    } else if(cssName.caseInsensitiveCompare("SlateBlue") == .OrderedSame) {
    s = "6A5ACD"
    } else if(cssName.caseInsensitiveCompare("SlateGray") == .OrderedSame) {
    s = "708090"
    } else if(cssName.caseInsensitiveCompare("SlateGrey") == .OrderedSame) {
    s = "708090"
    } else if(cssName.caseInsensitiveCompare("Snow") == .OrderedSame) {
    s = "FFFAFA"
    } else if(cssName.caseInsensitiveCompare("SpringGreen") == .OrderedSame) {
    s = "00FF7F"
    } else if(cssName.caseInsensitiveCompare("SteelBlue") == .OrderedSame) {
    s = "4682B4"
    } else if(cssName.caseInsensitiveCompare("Tan") == .OrderedSame) {
    s = "D2B48C"
    } else if(cssName.caseInsensitiveCompare("Teal") == .OrderedSame) {
    s = "008080"
    } else if(cssName.caseInsensitiveCompare("Thistle") == .OrderedSame) {
    s = "D8BFD8"
    } else if(cssName.caseInsensitiveCompare("Tomato") == .OrderedSame) {
    s = "FF6347"
    } else if(cssName.caseInsensitiveCompare("Turquoise") == .OrderedSame) {
    s = "40E0D0"
    } else if(cssName.caseInsensitiveCompare("Violet") == .OrderedSame) {
    s = "EE82EE"
    } else if(cssName.caseInsensitiveCompare("Wheat") == .OrderedSame) {
    s = "F5DEB3"
    } else if(cssName.caseInsensitiveCompare("White") == .OrderedSame) {
    s = "FFFFFF"
    } else if(cssName.caseInsensitiveCompare("WhiteSmoke") == .OrderedSame) {
    s = "F5F5F5"
    } else if(cssName.caseInsensitiveCompare("Yellow") == .OrderedSame) {
    s = "FFFF00"
    } else if(cssName.caseInsensitiveCompare("YellowGreen") == .OrderedSame) {
    s = "9ACD32"
    }
    return s
    }
    }