Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set RGB value for label text

Tags:

uilabel

rgb

I am having the color codes as R:38 G:171 B:228 , but when I set the values as .38f in the color with Red : Green : Blue:, I am unable to get the desired color:

[CategoryLbl setTextColor:[UIColor colorWithRed:.38f green:.171f blue:.226f alpha:1.0f]];

Please help.

like image 364
Singh Avatar asked Apr 17 '12 11:04

Singh


People also ask

How can we set RGB value?

For example, rgb(255, 0, 0) is displayed as red, because red is set to its highest value (255) and the others are set to 0. To display black, set all color parameters to 0, like this: rgb(0, 0, 0). To display white, set all color parameters to 255, like this: rgb(255, 255, 255).

Why is RGB 0 to 255?

Since 255 is the maximum value, dividing by 255 expresses a 0-1 representation. Each channel (Red, Green, and Blue are each channels) is 8 bits, so they are each limited to 256, in this case 255 since 0 is included.

How do you change the text color of a label in Java?

You can set the color of a JLabel by altering the foreground category: JLabel title = new JLabel("I love stackoverflow!", JLabel. CENTER); title. setForeground(Color.


1 Answers

You're mixing up two scales: UIColour looks like it uses floating point values 0-1 whereas the usual RGB values are 0-255. Instead you want

 38 / 255 = 0.1491f
171 / 255 = 0.6706f
226 / 255 = 0.8863f

so

[CategoryLbl setTextColor:[UIColor colorWithRed:0.1491f green:0.6706f blue:0.8863f alpha:1.0f]];

There may be better ways to do this, e.g. using the 0-255 values - I don't know OSX / iPhone development well.

Actually it looks like you can just do:

[CategoryLbl setTextColor:[UIColor colorWithRed:(38/255.f) green:(171/255.f) blue:(226/255.f) alpha:1.0f]];

which is easier to understand (although I gave you enough d.p. the first one should be as accurate).

like image 179
Rup Avatar answered Oct 16 '22 00:10

Rup