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,318
  • 3
  • 25
  • 49

3 Answers3

3

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
  • 20,200
  • 11
  • 84
  • 98
Daryn
  • 770
  • 9
  • 19
2
int newRgb = 17 * (((oldRgb & 0xF00) << 8) | ((oldRgb & 0xF0) << 4) | (oldRgb & 0xF));
Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
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
  • 20,200
  • 11
  • 84
  • 98
GOLDEE
  • 2,318
  • 3
  • 25
  • 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