How to get color components of a CGColor correctly

后端 未结 6 1155
日久生厌
日久生厌 2021-01-02 14:04

I have a UILabel with black color;

i am writing the following code to get black color\'s components.

UIColor *aColor = [aLabel.textColor retain];
con         


        
6条回答
  •  走了就别回头了
    2021-01-02 14:31

    extension UIColor {
    
      func toRGB() -> UIColor? {
        guard let model = cgColor.colorSpace?.model else {
          return nil
        }
        
        let components = cgColor.components
    
        switch model {
        case .rgb:
          return UIColor(red: components?[0] ?? 0.0, green: components?[1] ?? 0.0, blue: components?[2] ?? 0.0, alpha: components?[3] ?? 0.0)
        case .monochrome:
          return UIColor(red: components?[0] ?? 0.0, green: components?[0] ?? 0.0, blue: components?[0] ?? 0.0, alpha: components?[1] ?? 0.0)
        default:
          return nil
        }
      }
    }
    

提交回复
热议问题