iOS
Swift
Swift3.0

UIColorで四則演算

More than 1 year has passed since last update.

前はCGPointで四則演算したので今度はUIColorで

やれること

こういうことができるようになります
値とUIColorの計算ルールはまぁextensionに書いてる通りです

//準備
let red = UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
let green = UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0)
let white = UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

// UIColor同士で四則演算(めんどくさいから足し算だけ書く
red + green // yellow {red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0}

// 暗くしたい
white * 0.5 // {red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0} ※alphaは計算しないよ

extension

extension UIColor{
  func getRGBA()->(r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat){
    var red:CGFloat = 0
    var green:CGFloat = 0
    var blue:CGFloat = 0
    var alpha:CGFloat = 0
    self.getRed(&red, green: &green, blue: &blue, alpha: &alpha)
    return (r: red, g: green, b: blue, a: alpha)
  }

  static func + (left: UIColor, right: UIColor) -> UIColor {
    let c1 = left.getRGBA()
    let c2 = right.getRGBA()
    let r = max(0.0, min(1.0, c1.r + c2.r))
    let g = max(0.0, min(1.0, c1.g + c2.g))
    let b = max(0.0, min(1.0, c1.b + c2.b))
    let a = max(0.0, min(1.0, c1.a + c2.a))
    return UIColor(red: r, green: g, blue: b, alpha: a)
  }

  static func + (left: UIColor, right: CGFloat) -> UIColor {
    let c1 = left.getRGBA()
    let r = max(0.0, min(1.0, c1.r + right))
    let g = max(0.0, min(1.0, c1.g + right))
    let b = max(0.0, min(1.0, c1.b + right))
//    let a = max(0.0, min(1.0, c1.a + right))
    return UIColor(red: r, green: g, blue: b, alpha: c1.a)
  }

  static func + (left: CGFloat, right: UIColor) -> UIColor {
    return right + left
  }

  static func - (left: UIColor, right: UIColor) -> UIColor {
    let c1 = left.getRGBA()
    let c2 = right.getRGBA()
    let r = max(0.0, min(1.0, c1.r - c2.r))
    let g = max(0.0, min(1.0, c1.g - c2.g))
    let b = max(0.0, min(1.0, c1.b - c2.b))
    let a = max(0.0, min(1.0, c1.a - c2.a))
    return UIColor(red: r, green: g, blue: b, alpha: a)
  }

  static func - (left: UIColor, right: CGFloat) -> UIColor {
    let c1 = left.getRGBA()
    let r = max(0.0, min(1.0, c1.r - right))
    let g = max(0.0, min(1.0, c1.g - right))
    let b = max(0.0, min(1.0, c1.b - right))
//    let a = max(0.0, min(1.0, c1.a - right))
    return UIColor(red: r, green: g, blue: b, alpha: c1.a)
  }

  static func - (left: CGFloat, right: UIColor) -> UIColor {
    return -1 * (right - left)
  }

  static func * (left: UIColor, right: UIColor) -> UIColor {
    let c1 = left.getRGBA()
    let c2 = right.getRGBA()
    let r = max(0.0, min(1.0, c1.r * c2.r))
    let g = max(0.0, min(1.0, c1.g * c2.g))
    let b = max(0.0, min(1.0, c1.b * c2.b))
    let a = max(0.0, min(1.0, c1.a * c2.a))
    return UIColor(red: r, green: g, blue: b, alpha: a)
  }

  static func * (left: UIColor, right: CGFloat) -> UIColor {
    let c1 = left.getRGBA()
    let r = max(0.0, min(1.0, c1.r * right))
    let g = max(0.0, min(1.0, c1.g * right))
    let b = max(0.0, min(1.0, c1.b * right))
//    let a = max(0.0, min(1.0, c1.a * right))
    return UIColor(red: r, green: g, blue: b, alpha: c1.a)
  }

  static func * (left: CGFloat, right: UIColor) -> UIColor {
    return right * left
  }

  static func / (left: UIColor, right: UIColor) -> UIColor {
    let c1 = left.getRGBA()
    let c2 = right.getRGBA()
    let r = max(0.0, min(1.0, c1.r / c2.r))
    let g = max(0.0, min(1.0, c1.g / c2.g))
    let b = max(0.0, min(1.0, c1.b / c2.b))
    let a = max(0.0, min(1.0, c1.a / c2.a))
    return UIColor(red: r, green: g, blue: b, alpha: a)
  }

  static func / (left: UIColor, right: CGFloat) -> UIColor {
    let c1 = left.getRGBA()
    if right == 0 { return left }
    let r = max(0.0, min(1.0, c1.r / right))
    let g = max(0.0, min(1.0, c1.g / right))
    let b = max(0.0, min(1.0, c1.b / right))
//    let a = max(0.0, min(1.0, c1.a / right))
    return UIColor(red: r, green: g, blue: b, alpha: c1.a)
  }

  static func / (left: CGFloat, right: UIColor) -> UIColor {
    let c1 = right.getRGBA()
    if left == 0 { return right }
    let r = max(0.0, min(1.0, left / c1.r))
    let g = max(0.0, min(1.0, left / c1.g))
    let b = max(0.0, min(1.0, left / c1.b))
//    let a = max(0.0, min(1.0, left / c1.a))
    return UIColor(red: r, green: g, blue: b, alpha: c1.a)
  }
}