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 ww . j av a 2 s. com * Copyright 2012 Donghyuck, Son * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ 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; } } /** * Convert a String to an int. Truncates numbers if it's a float string; for * example, 4.5 yields a value of 4. * * @param in * String containing number to be parsed. * @return Integer value of number or 0 if error. * * @see #extractNumber(String) */ public final static int parseInt(String in) { int i; try { i = Integer.parseInt(in); } catch (Exception e) { i = (int) parseFloat(in); } ; return i; } /** * Convert a String to a float. * * @param in * String containing number to be parsed. * @return Float value of number or 0 if error. * * @see #extractNumber(String) */ public final static float parseFloat(String in) { float f = 0; try { f = Float.parseFloat(in); } catch (Exception e) { } ; return f; } }