UISlider - change multiple colors with gradation

Discussion in 'iOS Programming' started by Darkroom, Jun 28, 2009.

  1. Darkroom Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    does apple (or anyone else) have an example code of UISlider changing multiple colors with gradation? i've been at this problem of mine all day, and i just can't get it quite right.

    i have a UISlider with values from 0 to 1.0. position 0 of the UISlider should return one color, 0.5 should be white, and 1.0 the other color, and of course they need to gradate while shifting slider positions...

    please let me know if there is any sample code showing the math formula to do this.

    right now i can get white at 0.0 and gradate to purple at 1.0... but my brain is turning to mush. although this problem at first seemed exciting and rewarding if successful, i've accepted the fact that code glory will have to wait and i totally need help.

    Code:
    float purplesRedValue = 105 / 255.0;
    float purplesGreenValue = 0 / 255.0;
    float purplesBlueValue = 122 / 255.0;
    
    self.myView.backgroundColor = [UIColor	colorWithRed: (1.0 - [sender value]) + ([sender value] * purplesRedValue)
    	green: (1.0 - [sender value]) + ([sender value] * purplesGreenValue)
    	blue: (1.0 - [sender value]) + ([sender value] * purplesBlueValue)
    	alpha:1.0];
    
     
  2. Darkroom thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    figured it out... damn, that took way too long, but at least it now works, and YAY FOR CODE GLORY!

    my problem was the minimum and maximum values should not have been set to 0.00 and 1.00. instead, i should have set them to -1.00 and 1.00.

    Code:
    - (IBAction)changeColor:(UISlider *)sender
    	{
    	//Color One RGB Values
    	float colorOneOfRed = 35.0 / 255.0;
    	float colorOneOfGreen = 211.0 / 255.0;
    	float colorOneOfBlue = 255.0 / 255.0;
    
    	//Color Two RGB Values
    	float colorTwoOfRed = 87.0 / 255.0;
    	float colorTwoOfGreen = 11.0 / 255.0;
    	float colorTwoOfBlue = 198.0 / 255.0;
    
    	if ([sender value] > 0.0)
    		{
    		self.myView.backgroundColor = [UIColor	colorWithRed:	(1.0 - [sender value]) + ([sender value] * colorOneOfRed)
    									green:			(1.0 - [sender value]) + ([sender value] * colorOneOfGreen)
    									blue:			(1.0 - [sender value]) + ([sender value] * colorOneOfBlue)
    									alpha:			1.0];
    		}
    									
    	if ([sender value] < 0.0)
    		{
    		self.myView.backgroundColor = [UIColor colorWithRed:	(1.0 + [sender value]) - ([sender value] * colorTwoOfRed)
    									green:			(1.0 + [sender value]) - ([sender value] * colorTwoOfGreen)
    									blue:			(1.0 + [sender value]) - ([sender value] * colorTwoOfBlue)
    									alpha:			1.0];
    		}
    	}
    
     

Share This Page