Search code examples
iosiphonecolorsrgbuicolor

How to set UIColors based on touches on a RGB spectrum?


I am interesting in implementing a UIColor spectrum that appear similar to the one shown below. Based on where an user touch on the scale, I want the user to be able draw lines with the color selected via touch.

enter image description here

I am having trouble getting started to implement this concept. Since the RGB color begins with R = 255, G = 0, B = 0, but changes based on the the values of two colors changing simultaneously, I do not know how to correspond these changes on based only the increment in the y coordinate based on touch.

My questions are:

  1. Are there any open source examples of touched based color selections from a color spectrum that I could use or partly get guidance from? Even if they are not in objective-c, it'd still be very helpful to see how y coordinate change could correspond to the RGB value changes.

  2. What are some important segments in the RGB spectrum where the the colors inflect so if I were trying to implement the spectrum color selection on my own, I will be able to get started based on these values?

Thanks!


Solution

  • I think you're overthinking this. All you have to do is set up your slider to provide values from 0.0 to 1.0 and instead of creating a color with RGB, use +[UIColor colorWithHue:saturation:brightness:alpha:]. All you have to do is pass the sliders value to hue parameter.

    UIColor *color = [UIColor colorWithHue:mySlider.value saturation:1.0 brightness:1.0 alpha:1.0];