hex To Rgb - Java 2D Graphics

Java examples for 2D Graphics:Color RGB

Description

hex To Rgb

Demo Code

/**/*from w w w  .j  a v a 2  s  .  c  o  m*/
 * CamanJ - Java Image Manipulation
 * Ported from the CamanJS Javascript library
 *
 * Copyright 2011, Ryan LeFevre
 * Licensed under the new BSD License
 * See LICENSE for more info.
 * 
 * Project Home: http://github.com/meltingice/CamanJ
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        String hex = "java2s.com";
        System.out.println(java.util.Arrays.toString(hexToRgb(hex)));
    }

    public static int[] hexToRgb(String hex) {
        if (hex.charAt(0) == '#') {
            hex = hex.substring(1);
        }

        int[] rgb = new int[3];
        rgb[0] = Integer.parseInt(hex.substring(0, 2), 16);
        rgb[1] = Integer.parseInt(hex.substring(2, 4), 16);
        rgb[2] = Integer.parseInt(hex.substring(4, 6), 16);

        return rgb;
    }
}

Related Tutorials