NSColor extension adding ability to handle HSL color space and CSS3 style color codes
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
ColorCode.xcodeproj Update Swift code to 4.2 Aug 14, 2018
Sources/ColorCode Update Swift code to 4.2 Aug 14, 2018
Tests/ColorCodeTests Add black test Aug 14, 2018
.gitignore Update .gitignore Mar 25, 2017
.swift-version Update project for Xcode 9.3 Mar 30, 2018
.swiftlint.yml Force unwrap where cannot be nil Apr 23, 2018
CHANGELOG.md Update Swift code to 4.2 Aug 14, 2018
LICENSE Update project for Xcode 9.3 Mar 30, 2018
Package.swift Implement SPM Nov 15, 2017
README.md Delete case .invalid from ColorCodeType Apr 23, 2018
WFColorCode.podspec Update podspec Sep 14, 2018



Build Status codecov.io Carthage compatible SPM compatible CocoaPods version CocoaPods platform

WFColorCode is a NSColor extension that allows creating NSColor instance from a CSS color code string, or color code string from a NSColor instance. It also adds the ability to handle HSL color space.

  • Requirements: OS X 10.9 or later
  • ARC: ARC enabled


WFColorCode supports the following color code styles.

/// color code type
enum ColorCodeType: Int {
    case hex        // #ffffff
    case shortHex   // #fff
    case cssRGB     // rgb(255,255,255)
    case cssRGBa    // rgba(255,255,255,1)
    case cssHSL     // hsl(0,0%,100%)
    case cssHSLa    // hsla(0,0%,100%,1)
    case cssKeyword // White


Import ColorCode to use.

import ColorCode

// create NSColor instance from HSLa color code
var type: ColorCodeType?
let whiteColor = NSColor(colorCode: "hsla(0,0%,100%,0.5)", type: &type)
let hex: String = whiteColor.colorCode(type: .hex)  // => "#ffffff"

// create NSColor instance from HSLa values
let color = NSColor(deviceHue:0.1, saturation:0.2, lightness:0.3, alpha:1.0)

// create NSColor instance from a CSS3 keyword
let ivoryColor = NSColor(colorCode: "ivory")

// get HSL values from NSColor instance
var hue: CGFloat = 0
var saturation: CGFloat = 0
var lightness: CGFloat = 0
var alpha: CGFloat = 0
color.getHue(hue: &hue, saturation: &saturation, lightness: &lightness, alpha: &alpha)


Framework via Carthage

WFColorCode is Carthage compatible. Add the following line to your Cartfile.

github "1024jp/WFColorCode" ~> 2.0.0

Framework via Cocoapods

WFColorCode is also available via CocoaPods. You can easily install it adding the following line to your Podfile:

pod "WFColorCode"

Swift Package Manager

WFColorCode is also Swift Package Manager compatible.

Source files

If you use neither CocoaPods nor Carthage, place NSColor+ColorCode.swift and NSColor+HSL.swift in Classes/ directory somewhere in your project.


© 2014-2018 1024jp.

The source code is distributed under the terms of the MIT License. See the bundled "LICENSE" for details.