Giter Site home page Giter Site logo

swifthexcolors's Introduction

SwiftHEXColors

Build Status Swift Package Manager Compatible CocoaPods Compatible Docs Carthage Compatible Platform License

HEX color handling as an extension for UIColor. Written in Swift.

Examples

iOS

// With hash
let color: UIColor = UIColor(hexString: "#ff8942")

// Without hash, with alpha
let secondColor: UIColor = UIColor(hexString: "ff8942", alpha: 0.5)

// Short handling
let shortColorWithHex: UIColor = UIColor(hexString: "fff")

For those who don't want to type the double quotation, you can init a color from a real hex value (an Int)

// With hash
let color: UIColor = UIColor(hex: 0xff8942)

// Without hash, with alpha
let secondColor: UIColor = UIColor(hex: 0xff8942, alpha: 0.5)

OSX

// With hash
let color: NSColor = NSColor(hexString: "#ff8942")

// Without hash, with alpha
let secondColor: NSColor = NSColor(hexString: "ff8942", alpha: 0.5)

// Short handling
let shortColorWithHex: NSColor = NSColor(hexString: "fff")

// From a real hex value (an `Int`)
// With hash
let color: NSColor = NSColor(hex: 0xff8942)

// Without hash, with alpha
let secondColor: NSColor = NSColor(hex: 0xff8942, alpha: 0.5)

Installation

Swift Package Manager

Add this as a dependency in your Package.swift:

import PackageDescription

let package = Package(
    name: "MyPackage",
        dependencies: [
        // Other dependencies
	.package(url: "https://github.com/thii/SwiftHEXColors.git", from: "1.3.1")
    ]
)

CocoaPods

To integrate SwiftHEXColors into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'SwiftHEXColors'

Then, run the following command:

$ pod install

And add import SwiftHEXColors to the top of the files using SwiftHEXColors.

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate SwiftHEXColors into your Xcode project using Carthage, specify it in your Cartfile:

github "thii/SwiftHEXColors"

Run carthage update to build the framework and drag the built SwiftHEXColors.framework into your Xcode project.

Manually

  • Drag and drop SwiftHEXColors.swift file into your project

Requirements

  • Swift 3
  • iOS 8.0 or above.

License

MIT

swifthexcolors's People

Contributors

andrewsb avatar aporat avatar cedrick84 avatar danipralea avatar dependabot[bot] avatar ekimia avatar evgeniyd avatar honcheng avatar inket avatar jmper1 avatar joubin avatar landtanin avatar lawgimenez avatar lemikegao avatar mcrollin avatar mrgrauel avatar nexuist avatar readmecritic avatar retsohuang avatar taraskoksharov avatar thii avatar viktorasl avatar web-flow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swifthexcolors's Issues

Can return nil?

Documentation says usage is like:
let color: UIColor = UIColor(hexString: "#ff8942")

But how can this work if the function can return nil? Must be let color: UIColor? = ... then?

Update to Swift 2.0

Hi, I'm getting errors in Xcode 7. Do you have plans to update to Swift 2.0?

Thanks,
Kris

Swift 2 Arguments

Hello, I just installed this via CocoaPods and when using UIColor(hexString: "#FF8942") it returns the following:

Argument labels (hexStrings:) do not match any available overloads

What am I doing wrong ? Thanks

watchOS Support

Hey 👋

is there a reason you do not support watchOS? If not I'd like to contribute.

Regards,
Andreas

Using a default value for alpha

Hi,

I've built a library of pre-defined colours and am looking at adding some more helper functions. But in my code for each colour I set it up so that the alpha has a default value if the user doesn't specify one. That way I don't have to write two functions. Here's an example:

class func antiqueWhiteColor ( alpha: CGFloat = 1.0 ) -> UIColor { 
    return  UIColor.init ( red: 250.0/255.0, green: 235.0/255.0, blue: 215.0/255.0, alpha: alpha ) }

I do this instead of writing
class func antiqueWhiteColor ( ) -> UIColor {
return UIColor.antiqueWhiteColor ( alpha: 1.0 ) }

class func antiqueWhiteColor ( alpha: CGFloat ) -> UIColor { 
    return  UIColor.init ( red: 250.0/255.0, green: 235.0/255.0, blue: 215.0/255.0, alpha: alpha ) }

