iOS Find Color at Point Between Two Colors

后端 未结 5 790
感情败类
感情败类 2020-12-05 06:00

I have a problem: I need to be able to take two colors and make a \'virtual gradient\' out of them. I then need to be able to find the color at any point on this line. My cu

5条回答
  •  旧巷少年郎
    2020-12-05 06:16

    Thanks @ramchandra-n I implemented the extension to get the intermediate color from an array of colors by percentage

    extension Array where Element: UIColor {
        func intermediate(percentage: CGFloat) -> UIColor {
            let percentage = Swift.max(Swift.min(percentage, 100), 0) / 100
            switch percentage {
            case 0: return first ?? .clear
            case 1: return last ?? .clear
            default:
                let approxIndex = percentage / (1 / CGFloat(count - 1))
                let firstIndex = Int(approxIndex.rounded(.down))
                let secondIndex = Int(approxIndex.rounded(.up))
                let fallbackIndex = Int(approxIndex.rounded())
    
                let firstColor = self[firstIndex]
                let secondColor = self[secondIndex]
                let fallbackColor = self[fallbackIndex]
    
                var (r1, g1, b1, a1): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
                var (r2, g2, b2, a2): (CGFloat, CGFloat, CGFloat, CGFloat) = (0, 0, 0, 0)
                guard firstColor.getRed(&r1, green: &g1, blue: &b1, alpha: &a1) else { return fallbackColor }
                guard secondColor.getRed(&r2, green: &g2, blue: &b2, alpha: &a2) else { return fallbackColor }
    
                let intermediatePercentage = approxIndex - CGFloat(firstIndex)
                return UIColor(red: CGFloat(r1 + (r2 - r1) * intermediatePercentage),
                               green: CGFloat(g1 + (g2 - g1) * intermediatePercentage),
                               blue: CGFloat(b1 + (b2 - b1) * intermediatePercentage),
                               alpha: CGFloat(a1 + (a2 - a1) * intermediatePercentage))
            }
        }
    }
    

    You can use it to get an intermediate color between two or more colors:

    let color = [.green, .yellow, .red].intermediate(percentage: 70)
    

提交回复
热议问题