1

I have a program where I have a slider, and when i move it up or down (left or right) the color changes gradually. Sadly I am not able to achieve this. My colors change yes, but it is very sudden! I have the 7 colors of the rainbow on seperate .png files and when I scroll the respective one comes up. I was wondering if there was anything I could do to make the colors morph or blend into each other better to make the transaction appear muuch more smoothly.

Thank you

UPDATE

if(self.slider.value > 7 (
{
    self.label.text=@"red";
    //self.imgView.backgroundColor=[UIColor redColor];
    // self.imgView.backgroundColor=[UIColor colorWithPatternImage:@"redPicture"];
    self.view.backgroundColor=[UIColor colorWithRed:146 green:50 blue:146 alpha:1];

}
Sleep Paralysis
  • 449
  • 7
  • 22
  • 1
    Instead of an image, why not use some kind of shape control whose background color you can change programmatically? – Andrew Sep 30 '14 at 16:58
  • @AndrewArnold I can set the background of it programmatically yes, but I still find moving the slider gives me this "jump" from color to color. Probably what I am thinking of cannot be done. Was just wondering – Sleep Paralysis Sep 30 '14 at 17:00
  • It probably can, it's just that using images is not the way to go. Can you set the RBG value of the area instead? It'd help if you posted your code, too. – Andrew Sep 30 '14 at 17:03
  • @AndrewArnold I added the code, the commented code is what I have the last line is what im attempting, I tole the codes from photoshop – Sleep Paralysis Sep 30 '14 at 17:07

1 Answers1

2

This is going to be a generalized answer because I'm not going to write your code for you (not least because I have never written a letter of xcode in my life), but this should put you on the right track.

You want a continuous spectrum of color, so that should tell you right off the bat that using a series of if statements is the wrong way to go. Instead, you should calculate the color you want by doing some math with the slider value directly.

You haven't told me what your slider range is and whether it's discrete, so for the purposes of this answer let's call the lowest value min and the highest value max, just to keep things general. So your total range is max - min. Let's express the value of your slider as a percentage along this range; we can calculate this as (self.slider.value - min) / (max - min). (For instance, for a slider that goes from 0 to 50, a slider value of 37 gives you (37-0)/(50-0) = 0.74.)

So now you should have a decimal value between 0 and 1, which you can map along the Hue-Saturation-Value color scale. I don't know if xcode has a HSV method directly (this answer has some code which might be helpful), but if not it's pretty easy to convert HSV to RGB.

Community
  • 1
  • 1
Andrew
  • 4,953
  • 15
  • 40
  • 58
  • Thanks for helping me get one step closer. I really wasnt looking for something I could copy paste but rather the method. So i am trying to understand. say the values of my slider goes from 1-5 value 5 is blue and 4 is red. (5-1) / ( 5 – 1 ) 4 / 4 =1 When the sldier is on 5 the color I want is UIColor colorWithRed:44/255.0f green:141/255.0f blue:254/255.0f alpha:1.0f Then slider moves to 4 gives me a value of 0.75 And the value I want is UIColor colorWithRed:64/255.0f green:192/255.0f blue:254/255.0f alpha:1.0f. – Sleep Paralysis Oct 01 '14 at 15:07
  • I am not sure how, I should implement that decimal, and the RGB is what I use to state my color, wouldnt changing this based on the decimal give me a different color? – Sleep Paralysis Oct 01 '14 at 15:08
  • The method I describes just cycles through the spectrum from red to magenta; you don't really pick which values map to which colors—that's just the way the rainbow goes. You could do some more math to adjust your hue value before converting to RGB but that's up to you. – Andrew Oct 01 '14 at 15:15
  • hmmm, well thank you for your help. However I am really not seeing how I can make this work. In the event I want to change my spectrum of colors. However thanks! – Sleep Paralysis Oct 01 '14 at 15:59