1

I've the following html text (Exemple) :

<p>​<span style="color: rgb(33, 150, 243);">This is a <span style="background-color: rgb(255, 235, 59);">test !!</span></span></p>

I'm trying to display this text in a TextView.

textview.setText(Html.fromHtml(string));

It works, but display no colors.

For this to work, I have to convert the text in this form (with HEX colors)

<p>​<span style="color: #2196f3;">This is a <span style="background-color: #ffeb3b;">test !!</span></span></p>

How can I doi it?

I can not find the solution ... Has anyone ever had this problem and can help me?

Thanks in advance !

Joel
  • 192
  • 12
  • Do you have separate color variables in your code (int r; int g; int b;) or everything you have is this HTML string and you want to parse the values out of this string? – Krystian G Sep 04 '18 at 20:08
  • Everything is in the String. I get the String from a remote server, and I'm trying to display it correctly in a text view. – Joel Sep 04 '18 at 20:19

1 Answers1

6

Explanation in comments:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Stackoverflow52173062 {

    public static void main(final String[] args) {

        String html = "<p>​<span style=\"color: rgb(33, 150, 243);\">This is a <span style=\"background-color: rgb(255, 235, 59);\">test !!</span></span></p>";

        html = replaceRGBColorsWithHex(html);

        // final String
        System.out.println(html);
    }

    private static String replaceRGBColorsWithHex(String html) {
        // using regular expression to find all occurences of rgb(a,b,c) using
        // capturing groups to get separate numbers.
        Pattern p = Pattern.compile("(rgb\\(\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*\\))");
        Matcher m = p.matcher(html);

        while (m.find()) {
            // get whole matched rgb(a,b,c) text
            String foundRGBColor = m.group(1);
            System.out.println("Found: " + foundRGBColor);

            // get r value
            String rString = m.group(2);
            // get g value
            String gString = m.group(3);
            // get b value
            String bString = m.group(4);

            System.out.println(" separated r value: " + rString);
            System.out.println(" separated g value: " + gString);
            System.out.println(" separated b value: " + bString);

            // converting numbers from string to int
            int rInt = Integer.parseInt(rString);
            int gInt = Integer.parseInt(gString);
            int bInt = Integer.parseInt(bString);

            // converting int to hex value
            String rHex = Integer.toHexString(rInt);
            String gHex = Integer.toHexString(gInt);
            String bHex = Integer.toHexString(bInt);

            // add leading zero if number is small to avoid converting
            // rgb(1,2,3) to rgb(#123)
            String rHexFormatted = String.format("%2s", rHex).replace(" ", "0");
            String gHexFormatted = String.format("%2s", gHex).replace(" ", "0");
            String bHexFormatted = String.format("%2s", bHex).replace(" ", "0");

            System.out.println(" converted " + rString + " to hex: " + rHexFormatted);
            System.out.println(" converted " + gString + " to hex: " + gHexFormatted);
            System.out.println(" converted " + bString + " to hex:" + bHexFormatted);

            // concatenate new color in hex
            String hexColorString = "#" + rHexFormatted + gHexFormatted + bHexFormatted ;

            System.out.println("  replacing " + foundRGBColor + " with " + hexColorString);
            html = html.replaceAll(Pattern.quote(foundRGBColor), hexColorString);
        }
        return html;
    }
}
Milind Chaudhary
  • 1,632
  • 1
  • 17
  • 16
Krystian G
  • 2,842
  • 3
  • 11
  • 25
  • Works perfectly, just edit the following line : // concatenate new color in hex String hexColorString = "rgb(#" + rHexFormatted + gHexFormatted + bHexFormatted + ")"; to : String hexColorString = "#" + rHexFormatted + gHexFormatted + bHexFormatted; Thanks :-) – Joel Sep 04 '18 at 21:27
  • out of curiosity, can the opposite function be possible? (HEX to RGB) – Joel Sep 05 '18 at 04:50
  • 1
    Yes. That would require more or less reversing all these operations. You would have start by splitting "#rrggbb" into separate Strings: rr, gg, bb and converting each of them to decimal using `Integer.parseInt(rr, 16);` – Krystian G Sep 05 '18 at 10:44
  • I see what you mean, but I do not really understand how to find the colors in the string. I can convert only if the color is isolated from the rest of the string. if you have a solution you can post it also in response to the question. like that for future readers this subject will be complete :-) ' – Joel Sep 05 '18 at 18:39