---
title: "#255: Creating a ColorConvertible Protocol 🔩🎨"
url: https://littlebitesofcocoa.com/255-creating-a-colorconvertible-protocol
author: Jake Marsh
date: 2016-08-29
published: 2016-08-29
topics: ["Swift","Techniques"]
description: "Swift Protocols are great. They allow us to express the idea of expecting a set of functionality, without needing to expect a specific concrete type."
---

# #255: Creating a ColorConvertible Protocol 🔩🎨

By Jake Marsh, August 29, 2016. HTML: https://littlebitesofcocoa.com/255-creating-a-colorconvertible-protocol

_Swift Protocols_ are great. They allow us to express the idea of <i>expecting a set of functionality</i>, without needing to expect a specific concrete **type**. Today we'll look at creating a _Protocol_ to make working with **colors** in our **apps** a bit more flexible. Let's dive in. 🏊

We'll start by creating a new _Protocol_, giving it a conventional name, and require just one read-only **property** from our conforming **types**. This property will always return a `UIColor`.

``` swift
public protocol ColorConvertible {
  var colorValue: UIColor { get }
}
```

Now, let's try this new _Protocol_ out by creating an `enum` that represents the different **colors** we use in our **app**:

``` swift
public enum SpaceshipsColors : String {
  case white = "FFFFFF"
  case red = "E2574C"
  case gold = "EFC75E"
  case darkTeal = "314D5B"
  case lightTeal = "3CB39E"
  case spaceGray = "233640"
  case aluminum = "6A838D"
  case black = "000000"
}
```

Looking pretty good, now let's extend our **enum** to conform to `ColorConvertible`.

We're using [Hue](https://github.com/hyperoslo/Hue) here ([Bite #195](https://littlebitesofcocoa.com/195)) to convert our hex color code `Strings` into `UIColor`s.

``` swift
extension SpaceshipsColors : ColorConvertible {
  public var colorValue: UIColor {
    return UIColor.hex(rawValue)
  }
}
```

<p class='break'></p>

Now we can reference **colors** in our code by name, and get full autocompletion when typing them. Using our new _Protocol_ is super simple, let's make a quick `UIView` **subclass** to show it off:

``` swift
class SpaceshipNameHeaderView : UIView {
  var nameLabel: UILabel
  var nameTextColor: ColorConvertible? {
    didSet {
      nameLabel.textColor = nameTextColor?.colorValue
    }
  }
}
```

It'd be nice if we could still supply "one-off" `UIColor`s as well. No problem, let's simply conform `UIColor` <i>itself</i> to be `ColorConvertible`. We can just return self:

``` swift
extension UIColor : ColorConvertible {
  public var colorValue: UIColor {
    return self
  }
}
```

Finally, we can use it like this:

``` swift
let hv = SpaceshipNameHeaderView()

// works!
hv.nameTextColor = UIColor.black

// works just as well!
hv.nameTextColor = SpaceshipsColors.aluminum
```

_Swift Protocols_ can help us write code that is both expressive and quite flexible. They can take a while to get a handle on, but understanding them is a key step towards unlocking the full power of **Swift**.
