Pixels

Colorful programming


Keywords
alpha-component, brand-colors, color, rgb, rgba, uicolor
License
MIT
Install
pod try Pixels

Documentation

Pixels

Pixels

The purpose of Pixels library is to simplify common tasks with colors in iOS, which makes implementation of colorful UI designs easier. Absolutely compatible with native iOS SDK.

How To Get Started

  • Copy content of Source folder to your project.

or

  • Use Pixels cocoapod

Requirements

  • iOS 9 and later
  • Xcode 9 and later
  • Swift 4

Usage

Color Initialization

The common expression for color in RGB system is #123456. Another variety of this expression looks like #123 which is equivalent to #112233. The same time, iOS SDK doesn't support those expressions above and requires to use CGFloat values for red, green, blue and alpha components in constructor of UIColor class. Not comfortable enough, right?

Pixels has a solution for the problem described above, so instead of this

let grayColor = UIColor(
    red: 51.0 / 255.0,
    green: 51.0 / 255.0,
    blue: 51.0,
    alpha: 255.0
)

you can simply write this

let grayColor = UIColor(hexString: "#333333") // RGB {51, 51, 51}

or this

let grayColor = UIColor(hexString: "#333") // RGB {51, 51, 51}

or even this

let grayColor = UIColor(hexString: "333") // RGB {51, 51, 51}

As you can see in last example, sharp symbol is optional. Hex string will be parsed correctly with or without sharp prefix.

Pixels supports both RGB and RGBA color systems, so you can also add alpha component to expression:

let grayColorWithSmallTransparency = UIColor(hexString: "#333333dd") // RGBA {51, 51, 51, 221}
let halfTransparentGrayColor = UIColor(hexString: "#3338") // RGBA {51, 51, 51, 136}
let almostTransparentGrayColor = UIColor(hexString: "#33333310") // RGBA {51, 51, 51, 16}

If hex string has wrong format, nil will be returned instead of UIColor instance.

Operations with Color

With Pixels you can invert any color:

let invertedColor = UIColor.white.pxls
    .invertedColor(invertAlpha: false)
    .color // returns black color

Also, it's possible to mix two colors:

let mixedColor = UIColor.blue.pxls
    .mix(with: UIColor.orange)
    .color // returns purple color

All operations support chains, so you can use them like in example below:

let resultColor = UIColor.yellow.pxls
    .invertedColor(invertAlpha: false) // invert yellow color
    .mix(with: .orange)                // mix inverted color with orange
    .color                             // get `UIColor` instance
    .withAlphaComponent(0.5)           // make color 50% transparent

Every chain begins with .pxls and finishes by mentioning .color reference that generates final UIColor instance.

Brand Colors

Sometimes you don't know which colors are good enough for your project. But you don't need to be a professional designer, because you can reuse popular color schemes used by well-known services. Pixels provides great collection of brand colors. All that you need is to write:

let color = UIColor.Brands.Google.blue

or

let color = UIColor.Brands.Flickr.pink

Of course, you are not limited to make any operations with those colors:

let flickrPinkWithHalfTransparency = UIColor.Brands.Flickr.pink.colorWithAlphaComponent(0.5)

To see full collection of available brands, just type in Xcode UIColor.Brands. and you will see output like this:

Available Brands

Another way to check available brands is to take a look at the source code.

HTML Colors

Pixels provides a list of HTML colors. All of them are available in UIColor.HTML namespace:

let purple = UIColor.HTML.purple         // #800080
let orange = UIColor.HTML.orange         // #FFA500
let lightGreen = UIColor.HTML.lightGreen // #90EE90

Google Material Colors

Pixels also includes set of material colors that can be used like in example below:

let orange = UIColor.Material.Orange._500
let blue = UIColor.Material.Blue.a200
// etc.

License

Pixels is available under the MIT license. See the LICENSE file for more info.