Swift 4.2

Hey there,

are you guys going to create a new version and add it to the pod spec to reflect the new Swift 4.2. version changes?

Not that big of a deal, but it would kill the SWIFT4.2. warning without having to set the swift version manually in the pod file.

Cheers,
Evangelos

报错

po scanner.scanString("#")
error: Trying to put the stack in unreadable memory at: 0x16cd67ed0.

Error in XCode 6.1

In XCode 6.1, i get the following errors:

  1. Type 'UIColor' does not conform to protocol 'IntegerLiteralConvertible'
  2. 'scannerWithString' is unavailable: use object construction 'NSScanner(string:)'

swifcolors-error

Feature request: ARGB format

Would it be possible to add support for ARGB notation? So for example #80FFFF00 would get parsed as yellow with 50.2% transparency.

7.3 beta 4 warning

7.3 b4 includes some warnings for a deprecated init:start:end function.

let greenHex = hex.substringWithRange(Range<String.Index>(start: hex.startIndex.advancedBy(2), end: hex.startIndex.advancedBy(4)))

should be changed to:

let greenHex = hex.substringWithRange(Range<String.Index>(hex.startIndex.advancedBy(2) ..< hex.startIndex.advancedBy(4)))

Nil for 1 (hex) digit colors.

For this call:
UIColor(hex: color)
Initializer returns nil for all values between 0x000000 and 0x00000f because of formatting string:
var hexString = String(format: "%2X", hex) (SwiftHEXColors.swift line 111) which results in
" 0" for 0x000000 etc.

First solution:
Change
var hexString = String(format: "%2X", hex)
to
var hexString = String(format: "%02X", hex)

Second (better) solution:
You don't need to format Int to String, it's a waste of CPU time. The easiest method I can think of to convert Int to RGB color is:

convenience init(hex: Int) {
        let b = hex % 256
        let g = (hex >> 8) % 256
        let r = (hex >> 16) % 256

        let s = CGFloat(256)

        self.init(red: CGFloat(r)/s, green: CGFloat(g)/s, blue: CGFloat(b)/s, alpha: 1.0)
    }

Cant install using Carthage on Sierra/XCode 8.0

I get the following output when trying to install using Carthage for platform iOS:

*** Building scheme "SwiftHEXColors" in SwiftHEXColors.xcodeproj
** CLEAN FAILED **


The following build commands failed:
    Check dependencies
(1 failure)
** BUILD FAILED **


The following build commands failed:
    Check dependencies
(1 failure)
A shell task failed with exit code 65:
** CLEAN FAILED **


The following build commands failed:
    Check dependencies
(1 failure)
** BUILD FAILED **


The following build commands failed:
    Check dependencies
(1 failure)

Swift 3 Valid Architectures Fix it !

Hi Just to report that I have brand new project in Xcode 8 and installed the Swift 3 branch
with podfile config

platform :ios, '9.0'
use_frameworks!

target 'SwiftHexColors_SW3' do

pod 'SwiftHEXColors', :git => 'https://github.com/thii/SwiftHEXColors', :branch => 'swift-3.0'

end

And it gave me errors i.e. couldn't compile.
Then I deleted arm7 and arm7s in Build Settings -> Valid Architectures

and then I build again and all was working .

Thank you

Swift 2.3 compatibility

I've seen there's a branch compatible with Swift 3. Anyway, there's no branch compatible with Swift 2.3, since master is compatible with Swift 2.2.

Hex colors with leading zeros are not parsed

Hex colors with leading zeros don't seem to be handled correctly in version 1.0.0. Debugging shows that leading zeros are just truncated when hex number is converted to string here:

public convenience init?(hex: Int, alpha: Float) {
    let hexString = NSString(format: "%2X", hex)
    self.init(hexString: hexString as String , alpha: alpha)
}

For example, hex value of 0x009C17 results in hexString 9C17, which, apparently, is not correct and cannot be parsed.

Taking Color Space into account

Hex representation of web colors is specified in sRGB color space. Shouldn’t that be taken into account when converting to and from a hex string? Input color can be in any color space and that affects the hex output.

Also some colors (like system colors) don’t have RGB values so they need to be converted to another color space first too.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.