Convert Hex to RGB Color using Java

Integer.valueOf and substring methods

package app;

public class Main
{
    public static void main(String[] args)
    {
        String hexColor = "#8060c2";
        int r = Integer.valueOf(hexColor.substring(1, 3), 16);
        int g = Integer.valueOf(hexColor.substring(3, 5), 16);
        int b = Integer.valueOf(hexColor.substring(5, 7), 16);

        System.out.println(r + "," + g + "," + b);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.