Created
February 27, 2019 16:47
-
-
Save noahsark769/7f09bcae807bc4a0e3d3f900e50c4c12 to your computer and use it in GitHub Desktop.
Get the point on a color wheel image for a given color
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension UIImageView { | |
func pointOnColorWheel(for color: UIColor) -> CGPoint? { | |
guard let image = self.image else { return nil } | |
var hue: CGFloat = 0 | |
var saturation: CGFloat = 0 | |
var brightness: CGFloat = 0 | |
color.getHue(&hue, saturation: &saturation, brightness: &brightness, alpha: nil) | |
let width = self.frame.size.width | |
let radius = width / 2 | |
let colorRadius = saturation * radius | |
let angle = (1 - hue) * (2 * CGFloat.pi) | |
let midX = width / 2 | |
let midY = self.frame.size.height / 2 | |
let xOffset = cos(angle) * colorRadius | |
let yOffset = sin(angle) * colorRadius | |
return CGPoint(x: midX + xOffset, y: midY + yOffset) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment