LoginSignup
3

posted at

updated at

便利なExtension(Color編)

Color

Color+.swift
// iOS
extension Color {
    // Color -> UIColor
    func toUIColor() -> UIColor {
        if let components = self.cgColor?.components {
            return UIColor(displayP3Red: components[0], green: components[1], blue: components[2], alpha: components[3])
        } else {
            return UIColor(displayP3Red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
        }
    }
    // Color -> RGB
    func toRGB() -> (red: CGFloat, green: CGFloat, blue: CGFloat) {
        if let components = self.cgColor?.components {
            return (red: components[0], green: components[1], blue: components[2])
        } else {
            return (red: 0.0, green: 0.0, blue: 0.0)
        }
    }
    // Color -> カラーコード
    func toColorCode() -> String {
        if let components = self.cgColor?.components {
            let rgb: [CGFloat] = [components[0], components[1], components[2]]
            return rgb.reduce("") { res, value in
                let intval = Int(round(value * 255))
                return res + (NSString(format: "%02X", intval) as String)
            }
        } else {
            return ""
        }
    }
    // Color(hex: カラーコード)
    init(hex: String) {
        let v = Int("000000" + hex, radix: 16) ?? 0
        let r = CGFloat(v / Int(powf(256, 2)) % 256) / 255
        let g = CGFloat(v / Int(powf(256, 1)) % 256) / 255
        let b = CGFloat(v / Int(powf(256, 0)) % 256) / 255
        self.init(red: r, green: g, blue: b)
    }
}

// MacOS
extension Color {
    // Color -> NSColor
    func toNSColor() -> NSColor {
        if let components = self.cgColor?.components {
            return NSColor(calibratedRed: components[0], green: components[1], blue: components[2], alpha: components[3])
        } else {
            return NSColor(calibratedRed: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
        }
    }
}

// Color -> UIColor
Color.white.toUIColor()

// Color -> RGB
Color.white.toRGB()

// Color -> カラーコード
Color.white.toColorCode()

// Color(hex: カラーコード)
Color(hax: "FFFFF")

UIColor

UIColor+.swift
extension UIColor {
    // UIColor -> Color
    func toColor() -> Color {
        if let components = self.cgColor.components {
            return Color(red: components[0], green: components[1], blue: components[2], opacity: components[3])
        } else {
            return Color(red: 0.0, green: 0.0, blue: 0.0, opacity: 0.0)
        }
    }
    // UIColor -> RGB
    func toRGB() -> (displayP3Red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
        if let components = self.cgColor.components {
            return (displayP3Red: components[0], green: components[1], blue: components[2], alpha: components[3])
        } else {
            return (displayP3Red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
        }
    }
    // UIColor -> カラーコード
    func toColorCode() -> String {
        if let components = self.cgColor.components {
            let rgb: [CGFloat] = [components[0], components[1], components[2]]
            return rgb.reduce("") { res, value in
                let intval = Int(round(value * 255))
                return res + (NSString(format: "%02X", intval) as String)
            }
        } else {
            return ""
        }
    }
    // UIColor(hex: カラーコード, alpha: CGFloat)
    convenience init(hex: String, alpha: CGFloat) {
        let v = Int("000000" + hex, radix: 16) ?? 0
        let r = CGFloat(v / Int(powf(256, 2)) % 256) / 255
        let g = CGFloat(v / Int(powf(256, 1)) % 256) / 255
        let b = CGFloat(v / Int(powf(256, 0)) % 256) / 255
        self.init(red: r, green: g, blue: b, alpha: alpha)
    }
}

// UIColor -> Color
UIColor.white.toColor()

// UIColor -> RGB
UIColor.white.toRGB()

// UIColor -> カラーコード
UIColor.white.toColorCode()

// UIColor(hex: カラーコード, alpha: CGFloat)
UIColor(hex: "FFFFFF", alpha: 1.0)

NSColor

NSColor+.swift
extension NSColor {
    // NSColor -> Color
    func toColor() -> Color {
        if let components = self.cgColor.components {
            return Color(red: components[0], green: components[1], blue: components[2], opacity: components[3])
        } else {
            return Color(red: 0.0, green: 0.0, blue: 0.0, opacity: 0.0)
        }
    }
    // NSColor -> RGB
    func toRGB() -> (displayP3Red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) {
        if let components = self.cgColor.components {
            return (displayP3Red: components[0], green: components[1], blue: components[2], alpha: components[3])
        } else {
            return (displayP3Red: 0.0, green: 0.0, blue: 0.0, alpha: 0.0)
        }
    }
    // NSColor -> カラーコード
    func toColorCode() -> String {
        if let components = self.cgColor.components {
            let rgb: [CGFloat] = [components[0], components[1], components[2]]
            return rgb.reduce("") { res, value in
                let intval = Int(round(value * 255))
                return res + (NSString(format: "%02X", intval) as String)
            }
        } else {
            return ""
        }
    }
    // NSColor(hex: カラーコード, alpha: CGFloat)
    convenience init(hex: String, alpha: CGFloat) {
        let v = Int("000000" + hex, radix: 16) ?? 0
        let r = CGFloat(v / Int(powf(256, 2)) % 256) / 255
        let g = CGFloat(v / Int(powf(256, 1)) % 256) / 255
        let b = CGFloat(v / Int(powf(256, 0)) % 256) / 255
        self.init(red: r, green: g, blue: b, alpha: alpha)
    }
}

// NSColor -> Color
NSColor.white.toColor()

// NSColor -> RGB
NSColor.white.toRGB()

// NSColor -> カラーコード
NSColor.white.toColorCode()

// NSColor(hex: カラーコード, alpha: CGFloat)
NSColor(hex: "FFFFFF", alpha: 1.0)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
3