Here you can find the source of hexToColor(String color)
Parameter | Description |
---|---|
color | the String (in RGB hexadecimal format) to convert |
public final static Color hexToColor(String color)
//package com.java2s; /*//from w w w .ja v a 2s. c o m * @(#)TextUtility.java * * Copyright (c) 2003 DCIVision Ltd * All rights reserved. * * This software is the confidential and proprietary information of DCIVision * Ltd ("Confidential Information"). You shall not disclose such Confidential * Information and shall use it only in accordance with the terms of the license * agreement you entered into with DCIVision Ltd. */ import java.awt.Color; public class Main { /** * Convert html hex string to Color. If the hexadecimal string is not * a valid character, <code>Color.black</code> is returned. * Only the first six hexadecimal characters are considered; any * extraneous values are discarded. Also, a leading "#", if any, is allowed * (and ignored). * @param color the String (in RGB hexadecimal format) to convert * @return the java.awt.Color */ public final static Color hexToColor(String color) { try { if (color.charAt(0) == '#') { color = color.substring(1, 7); } int[] col = new int[3]; for (int i = 0; i < 3; i++) { col[i] = Integer.parseInt(color.substring(i * 2, (i * 2) + 2), 16); } return new Color(col[0], col[1], col[2]); } catch (Exception e) { return Color.black; } } }