Skip to content

Instantly share code, notes, and snippets.

@nicholascross
Last active March 23, 2020 17:40
Show Gist options
  • Save nicholascross/4b730fdafd975644f690f57c27e93cfb to your computer and use it in GitHub Desktop.
Save nicholascross/4b730fdafd975644f690f57c27e93cfb to your computer and use it in GitHub Desktop.
Swift UIColor manipulation
//Combine colors: add, subtract, multiply components, set and adjust color components
import UIKit
func + (left: UIColor, right: UIColor) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
var rightRGBA = [CGFloat](repeating: 0.0, count: 4)
left.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3])
right.getRed(&rightRGBA[0], green: &rightRGBA[1], blue: &rightRGBA[2], alpha: &rightRGBA[3])
return UIColor(
red: min(max(leftRGBA[0] + rightRGBA[0], 0.0), 1.0),
green: min(max(leftRGBA[1] + rightRGBA[1], 0.0), 1.0),
blue: min(max(leftRGBA[2] + rightRGBA[2], 0.0), 1.0),
alpha: min(max(leftRGBA[3] + rightRGBA[3], 0.0), 1.0)
)
}
func - (left: UIColor, right: UIColor) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
var rightRGBA = [CGFloat](repeating: 0.0, count: 4)
left.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3])
right.getRed(&rightRGBA[0], green: &rightRGBA[1], blue: &rightRGBA[2], alpha: &rightRGBA[3])
return UIColor(
red: min(max(leftRGBA[0] - rightRGBA[0], 0.0), 1.0),
green: min(max(leftRGBA[1] - rightRGBA[1], 0.0), 1.0),
blue: min(max(leftRGBA[2] - rightRGBA[2], 0.0), 1.0),
alpha: min(max(leftRGBA[3] - rightRGBA[3], 0.0), 1.0)
)
}
func * (left: UIColor, right: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
left.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
red: min(max(leftRGBA[0] * right, 0.0), 1.0),
green: min(max(leftRGBA[1] * right, 0.0), 1.0),
blue: min(max(leftRGBA[2] * right, 0.0), 1.0),
alpha: min(max(leftRGBA[3] * right, 0.0), 1.0)
)
}
extension UIColor {
func color(withAlpha alpha: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
red: leftRGBA[0],
green: leftRGBA[1],
blue: leftRGBA[2],
alpha: min(max(leftRGBA[3] + alpha, 0.0), 1.0)
)
}
func color(withRed red: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
red: min(max(red, 0.0), 1.0),
green: leftRGBA[1],
blue: leftRGBA[2],
alpha: leftRGBA[3]
)
}
func color(withGreen green: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
red: leftRGBA[0],
green: min(max(green, 0.0), 1.0),
blue: leftRGBA[2],
alpha: leftRGBA[3]
)
}
func color(withBlue blue: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getRed(&leftRGBA[0], green: &leftRGBA[1], blue: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
red: leftRGBA[0],
green: leftRGBA[1],
blue: min(max(blue, 0.0), 1.0),
alpha: leftRGBA[3]
)
}
func color(withBrightness brightness: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getHue(&leftRGBA[0], saturation: &leftRGBA[1], brightness: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
hue: leftRGBA[0],
saturation: leftRGBA[1],
brightness: min(max(brightness, 0.0), 1.0),
alpha: leftRGBA[3]
)
}
func color(withSaturation saturation: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getHue(&leftRGBA[0], saturation: &leftRGBA[1], brightness: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
hue: leftRGBA[0],
saturation: min(max(saturation, 0.0), 1.0),
brightness: leftRGBA[2],
alpha: leftRGBA[3]
)
}
func color(withHue hue: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getHue(&leftRGBA[0], saturation: &leftRGBA[1], brightness: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
hue: min(max(hue, 0.0), 1.0),
saturation: leftRGBA[1],
brightness: leftRGBA[2],
alpha: leftRGBA[3]
)
}
func color(byShiftingHue hue: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getHue(&leftRGBA[0], saturation: &leftRGBA[1], brightness: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
hue: (leftRGBA[0] + hue).truncatingRemainder(dividingBy: 1.0),
saturation: leftRGBA[1],
brightness: leftRGBA[2],
alpha: leftRGBA[3]
)
}
func color(byAdjustingBrightness brightness: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getHue(&leftRGBA[0], saturation: &leftRGBA[1], brightness: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
hue: leftRGBA[0],
saturation: leftRGBA[1],
brightness: min(max(leftRGBA[2] + brightness, 0.0), 1.0),
alpha: leftRGBA[3]
)
}
func color(byAdjustingSaturation saturation: CGFloat) -> UIColor {
var leftRGBA = [CGFloat](repeating: 0.0, count: 4)
self.getHue(&leftRGBA[0], saturation: &leftRGBA[1], brightness: &leftRGBA[2], alpha: &leftRGBA[3])
return UIColor(
hue: leftRGBA[0],
saturation: min(max(leftRGBA[1] + saturation, 0.0), 1.0),
brightness: leftRGBA[2],
alpha: leftRGBA[3]
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment