Convenience methods for creating color using RGBA hex string.

Overview

UIColor+Hex, now Swift. Build Status codecov.io Carthage compatible

Convenience method for creating autoreleased color using RGBA hex string.

    // Solid color
    let strokeColor = UIColor("#FFCC00").cgColor 
    
    // Color with alpha
    let fillColor = UIColor("#FFCC00DD").cgColor 

    // Supports shorthand 3 character representation
    let backgroundColor = UIColor("#FFF") 

    // Supports shorthand 4 character representation (with alpha)
    let menuTextColor = UIColor("#013E") 

    // "#FF0000FF"
    let hexString = UIColor.red.hexString()

    // Convert shorthand 4 character representation (with alpha) from argb to rgba
    if let rgba = "#AFFF".argb2rgba {            
        let androidBackgroundColor = UIColor(rgba)
    }

    // Convert 8 character representation (with alpha) from argb to rgba
    if let rgba = "#AAFFFFFF".argb2rgba {        
        let androidFrontColor = UIColor(rgba)
    }

Release Notes

  • Upgrade to Swift 5.
  • macOS gets supported.

Installation

Swift Package Manager

To add a package dependency to your Xcode project, select File > Swift Packages > Add Package Dependency and enter https://github.com/yeahdongcn/UIColor-Hex-Swift to the text field.

CocoaPods

Simply add the following lines to your Podfile:

# required by CocoaPods 0.36.0.rc.1 for Swift Pods
use_frameworks! 

pod 'UIColor_Hex_Swift', '~> 5.1.1'

Then import it where you use it:

import UIColor_Hex_Swift

(CocoaPods v0.36 or later required. See this blog post for details.)

Carthage

Simply add the following line to your Cartfile:

= 5.1.1 ">
github "yeahdongcn/UIColor-Hex-Swift" >= 5.1.1

Then add the HexColor.framework to your frameworks list in the Xcode project.

Then import it where you use it:

import HEXColor

See more in RSBarcodes_Swift and objc version

