create a function that inverts rgb color to uicolor in swift

Here's a function that takes in RGB color values as arguments and returns a UIColor object with those colors inverted:

main.swift
func invertRGBToUIColor(red: CGFloat, green: CGFloat, blue: CGFloat) -> UIColor {
    let invertedRed = 1.0 - red
    let invertedGreen = 1.0 - green
    let invertedBlue = 1.0 - blue
    return UIColor(red: invertedRed, green: invertedGreen, blue: invertedBlue, alpha: 1.0)
}
277 chars
7 lines

You can call this function with the RGB values of the color you want to invert, like this:

main.swift
let myRed = CGFloat(0.2)
let myGreen = CGFloat(0.4)
let myBlue = CGFloat(0.6)

let invertedColor = invertRGBToUIColor(red: myRed, green: myGreen, blue: myBlue)
160 chars
6 lines

This will give you a UIColor object with the inverted colors.

related categories

gistlibby LogSnag