1

I want to generate same Random color for selected cell and background cell. I have successfully implemented it with flowing code . but with this i am getting cell.background color correctly but i want to set light color on each indexpath of selected cell. so, how can i do it ? suppose ,

if (indexpath.row ==1) {
    // lightcolor       
}
if (indexpath.row == 2) {
    // More light color as compare to indexpath.row ==1
}

@interface DisplayViewController () {
    DisplayCollectionViewCell *cell;
    float randomRed,randomGreen,randomBlue;
}

- (void)viewDidLoad {    
    randomRed = arc4random() % 255;
    randomGreen = arc4random() % 255;
    randomBlue = arc4random() % 255;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DisplayCellIphone" forIndexPath:indexPath];

    if (indexPath.row == _setRandomIndex) {
        // For Selected Color
        cell.layer.backgroundColor  =  [UIColor colorWithRed:randomRed * 0.9 /255.0 green:randomGreen * 0.9 /255.0 blue:randomBlue * 0.9/255.0 alpha:1].CGColor;
    } else {
       cell.backgroundColor = [UIColor colorWithRed:randomRed/255.0 green:randomGreen/255.0 blue:randomBlue/255.0 alpha:1.0];
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    _count ++;
    randomRed = arc4random() % 255;
    randomGreen = arc4random() % 255;
    randomBlue = arc4random() % 255;

    if (indexPath.row == _setRandomIndex) {
        if (_count == 0) {
            [self addData:4];
        }      
        NSLog(@"Selected Right Cell");

        if (_count == 1 || _count == 2) {
            _setRandomIndex = arc4random() % 6;
            [self addData:6];            
           // _setRandomIndex = 2;          
        }
    // next cell till 20;
   }
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
Badal Shah
  • 7,541
  • 2
  • 30
  • 65
  • I guess you should set Alpha property of the color randomly. Isn't it fixes this problem ? – Sabrican Ozan Sep 09 '15 at 13:09
  • @SabricanOzan thanks for the replay . i tried it but it not apply for all colors . some cell looks white . i have used random colors. so . – Badal Shah Sep 09 '15 at 13:11
  • Well if you set some color's alpha to 0 it will be go white. You may try to run your random function in interval of 0.4- 0.8 Alpha values. That way no cell will be white or black. – Sabrican Ozan Sep 09 '15 at 13:13
  • @SabricanOzan yes i tried it to set alfa between 0.4 to 0.8. – Badal Shah Sep 09 '15 at 13:14

2 Answers2

0

You can use [UIColor colorWithHue: saturation: brightness: alpha:]

and change the brightness and saturation values. Set random values for the parameters, and using the answer in this post get components of your UIColor and generate colors by changing only the brightness and saturation values keeping hue value constant

Is there function to convert UIColor to Hue Saturation Brightness?

Community
  • 1
  • 1
Vinay Jain
  • 1,653
  • 20
  • 28
0

First keep a UIColor property (I named it cellColor) and use the code below

if (indexpath.row ==1) {
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //  0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //  0.5 to 1.0, away from black
    self.cellColor = [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];    
}
if (indexpath.row == 2) {
    CGFloat hue, saturation, brightness, alpha;
    BOOL success = [self.cellColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
    self.cellColor = [self.cellColor initWithHue:hue saturation:saturation/2 brightness:brightness alpha:alpha;
}

Now, in indexPath 1 condition, you create a random color. And in indexPath 2 condition, I got the hue and saturation values and changed the saturation value for random color to make brighter. I hope this could help you.

Candost
  • 1,029
  • 1
  • 12
  • 28
  • what is bool success ? – Badal Shah Sep 09 '15 at 13:54
  • getHue:saturation:brightness:alpha method's explanation: Returns the components that make up the color in the HSB color space. If the color is in a compatible color space, the color is converted into the HSB color space and its components are returned to your application. If the color is not in a compatible color space, the parameters unchanged. BOOL means: YES if the color could be converted, NO otherwise. – Candost Sep 09 '15 at 13:56
  • no buddy solution doesn't worked for me . some time cell's color will be white . – Badal Shah Sep 10 '15 at 04:51
  • It generates random color. Try to change saturation/2 part as you wish. (for instance (2*saturation/3) – Candost Sep 10 '15 at 06:41
  • yes you are right but i want to set more and more lighter when user click on cell. and all colors are come random . and at the end it will be complex. user can't identify the difference between selected cell and background cell . – Badal Shah Sep 10 '15 at 07:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89231/discussion-between-badal-shah-and-candost-dagdeviren). – Badal Shah Sep 10 '15 at 07:32