Comments
  • Carthage build fails for WatchOS

    Carthage build fails for WatchOS

    I'm using Carthage to get UIColor-Hex and since the latest Xcode update the repo fails to compile for all architectures.

    When I run carthage update I get this error when it reaches the HexColor:

    *** Checking out UIColor-Hex-Swift at "2.0"
    *** xcodebuild output can be found in /var/folders/06/dgpth9f15tsb1r1_j34528x40000gn/T/carthage-xcodebuild.G2t9PI.log
    *** Building scheme "HEXColor-watchOS" in HEXColor.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)
    
    opened by sprint84 9
  • Argument labels '(rgba:)' do not match any available overloads

    Argument labels '(rgba:)' do not match any available overloads

    pod 'UIColor_Hex_Swift', '~> 3.0.2' import UIColor_Hex_Swift

    Argument labels '(rgba:)' do not match any available overloads

    import HEXColor no such model

    opened by googlb 8
  • version 4.0.0 it's not yet on cocoapods

    version 4.0.0 it's not yet on cocoapods

    Hi, When I try pod 'UIColor_Hex_Swift', '~> 4.0.0' it cause error on pod update. It seems version 4 it's not updated on cocoapods yet. I used pod 'UIColor_Hex_Swift', :git => 'https://github.com/yeahdongcn/UIColor-Hex-Swift.git' and it works

    opened by johncodeos 7
  • Invalid hex strings produced from wide display colors

    Invalid hex strings produced from wide display colors

    The documentation for getRed(: green: blue: ) explains that older versions of iOS always set the values to a range within 0 and 1, but not any more. When one of these returns outside the expected range, hexString(:) produces invalid hex codes (for example, #54A8FFFFFFDF).

    opened by jordansmithnz 6
  • UInt32 for the argument

    UInt32 for the argument

    This pull request includes:

    1. Xcode Project with shared scheme (New version carthage seems to find no shared scheme for current UIColor-Hex-Swift)
    2. XCTestCase instead of Sample
    3. init(hex: UInt32) instead of int(rgba: String)

    3. is just my suggestion. I wondered if I should make a new repository, but too similar purpose to do so. UInt32 argument leaves us from many exceptions:

    • Does it include #?
    • Is it hex format? (ex. #FGFFFF)
    • Is it not too long? (ex. #FFFFFFFFF)

    So, I think UInt32 argument is better than String for UIColor-Hex. Your repository is very famous for UIColor extension for Hex format, so I would like to hear your opinion.


    I created initializers for each format.

    • init(hex3: UInt16, alpha: CGFloat = 1)
    • init(hex4: UInt16)
    • init(hex6: UInt32, alpha: CGFloat = 1)
    • init(hex8: UInt32)
    opened by uny 5
  • Update(see comment)

    Update(see comment)

    • Added tests for UIColorInputError.
    • Improved functionality for argb2rgba converting.
    • Removed .missingHashMarkAsPrefix from UIColorInputError enum.
    • Updated readme.
    opened by SeRG1k17 3
  • Undefined symbols for architecture x86_64

    Undefined symbols for architecture x86_64

    Tried to build the app on Swift 3.0, XCode version 8.2.1 I got the error on linking step.

    Undefined symbols for architecture x86_64: "(extension in UIColor_Hex_Swift):__ObjC.UIColor.(init (Swift.String, defaultColor : __ObjC.UIColor) -> __ObjC.UIColor).(default argument 1)"

    opened by xPutnikx 3
  • Argument Labels Don't Match Overloads Issue

    Argument Labels Don't Match Overloads Issue

    Hey,

    Great git guys.

    I followed the installation process and I get an error: Argument labels '(rgba:)' do not match any available overloads')' when i try to add this line in my App Delegate

        UITabBar.appearance().barTintColor = UIColor(rgba: "#12296f")
    

    Do i have to add an import statement? If so what is it? What am i missing?

    Thanks a lot

    opened by rinyfo4 3
  • Minimum deployment target using Carthage

    Minimum deployment target using Carthage

    Using Carthage to install UIColor-Hex-Swift and my project's deployment target set to iOS 8.0, I get this error when building:

    "Module file's minimum deployment target is ios9.0 v9.0"

    Looking at the pod spec, it seems that the minimum version should be 8.0.

    opened by schjonhaug 3
  • Swift Package Manager

    Swift Package Manager

    I see that the support for SPM has been added, when you will release a new version in order to be able to add the package to SPM?

    Now I have the following error: Screenshot 2019-10-05 at 17 14 47

    opened by tradingstratagem 2
  • Support MacOS

    Support MacOS

    Can you add support for MacOS You can do it easily with an typealias

    I've had to import it to my Mac Project from source rather than cocoapods. Would prefer Cocoapods.

    A quick way to support macos Just replace top of UIColorExtension.swift

    import UIKit
    @objc extension UIColor {
    

    WITH

    
    #if os(iOS)
    //---------------------------
    //iOS
    //---------------------------
    import Foundation
    import UIKit
    typealias AppColor = UIColor
    
    //---------------------------
    #elseif os(macOS)
    //---------------------------
    //macOS
    //---------------------------
    import Cocoa
    typealias AppColor = NSColor
    
    //---------------------------
    //#else
    // os(tvOS) || os(watchOS)
    //---------------------------
    #endif
    
    
    @objc extension AppColor {
        /**
         The shorthand three-digit hexadecimal representation of color.
         #RGB defines to the color #RRGGBB.
         
         - parameter hex3: Three-digit hexadecimal value.
         - parameter alpha: 0.0 - 1.0. The default is 1.0.
         */
        public convenience init(hex3: UInt16, alpha: CGFloat = 1) {
            let divisor = CGFloat(15)
            let red     = CGFloat((hex3 & 0xF00) >> 8) / divisor
            let green   = CGFloat((hex3 & 0x0F0) >> 4) / divisor
            let blue    = CGFloat( hex3 & 0x00F      ) / divisor
            self.init(red: red, green: green, blue: blue, alpha: alpha)
        }
    ...}
    

    Swift 5 was throwing errors about failable inits and non failable in I had to change init

       public convenience init(_ rgba: String, defaultColor: NSColor = NSColor.clear) {
    
    

    TO init?

    public convenience init?(_ rgba: String, defaultColor: NSColor = NSColor.clear) {
    
    opened by clearbrian 2
  • Cocoapods does not have version 5.1.9

    Cocoapods does not have version 5.1.9

    The Cocoapods Specs only has up to version 5.1.7 and not 5.1.9. Is this intended?

    [!] CocoaPods could not find compatible versions for pod "UIColor_Hex_Swift":
      In Podfile:
        UIColor_Hex_Swift (~> 5.1.9)
    
    None of your spec sources contain a spec satisfying the dependency: `UIColor_Hex_Swift (~> 5.1.9)`.
    

    https://cocoapods.org/pods/UIColor_Hex_Swift

    opened by skelleher1256 0
  • The different name between SwiftPackageManager and Cocoapod

    The different name between SwiftPackageManager and Cocoapod

    When I create third part SDK depend on UIColor-Hex-Swift , because of the different name between SwiftPackageManager and Cocoapod, I cant import in the SDK to my app.

    when I use the SwiftPackageManage name in my SDK, I cant pod trunk push my SDK to remote, but when I use Pod name in my SDK, I cant use SwiftPackageManage to install my SDK in my App.

    opened by zkhCreator 0
  • Can't find package with Xcode 13.1

    Can't find package with Xcode 13.1

    Hi,

    Thanks for offering a great package.

    Swift Package Manager can no longer find UIColor-Hex-Swift after updating to the latest version of Xcode.

    Any idea why?

    Thanks! Blessings, --Mark

    opened by garageshop 2
  • Archive export issues when used as a Swift Package

    Archive export issues when used as a Swift Package

    We recently started migrating from Cocoapods to SwiftPM for our projects and I faced this issue while exporting an archive:

    + xcodebuild -exportArchive -exportOptionsPlist /var/folders/3v/vf11v3tx64j79b239rzbg_280000gn/T/gym_config20211103-77126-b4sgdz.plist -archivePath '/Users/{SANITIZED_USERNAME}/Library/Developer/Xcode/Archives/2021-11-03/{SANITIZED_PROJECT_NAME} 2021-11-03 17.32.44.xcarchive' -exportPath /var/folders/3v/vf11v3tx64j79b239rzbg_280000gn/T/gym_output20211103-77126-1ub76v
    2021-11-03 17:34:05.031 xcodebuild[81855:2683877] [MT] IDEDistribution: -[IDEDistributionLogging _createLoggingBundleAtPath:]: Created bundle at path "/var/folders/3v/vf11v3tx64j79b239rzbg_280000gn/T/{SANITIZED_PROJECT_NAME}_2021-11-03_17-34-05.031.xcdistributionlogs".
    2021-11-03 17:34:46.374 xcodebuild[81855:2683877] -[NSNull path]: unrecognized selector sent to instance 0x1fa120340
    ** INTERNAL ERROR: Uncaught exception **
    Uncaught Exception: -[NSNull path]: unrecognized selector sent to instance 0x1fa120340
    Stack:
      0   __exceptionPreprocess (in CoreFoundation)
      1   objc_exception_throw (in libobjc.A.dylib)
      2   -[NSObject(NSObject) __retain_OA] (in CoreFoundation)
      3   ___forwarding___ (in CoreFoundation)
      4   _CF_forwarding_prep_0 (in CoreFoundation)
      5   -[IDEDistributionProcessingPipeline process:] (in IDEFoundation)
      6   -[IDEDistributionPackagingStep loadFromExportOptions:error:] (in IDEFoundation)
      7   -[IDEDistributionDriver runWithDestinationPath:error:] (in IDEFoundation)
      8   -[Xcode3CommandLineBuildTool _distributeArchiveAndExit] (in Xcode3Core)
      9   -[Xcode3CommandLineBuildTool run] (in Xcode3Core)
     10   main (in xcodebuild)
     11   start (in dyld)
    
    /Users/{SANITIZED_PROJECT_NAME}/.rvm/gems/ruby-3.0.0/gems/fastlane-2.197.0/gym/lib/assets/wrap_xcodebuild/xcbuild-safe.sh: line 61: 81855 Abort trap: 6           xcodebuild "$@"
    

    When I removed the UIColor-Hex-Swift from Swift Package dependencies the issue was resolved. Please note that using it with Cocoapods does not reproduce the above issue.

    opened by ispiropoulos 0
Releases(4.2.0)
Owner
R0CKSTAR
Developer
R0CKSTAR
HEX color handling as an extension for UIColor. Written in Swift.

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

Thi Doãn 692 Dec 22, 2022
A simple UIColor category to get color with hex code.

TFTColor A simple UIColor library to get UIColor object from RGB hex string/value, CMYK hex string/value or CMYK base component values. You can also r

Burhanuddin Sunelwala 18 Jun 21, 2022
HEX color handling as an extension for UIColor

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

Thi Doãn 692 Dec 22, 2022
ColorWheel Test - An attempt at creating a Color Wheel to be utilized for color picking in SwiftUI utlizing various tutuorials on youtube

This code focuses on creating a Color Wheel in which a user will be able to select a desired color to then be implemented in another project that will display that color in an LED connected to an arduino

Gerardo Cerpa 0 Jan 15, 2022
A beautiful set of predefined colors and a set of color methods to make your iOS/OSX development life easier.

Installation Drag the included Colours.h and Colours.m files into your project. They are located in the top-level directory. You can see a demo of how

Ben Gordon 3.1k Dec 28, 2022
Generate color based on the given string.

PFColorHash Swift 4.0 SUPPORT! Generate color based on the given string. Thanks to color-hash. Usage Basic let colorHash = PFColorHash() // in HSL, H

null 26 Feb 23, 2022
System Color Picker - The macOS color picker as an app with more features

System Color Picker The macOS color picker as an app with more features Download Requires macOS 11 or later. Features Quickly copy, paste, and convert

Sindre Sorhus 758 Dec 24, 2022
HexColor is a simple extension that lets you initialize UIColors the way they were meant to be initialized: With hex integer values

HexColor is a simple extension that lets you initialize UIColors the way they were meant to be initialized: With hex integer values. Requirem

Tuomas Artman 103 Nov 24, 2022
UIGradient - A simple and powerful library for using gradient layer, image, color

UIGradient is now available on CocoaPods. Simply add the following to your project Podfile, and you'll be good to go.

Đinh Quang Hiếu 247 Dec 1, 2022
ChromaColorPicker - An intuitive HSB color picker built in Swift

An intuitive HSB color picker built in Swift. Supports multiple selection handles and is customizable to your needs.

Jonathan Cardasis 536 Dec 29, 2022
SheetyColors is an action sheet styled color picker for iOS

?? Based on UIAlertController: The SheetyColors API is based on UIKit's UIAlertController. Simply add buttons to it as you would for any other Action Sheet by defining UIAlertAction instances. Therefore, it nicely integrates with the look & feel of all other native system dialogs. However, you can also chose to use the color picker it self without an action sheet.

Christoph Wendt 102 Nov 10, 2022
A tool to calculate the color ratio of UIImage in iOS.

UIImageColorRatio A tool to calculate the color ratio of UIImage in iOS. How to use UIImageColorRatio Get the color ratio of UIImage. let image = ...

Yanni Wang 王氩 34 Jan 1, 2023
Color framework for Swift & Objective-C (Gradient colors, hexcode support, colors from images & more).

Swift 3 To use the Swift 3 version, add this to your Podfile (until 2.2 or higher is released): pod 'ChameleonFramework/Swift', :git => 'https://githu

Vicc Alexander 12.5k Dec 27, 2022
An attractive color generator for Swift. Ported from randomColor.js.

Random Color Swift Inspired by David Merfield's randomColor.js. It is a ported version to Swift. You can use the library to generate attractive random

Wei Wang 624 Jan 3, 2023
Flat UI color palette helpers written in Swift.

FlatUIColors (swift) install Make sure you have the latest version of CocoaPods (gem install cocoapods) that has Swift support. At the time of this wr

Bryn Bellomy 172 Dec 6, 2022
A UIColor extension with CSS3 Color names.

CSS3ColorsSwift Overview CSS3ColorsSwift provides a UIColor extension with Web Color names. Demo Run the demo project in the Demo directory without ca

WorldDownTown 67 Aug 6, 2022
Google Material Color Palette in Swift

Google Material Color in Swift Defined Google Material Color value ready to use in Swift refer to Google Material Design in Style/Color section ###How

Todsaporn Banjerdkit 48 Oct 30, 2019
A Lightweight But Powerful Color Kit (Swift)

BCColor A lightweight but powerful color kit (Swift) Features Pick Colors From Image Generate Monochrome Image Support Hex Color Style Lighten / Darke

Xiaobo Zhang 418 Nov 25, 2022
Aesthetic color-scheme generation written in Swift

Lorikeet Lightweight framework for generating visually aesthetic color-schemes in Swift Requirements UIKit Features What can Lorikeet do for you Calcu

Þorvaldur Rúnarsson 31 Jun 21, 2022