Skip to content

Instantly share code, notes, and snippets.

@noahsark769
Created February 27, 2019 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noahsark769/7f09bcae807bc4a0e3d3f900e50c4c12 to your computer and use it in GitHub Desktop.
Save noahsark769/7f09bcae807bc4a0e3d3f900e50c4c12 to your computer and use it in GitHub Desktop.
Get the point on a color wheel image for a given color
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