0

I'm converting RGB to Hexadecimal Colors using this code. I have 3 Edittext to input R, G and B. Now what I want to do is to convert it without using the API. Like converting and computing it with my own code and not by toHexString(). can someone help me to do that? Thanks a lot. Here is my code.

      convert.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                final int r = Integer.parseInt(showRed.getText().toString());
                final int g = Integer.parseInt(showGreen.getText().toString());
                final int b = Integer.parseInt(showBlue.getText().toString());

                final StringBuilder builder = new StringBuilder();
                builder.append("#");
                builder.append(Integer.toHexString(r)); // Real computation here
                builder.append(Integer.toHexString(g)); // Real computation here
                builder.append(Integer.toHexString(b)); // Real computation here

                result.setText(builder.toString());

            }
        });
jajaja
  • 389
  • 1
  • 12
  • 26

2 Answers2

4

Assuming r, g and b values in the range [0, 255]:

String hexColor = String.format( "#%02x%02x%02x", r, g, b );

Read java.util.Formatter javadoc for more formatting options.

Oleg Estekhin
  • 8,063
  • 5
  • 49
  • 52
  • this works but I want to use the real computation. Like with the operators and remainders etc.. Can you help me with that? :( – jajaja Apr 03 '14 at 06:53
2

So you want to do it without toHexString for some reason...

What you're looking for is a way to convert a base 10 number to a base 16 number. You want to code it yourself, but not actually code it yourself obviously, since you're here. Right?

Divide the number by 16. Convert the remainder to Hex if greater than 9 by;

if(number > 9) {
    switch(number) {
        case 10: string remainderPlaceholder = "A";
        break;

        case 11: string remainderPlaceholder = "B";
        break;

Etcetera, you get the idea.

Then take your quotient from the first part and, if greater than 16, divide it by 16 again, take the remainder and convert it to hex again using the selfsame switch statement mentioned above.

Lather, rinse, and repeat, appending each new answer to the front of the string, until you've got the full value.

Example: 255/16 = 15r15 15 (converted to hex using the above switch) = "F"; And again.

Now one more example, with the number 146:

146/16 = 9r2

Converting to hex: 92.

Let me know if you actually want me to write out the code for you. But if you do, you may as well just use toHexString() since it'll be more efficient than whatever I write for you, and since you wouldn't be writing the code yourself either way...

Edit: some pseudo-ish code so you can figure out how to code it:

int redMain = r/16;
int redRemainder = r%16;
//(do the same thing for green and blue)
int redFirst = redMain/16;

Then put the case statement like above into a function and call it on the two things in a row like

public void makeHex(number) {
    if(number > 9) {
        switch(number) {
            case 10: string remainderPlaceholder = "A";
            break;

            case 11: string remainderPlaceholder = "B";
            break;
//etcetera

Which you'd call like makeHex(redMain);

And then you just have to append the strings in the right order, so it would look like;

String redValue = makeHex(redMain).toString();
redValue = redValue + makeHex(redMain).toString();

And then put them all together with a hashtag like

String finalHex = "#" + redValue + greenValue + blueValue;

Not too hard.

But I would seriously just use toHexString since there are likely a bunch of things wrong with this way of doing it.

nilesblack
  • 109
  • 4