Skip to content

Instantly share code, notes, and snippets.

@marchinram
marchinram / UIImage+PixelColor.swift
Last active January 19, 2022 08:53
iOS Swift UIImage subscript extension to get pixel color
import UIKit
extension UIImage {
subscript (x: Int, y: Int) -> UIColor? {
guard x >= 0 && x < Int(size.width) && y >= 0 && y < Int(size.height),
let cgImage = cgImage,
let provider = cgImage.dataProvider,
let providerData = provider.data,
let data = CFDataGetBytePtr(providerData) else {
return nil
@marchinram
marchinram / UIColor+Random.swift
Last active May 12, 2021 11:03
iOS Swift UIColor extension
import UIKit
extension UIColor {
class func randomColor() -> UIColor {
let red = CGFloat(arc4random() % 256) / 256.0
let green = CGFloat(arc4random() % 256) / 256.0
let blue = CGFloat(arc4random() % 256) / 256.0
let alpha = CGFloat(arc4random() % 256) / 256.0
return UIColor(red: red, green: green, blue: blue, alpha: alpha)
}