Skip to content

Commit

Permalink
Now uses initializer. Added initializers for hex values.
Browse files Browse the repository at this point in the history
  • Loading branch information
NorthernRealities committed Apr 7, 2015
1 parent 5c8b87a commit 13f6fda
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 13 deletions.
15 changes: 12 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,22 @@ If you can't find a colour in the library and you want to use the new color inst

to the following

let backgroundColour = UIColor.createColorWith ( red: 94, green: 7, blue: 95, alpha: 1.0 )
let backgroundColour = UIColor ( redValue: 94, greenValue: 7, blueValue: 95, alphaValue: 1.0 )

Note that because the alpha value in this case was 1.0 it could have been left off because the function has it set to that value by default.
Note that because the alpha value in this case was 1.0 it could have been left off because the function has it set to that value by default. There has been a change from the initial release of the library. This function used to be called createColorWith but is now a convenience initializer. I hope that this change doesn't cause you too much trouble.

There are also functions that allow you to create UIColor objects using a hexidecimal integer or string.

let backgroundColour = UIColor ( hex: 0x42ac58, alpha: 0.75 )
let foregroundColour = UIColor ( hexString: "#0f23ec" )

The setting of the alpha is available in both but if you leave it out then it defaults to 1.0. (Please note that I have no idea what those will produce. I just made them up as an example.)

## Future

In the short term I hope to release an app that will let you view the colours and perform searching. In the longer term I want to add more colours to the library, either individually or through collections. Ideally it would be great to get one or more of the paint manufacturers to give their permission to use their their palette. I know that Pantone would achive the same thing but it would cost money to get the information and they would probably be opposed to it being publicly posted. All information for the colours was obtained from Wikipedia. I cleaned up any broken links or incorrect information that I came across while working on this project. If you contribute a colour to the library please also let me know if I can add it to the list of colours on Wikipedia.
In the short term I hope to release an app that will let you view the colours and perform searching. I am continuing work on the app. If anyone knows of a good library that lets you choose a colour from a colour wheel or something similar I would appreciate hearing about it. Also I'm looking for a tab bar icon of a palette. The only ones I've found so far are in paid collections and I'm trying to minimize expenses.

In the longer term I want to add more colours to the library, either individually or through collections. Ideally it would be great to get one or more of the paint manufacturers to give their permission to use their their palette. I know that Pantone would achive the same thing but it would cost money to get the information and they would probably be opposed to it being publicly posted. All information for the colours was obtained from Wikipedia. I cleaned up any broken links or incorrect information that I came across while working on this project. If you contribute a colour to the library please also let me know if I can add it to the list of colours on Wikipedia.

In the meantime you can view the colours at Wikipedia where I got the data for all of the colours at the pages.

Expand Down
101 changes: 91 additions & 10 deletions UIColor+Creater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@
// Created by Reid Gravelle on 2015-03-18.
// Copyright (c) 2015 Northern Realities Inc. 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 NON-INFRINGEMENT. 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

Expand All @@ -13,19 +31,82 @@ extension UIColor {
/**
Returns a color object representing the color with the given RGB component values and has the specified opacity.

:param: red The red component of the color object, specified as a value between 0 and 255.
:param: green The green component of the color object, specified as a value between 0 and 255.
:param: blue The blue component of the color object, specified as a value between 0 and 255.
:param: alpha A CGFloat between 0.0 and 1.0 representing the opacity with a default value of 1.0.
:param: redValue The red component of the color object, specified as a value between 0 and 255.
:param: greenValue The green component of the color object, specified as a value between 0 and 255.
:param: blueValue The blue component of the color object, specified as a value between 0 and 255.
:param: alphaValue A CGFloat between 0.0 and 1.0 representing the opacity with a default value of 1.0.

:returns: The UIColor object
*/

class func createColorWith ( #red: Int, green: Int, blue: Int, alpha: CGFloat = 1.0 ) -> UIColor {
return UIColor (
red: CGFloat ( red ) / 255.0,
green: CGFloat ( green ) / 255.0,
blue: CGFloat ( blue ) / 255.0,
alpha: alpha )
convenience init ( redValue: Int, greenValue: Int, blueValue: Int, alphaValue: CGFloat = 1.0 ) {

let redFloat = CGFloat ( redValue ) / 255.0
let greenFloat = CGFloat ( greenValue ) / 255.0
let blueFloat = CGFloat ( blueValue ) / 255.0

self.init ( red: redFloat, green: greenFloat, blue: blueFloat, alpha: alphaValue )
}


/**
Returns a color object representing the color with the given RGB value passed in as a hexidecimal integer and has the specified opacity.

:param: hex The red, green, and blue components that compromise the color combined into a single hexidecimal number. Each component has two digits which range from 0 through to f.
:param: alphaValue A CGFloat between 0.0 and 1.0 representing the opacity with a default value of 1.0.

:returns: The UIColor object
*/

convenience init ( hex : Int, alpha : CGFloat = 1.0 ) {

let red = ( hex >> 16 ) & 0xff
let green = ( hex >> 08 ) & 0xff
let blue = hex & 0xff

self.init ( redValue: red, greenValue: green, blueValue: blue, alphaValue: alpha )
}


/**
Returns a color object representing the color with the given RGB value passed in as a hexidecimal integer and has the specified opacity.

:param: hex The red, green, and blue components that compromise the color combined into a single hexidecimal string. Each component has two characters which range from 0 through to f. The string may be optionally prefixed with a '#' sign.
:param: alphaValue A CGFloat between 0.0 and 1.0 representing the opacity with a default value of 1.0.

:returns: The UIColor object
*/

convenience init ( hexString : String, alpha : CGFloat = 1.0 ) {

var error : NSError?

var hexIntValue : UInt32 = 0x000000

let stringSize = countElements( hexString )


if ( ( stringSize == 6 ) || ( stringSize == 7 ) ) {

let range = NSMakeRange( 0, stringSize )
let pattern = "#?[0-9A-F]{6}"

if let regex = NSRegularExpression ( pattern: pattern, options: .CaseInsensitive, error: &error ) {
let matchRange = regex.rangeOfFirstMatchInString( hexString, options: .ReportProgress, range: range )

if matchRange.location != NSNotFound {
var workingString = hexString

if ( stringSize == 7 ) {
workingString = workingString.substringFromIndex( advance( workingString.startIndex, 1 ) )
}

NSScanner ( string: workingString ).scanHexInt ( &hexIntValue )
}
}
}


self.init ( hex: Int( hexIntValue ), alpha: alpha )
}
}

0 comments on commit 13f6fda

Please sign in to comment.