user2004159
user2004159

Reputation:

How to get RGB color value from UISlider

I have tried this code but not working properly.On value change of UISlider I called this method...

@property (strong, nonatomic) IBOutlet UISlider *r;
@property (strong, nonatomic) IBOutlet UISlider *g;
@property (strong, nonatomic) IBOutlet UISlider *b;
@property (strong, nonatomic) IBOutlet UILabel *colorLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];

    _r.minimumValue=0;
    _r.maximumValue=255;

    _g.minimumValue=0;
    _g.maximumValue=255;

    _b.minimumValue=0;
    _b.maximumValue=255;

    // Do any additional setup after loading the view, typically from a nib.
}

-(void)sliderValueChanged:(UISlider*)slider
{
    [_colorLabel setBackgroundColor:[UIColor colorWithRed:_r.value green:_g.value blue:_b.value alpha:1]];
}

please help if any one have some idea..

Upvotes: 3

Views: 3026

Answers (5)

Bhavesh Odedara
Bhavesh Odedara

Reputation: 79

  • First create 3 sliders in your Main.storyboard
  • Second create all sliders action ex:. Red_slider, Green_slider, Blue_slider
  • Third create 3 Labels and it's Outlets

Now in ViewCotroller

Enter code in ViewController.swift same as like Image

Upvotes: 0

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

For u Exothug

If slider value from 0 to 1 only

-(IBAction)sliderValue:(UISlider*)sender
{
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:1];
    _colorLabel.backgroundColor = newColor;
}

Upvotes: 0

Prince Kumar Sharma
Prince Kumar Sharma

Reputation: 12641

Use your setting color method as

-(void)sliderValueChanged:(UISlider*)slider
{
    float r=[[NSString stringWithFormat:@"%.0f",_r.value] floatValue];
    float g=[[NSString stringWithFormat:@"%.0f",_g.value]floatValue];
    float b=[[NSString stringWithFormat:@"%.0f",_b.value]floatValue];

    UIColor *colorToSet=[UIColor colorWithRed:(r/255.0f) green:(g/255.0f) blue:(b/255.0f) alpha:1];
    [_colorLabel setBackgroundColor:colorToSet];
}

Check Sample

Upvotes: 2

Exothug
Exothug

Reputation: 339

Try this (Ignore the alpha slider, this is my own actual solution to a similar task) :

KEEP IN MIND : let your sliders go from 0 to 1 to avoid the division through 255.

.h file :

IBOutlet UISlider *rSlider;
IBOutlet UISlider *gSlider;
IBOutlet UISlider *bSlider;

.m file

-(void)blueSlider:(id)sender
{
    bSlider = (UISlider *)sender;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}



-(void)greenSlider:(id)sender
{
    gSlider = (UISlider *)sender;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}


-(void)redSlider:(id)sender
{
    rSlider = (UISlider *)sender;
    UIColor *newColor = [UIColor colorWithRed:rSlider.value green:gSlider.value blue:bSlider.value alpha:alphaSlider.value];
    colorView.backgroundColor = newColor;
}

Upvotes: 3

Wain
Wain

Reputation: 119041

The UIColor methods take each component parameter in the range 0 to 1, so you need to divide your slider values by 255.0 before passing them.

Upvotes: 4

Related Questions