Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
288 views
in Technique[技术] by (71.8m points)

uibutton - How to initialize UIColor from RGB values properly?

I am working on an iPhone application which uses various colors. When user selects the particular color button I set drawing color accordingly. I am getting the color for some but in the most of the cases I am getting white color.

Here is my code:

-(IBAction)colorSelected:(UIButton *)sender
{

switch (sender.tag) 
{
    case 1:
        self.drawcolor= [UIColor colorWithRed:200 green:191 blue:231 alpha:1];
        break;            
    case 2:
        self.drawcolor= [UIColor colorWithRed:163 green:73 blue:164 alpha:1];
        break;  
    case 3:
        self.drawcolor= [UIColor colorWithRed:112 green:146 blue:76 alpha:1];
        break;  
    case 4:
        self.drawcolor= [UIColor colorWithRed:63 green:72 blue:204 alpha:1];
        break;  
    case 5:
        self.drawcolor= [UIColor colorWithRed:153 green:217 blue:234 alpha:1];
        break;  
    case 6:
        self.drawcolor= [UIColor colorWithRed:0 green:162 blue:232 alpha:1];
        break;          
    case 7:
        self.drawcolor= [UIColor colorWithRed:181 green:230 blue:29 alpha:1];
        break;  
    case 8:
        self.drawcolor= [UIColor colorWithRed:34 green:177 blue:76 alpha:1];
        break;          
    case 9:
        self.drawcolor= [UIColor colorWithRed:239 green:228 blue:176 alpha:1];
        break;          
    case 10:
        self.drawcolor= [UIColor colorWithRed:255 green:201 blue:0 alpha:1];
        break;          
    case 11:
        self.drawcolor= [UIColor colorWithRed:255 green:201 blue:14 alpha:1];
        break;          
    case 12:
        self.drawcolor= [UIColor colorWithRed:237 green:28 blue:36 alpha:1];
        break;          
    case 13:
        self.drawcolor= [UIColor colorWithRed:255 green:127 blue:39 alpha:1];
        break;          
    case 14:
        self.drawcolor= [UIColor colorWithRed:255 green:174 blue:201 alpha:1];
        break;          
    case 15:
        self.drawcolor= [UIColor colorWithRed:185 green:122 blue:87 alpha:1];
        break;          
    case 16:
        self.drawcolor= [UIColor colorWithRed:136 green:0 blue:21 alpha:1];
        break;          
    case 17:
        self.drawcolor= [UIColor colorWithRed:195 green:195 blue:195 alpha:1];
        break;          
    case 18:
        self.drawcolor= [UIColor colorWithRed:127 green:127 blue:127 alpha:1];
        break;          
    case 19:
        self.drawcolor= [UIColor colorWithRed:255 green:255 blue:255 alpha:1];
        break;
    case 20:
        self.drawcolor= [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        break;  
    default:
        break;
}

self.preColor=self.drawcolor;
self.lineWidth=self.prelineWidth;
}

Can any one tell me what I am doing wrong ? Sumit

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The values are in the 0.0 to 1.0 range.

E.g. divide by 255., but remember the decimal dot so you get floating point division and not integer division.

Like

selectedColor = [UIColor colorWithRed:14.0/255.0 green:114.0/255.0 blue:199.0/255.0 alpha:1];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...