<commit_before>---
layout: post
category : Notes
title:  "UIColor with Hex"
---

Sometimes we want to create UIColor with hex, but there isn't any builtin way, so let't make our own.

We hope our function to create UIColor with hex is something like `UIColor(1BB0F4FF)`, obviously it's better to be added as extension.

```swift
extension UIColor {
	convenience init(hex: Int) {
		
	}
}
```

So here comes the problem: **how can we convert hex to demical?**

First look at the given hex `1BB0F4FF`, we consider every two elements as a component of color's rgb, so the rgb is `red: 1B, green: B0, blue: F4`, and yes the last component is `alpha`.

In order to extract component from hex, we need to use bitwise shifting.

`hex >> 24` means right shift by `24` positions, in binary of course, results in the hex `1B`, which is `27` in demical. Additionally we should better add a logical AND operator with `0xff` to make it less than or equal to `255`.

Enough talking, let's showing the code.

```swift
convenience init(hex: Int) {
	self.init((hex >> 24) & 0xff, (hex >> 16) & 0xff, (hex >> 8) & 0xff, hex & 0xff)
}
```

Oops, there's an error said `Argument labels '(_:, _:, _:, _:)' do not match any available overloads`. 

![](/assets/images/excuseme.jpg)<commit_msg>Update 2016-04-19-UIColor-with-Hex.md<commit_after>---
layout: post
category : Notes
title:  "UIColor with Hex"
---

Sometimes we want to create UIColor with hex, but there isn't any builtin way, so let't make our own.

We hope our function to create UIColor with hex is something like `UIColor(1BB0F4FF)`, obviously it's better to be added as extension.

```swift
extension UIColor {
	convenience init(hex: Int) {
	
	}
}
```

So here comes the problem: **how can we convert hex to demical?**

First look at the given hex `1BB0F4FF`, we consider every two elements as a component of color's rgb, so the rgb is `red: 1B, green: B0, blue: F4`, and yes the last component is `alpha`.

In order to extract component from hex, we need to use bitwise shifting.

`hex >> 24` means right shift by `24` positions, in binary of course, results in the hex `1B`, which is `27` in demical. Additionally we should better add a logical AND operator with `0xff` to make it less than or equal to `255`.

Enough talking, let's showing the code.

```swift
convenience init(hex: Int) {
	self.init((hex >> 24) & 0xff, (hex >> 16) & 0xff, (hex >> 8) & 0xff, hex & 0xff)
}
```

Oops, there's an error said `Argument labels '(_:, _:, _:, _:)' do not match any available overloads`. 

![](/assets/images/excuseme.jpg)
{'fraction_non_alphanumeric': 0.11401796173369777, 'fraction_numerical': 0.025771183131589222, 'mean_word_length': 4.033398821218075, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1448656', 'n_tokens_mistral': 886, 'n_tokens_neox': 810, 'n_words': 384}