Question

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!

Was it helpful?

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];

OTHER TIPS

There are quite a few different color models. Most involve at least 3 parameters:

HSB, RGB CMYK, LAB,

and various others.

Go to http://www.cocoacontrols.com and search on "Color picker". You will find quite a few. Most involve specifying 3 different settings: With RGB, it's typical to use 3 sliders. With HSB, you tend to use a color wheel (hue = angle around the circle, distance from the center = saturation) plus a slider for brightness.

HSB (Hue, Saturation and Brightness) is one of the easiest to understand.

If you want to let the user pick colors based on a single slider setting, you'll need to define a sequence of colors somehow. Your color bar looks like it's varying hue and keeping the saturation and brightness values at max. Just fix saturation and brightness and let the user pick a hue. UIColor includes methods that let you enter hue, satiation and brightness values, and also has a method that lets you query a given RGB color and get back HSB values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top