1

I have to convert a color string in the format #RGB to #RRGGBB like #0af To #RRGGBB now can any one help out here using android

GOLDEE
  • 2,290
  • 3
  • 24
  • 49

3 Answers3

2

One way is:

String rgb = "#0AF";
String rrggbb = "#";
for (int i = 1; i < rgb.length(); i++) {
    rrggbb += (rgb.charAt(i) + "" + rgb.charAt(i));
}
Sergey Glotov
  • 19,939
  • 11
  • 82
  • 96
Daryn
  • 752
  • 8
  • 16
2
int newRgb = 17 * (((oldRgb & 0xF00) << 8) | ((oldRgb & 0xF0) << 4) | (oldRgb & 0xF));
Sergey Glotov
  • 19,939
  • 11
  • 82
  • 96
Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199
0
String hexColor = String.format("#%06X", (0xFFFFFF & intColor));

thats what i was searching. It will ensure that color string always will be #RRGGBB.

Sergey Glotov
  • 19,939
  • 11
  • 82
  • 96
GOLDEE
  • 2,290
  • 3
  • 24
  • 49
  • 1
    Oh really? I think it's obvious that this doesn't do what you asked for. – class stacker Apr 30 '13 at 08:38
  • @ClassStacker thanks for ur suggestions but i dont think u r a right person to tell me what i asked is wrong or right ...ok ...enjoy . \m/ – GOLDEE Apr 30 '13 at 12:06
  • Oh, am I not? That surprises me. Because Daryn and @Dmitry Bychenko spent their time on answers which are based on an interpretation of your question which matches mine. I'd call that a majority. But I presume you're just after as much free code as possible. And you're very young, that explains a lot. Farewell, my friend! – class stacker Apr 30 '13 at 12:22