Skip to content

Instantly share code, notes, and snippets.

@DominatorVbN
Created February 1, 2019 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DominatorVbN/7bf2edc49f82e58be809eb4379f5bf7b to your computer and use it in GitHub Desktop.
Save DominatorVbN/7bf2edc49f82e58be809eb4379f5bf7b to your computer and use it in GitHub Desktop.
UIColor Extension with convinent initializers for hex code and rgb values
//
// UIColor+RGB.swift
// CfssApp
//
// Created by mac on 31/01/19.
// Copyright © 2019 mmfinfotech. All rights reserved.
//
import UIKit
extension UIColor {
/**
custom initializer for using rgb color code directly i.e.(without dividing it by 255)
- Parameters:
- red: value of red in color.
- green: value of green in color.
- blue: value of blue in color.
*/
convenience init(red: Int, green: Int, blue: Int) {
assert(red >= 0 && red <= 255, "Invalid red component")
assert(green >= 0 && green <= 255, "Invalid green component")
assert(blue >= 0 && blue <= 255, "Invalid blue component")
self.init(red: CGFloat(red) / 255.0, green: CGFloat(green) / 255.0, blue: CGFloat(blue) / 255.0, alpha: 1.0)
}
/**
custom initializer for using hex color codes.
- Parameters:
- rgb: hash value of color in formate 0xffffff.
You can use this initilizer as:
````
let color = UIColor.init(rgb:0xff0000)
````
will give red color
*/
convenience init(rgb: Int) {
self.init(
red: (rgb >> 16) & 0xFF,
green: (rgb >> 8) & 0xFF,
blue: rgb & 0xFF
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment