Giter Site home page Giter Site logo

hex's Introduction

Hex

A hex triplet is a six-digit, three-byte hexadecimal number used in HTML, CSS, SVG, and other computing applications to represent colors. The bytes represent the red, green and blue components of the color. One byte represents a number in the range 00 to FF (in hexadecimal notation), or 0 to 255 in decimal notation. This represents the least (0) to the most (255) intensity of each of the color components. Thus web colors specify colors in the True Color (24-bit RGB) color scheme.

Dealing with HEX colors in iOS is problematic and can become quite repetitive, we've long waited for Apple to do something about this but nothing has really happened.

This is why I created Hex, a simple tiny extension to UIColor that helps you in this case. It works nicely with both Objective-C and Swift.

Installation

pod 'Hex'

Create color

Swift

import Hex

let redColor = UIColor(hex: "FF0000")

Objective-C

@import Hex;

UIColor *color = [[UIColor alloc] initWithHex:@"FF0000"];

Translucency

HEX doesn't support translucency, since it's only RGB, but we thought why it shouldn't? That's why you can append the translucency percentage at the end of your HEX string.

Some examples:

UIColor(hex: "#ff0000")   => UIColor.red
UIColor(hex: "#ff000075") => UIColor.red.withAlphaComponent(0.75)
UIColor(hex: "#ff000050") => UIColor.red.withAlphaComponent(0.50)

Check for equal colors

Using isEqual on UIColor doesn't work when you create the color from an HEX string, this is because how UIKit handles colors. That's why before comparing a color you have to convert the colors to RGBSpace. Luckly Hex contains a helper method that helps you checking for equal colors, internally this method normalizes the color space of colors.

let blackHex = UIColor(hex: "000000")
let black = UIColor.black

if blackHex.isEqualTo(black) {
    // Do something
}
UIColor *blackHex = [[UIColor alloc] initWithHex: @"000000"];
UIColor *black = [UIColor blackColor];

if ([blackHex isEqualTo:black]) {
    // Do something
}

Be Awesome

If something looks stupid, please create a friendly and constructive issue, getting your feedback would be awesome.

Have a great day.

Author

Elvis Nuñez, @3lvis

License

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

hex's People

Contributors

3lvis avatar jeffleeismyhero avatar kchatzigeorgiou avatar nselvis avatar voxar 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

Watchers

 avatar  avatar  avatar

hex's Issues

Doesn't build in Xcode 8.2.1 due to Swift 2.3 syntax - please upgrade to Swift 3 syntax

This Form pod is awesome, and I'm eager to use it! Thanks so much for sharing it. :-)

I'm trying to build in Xcode 8.2.1 (8C1002) on OS X 10.12.3. My app has an iOS 10 target and is pure Objective-C.

I'm actually using Hex only by virtue of it's being a dependency of the Form pod, I'm not using Hex directly. See the related Form issue: https://github.com/hyperoslo/Form/issues/565

When I add the Form pod, Xcode asks if it can convert the Hex pod from Swift 2.3 to Swift 3. I tell it no, because I don't want to convert a Pod dependency since I should treat local pods as readonly. Then Xcode warns me that due to Hex's outdated Swift syntax, it won't be able to build properly and that certain "editor features" may not work right ...

I tried to build but got the error:

Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

Any way we could upgrade Hex to Swift 3 syntax so that I can build? :-) I'm happy to help.

Thanks.

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.