You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.5 KiB

6 years ago
  1. ![Hex](https://raw.githubusercontent.com/3lvis/Hex/master/Images/hex.png)
  2. >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.
  3. 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.
  4. 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.
  5. ## Installation
  6. ```ruby
  7. pod 'Hex'
  8. ```
  9. ## Create color
  10. ### Swift
  11. ``` swift
  12. import Hex
  13. let redColor = UIColor(hex: "FF0000")
  14. ```
  15. ### Objective-C
  16. ```objc
  17. @import Hex;
  18. UIColor *color = [[UIColor alloc] initWithHex:@"FF0000"];
  19. ```
  20. ## Translucency
  21. `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.
  22. Some examples:
  23. ```swift
  24. UIColor(hex: "#ff0000") => UIColor.redColor()
  25. UIColor(hex: "#ff000075") => UIColor.redColor().colorWithAlphaComponent(0.75)
  26. UIColor(hex: "#ff000050") => UIColor.redColor().colorWithAlphaComponent(0.50)
  27. ```
  28. ## Check for equal colors
  29. 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.
  30. ```swift
  31. let blackHex = UIColor(hex: "000000")
  32. let black = UIColor.blackColor()
  33. if blackHex.isEqualTo(black) {
  34. // Do something
  35. }
  36. ```
  37. ```objc
  38. UIColor *blackHex = [[UIColor alloc] initWithHex: @"000000"];
  39. UIColor *black = [UIColor blackColor];
  40. if ([blackHex isEqualTo:black]) {
  41. // Do something
  42. }
  43. ```
  44. ## Be Awesome
  45. If something looks stupid, please create a friendly and constructive issue, getting your feedback would be awesome.
  46. Have a great day.
  47. ## Author
  48. Elvis Nuñez, [@3lvis](https://twitter.com/3lvis)
  49. ## License
  50. **Hex** is available under the MIT license. See the [LICENSE](/LICENSE.md) file for more info.