0

I'm using the SwiftHSVColorPicker framework and needed to generate a random color on the color wheel.My current way of doing works but something that brightness is off.Here is my code

func generateRandomColor() -> UIColor {
    let lowerx : UInt32 = UInt32(0.0)
    let upperx : UInt32 = 707
    let randomNumberx = arc4random_uniform(upperx - lowerx) + lowerx
    let lowery : UInt32 = UInt32(0.0)
    let uppery : UInt32 = 707
    let randomNumbery = arc4random_uniform(upperx - lowerx) + lowerx
    let c = Colorwheel.colorWheel.hueSaturationAtPoint(CGPoint(x: Double(randomNumberx), y: Double(randomNumbery)))
    let brightness = 1.0
    return UIColor(hue: c.hue, saturation: c.saturation, brightness: CGFloat(brightness), alpha: 1.0)
}
James Ikeler
  • 111
  • 9

1 Answers1

1

Why don't you use something like

func getRandomColor() -> UIColor{

    let randomRed:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)
    let randomGreen:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)
    let randomBlue:CGFloat = CGFloat(arc4random()) / CGFloat(UInt32.max)

    return UIColor(red: randomRed, green: randomGreen, blue: randomBlue, alpha: 1.0)

}

EDIT:

Try this, In this hue,brightness is also there

func generateRandomColor() -> UIColor {
  let hue : CGFloat = CGFloat(arc4random() % 256) / 256 // use 256 to get full range from 0.0 to 1.0
  let saturation : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from white
  let brightness : CGFloat = CGFloat(arc4random() % 128) / 256 + 0.5 // from 0.5 to 1.0 to stay away from black

  return UIColor(hue: hue, saturation: saturation, brightness: brightness, alpha: 1)
}

SwiftHSVColorPicker results

enter image description here

Rajan Maheshwari
  • 14,465
  • 6
  • 64
  • 98