daspianist
daspianist

Reputation: 5525

UIColors with RGBs showing on screen as basic colors

I have made a dictionary of colors using the [UIColor colorWithRed: green: blue: alpha: ] function. However, when called and shown on screen, they look nothing like the actual colors, but rather resembling the basic [UIColor yellowColor] functions. Is there a library that I should be including in order to show the actual shades of RGB colors?

For reference, here is the dictionary I made:

colorStorage = [NSDictionary dictionaryWithObjectsAndKeys:
                    [UIColor colorWithRed:229.0 green:43.0 blue:80.0 alpha:1.0], @"Amaranth",
                    [UIColor colorWithRed:225 green:191 blue:0 alpha:1.0], @"Amber",
                    [UIColor colorWithRed:239 green:222 blue:205 alpha:1.0], @"Almond",
                    [UIColor colorWithRed:205 green:149 blue:117 alpha:1.0], @"Antique brass",
                    [UIColor colorWithRed:0 green:128 blue:0 alpha:1.0], @"Ao",
                    [UIColor colorWithRed:141 green:182 blue:0 alpha:1.0], @"Apple green",
                    [UIColor colorWithRed:251 green:206 blue:177 alpha:1.0], @"Apricot",
                    [UIColor colorWithRed:135.0 green:169.0 blue:107.0 alpha:1.0], @"Asparagus",
                    [UIColor colorWithRed:0 green:127 blue:255 alpha:1.0], @"Azure",
                    [UIColor colorWithRed:225 green:32 blue:82 alpha:1.0], @"Awesome",
                    [UIColor colorWithRed:178 green:190 blue:181 alpha:1.0], @"Ash grey",
                    [UIColor colorWithRed:110 green:127 blue:128 alpha:1.0], @"AutoMetalSaurus", nil];

And they are called as such:

for (id key in colorStorage){
        UIButton *colorButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [colorButton setTitle:key forState:UIControlStateNormal];

        //additional code here...

        colorButton.backgroundColor = [colorStorage objectForKey:key];
        colorButton.tag = i;
        [self.colorPicker addSubview:colorButton];
        i = i + 1;
    }

Upvotes: 1

Views: 491

Answers (1)

klcjr89
klcjr89

Reputation: 5902

As stated, always divide RGB values by 255.0. I did this mistake too when first learning.

colorStorage = [NSDictionary dictionaryWithObjectsAndKeys:
                [UIColor colorWithRed:229.0/255.0 green:43.0/255.0 blue:80.0/255.0 alpha:1.0], @"Amaranth",
                [UIColor colorWithRed:225.0/255.0 green:191.0/255.0 blue:0.0/255.0 alpha:1.0], @"Amber",
                [UIColor colorWithRed:239.0/255.0 green:222.0/255.0 blue:205.0/255.0 alpha:1.0], @"Almond",
                [UIColor colorWithRed:205.0/255.0 green:149.0/255.0 blue:117.0/255.0 alpha:1.0], @"Antique brass",
                [UIColor colorWithRed:0.0/255.0 green:128.0/255.0 blue:0.0/255.0 alpha:1.0], @"Ao",
                [UIColor colorWithRed:141.0/255.0 green:182.0/255.0 blue:0.0/255.0 alpha:1.0], @"Apple green",
                [UIColor colorWithRed:251.0/255.0 green:206.0/255.0 blue:177.0/255.0 alpha:1.0], @"Apricot",
                [UIColor colorWithRed:135.0/255.0 green:169.0/255.0 blue:107.0/255.0 alpha:1.0], @"Asparagus",
                [UIColor colorWithRed:0.0/255.0 green:127.0/255.0 blue:255.0/255.0 alpha:1.0], @"Azure",
                [UIColor colorWithRed:225.0/255.0 green:32.0/255.0 blue:82.0/255.0 alpha:1.0], @"Awesome",
                [UIColor colorWithRed:178.0/255.0 green:190.0/255.0 blue:181.0/255.0 alpha:1.0], @"Ash grey",
                [UIColor colorWithRed:110.0/255.0 green:127.0/255.0 blue:128.0/255.0 alpha:1.0], @"AutoMetalSaurus", nil];

Upvotes: 3

Related Questions