Convert an html-color-code to an AWT-color. - Java 2D Graphics

Java examples for 2D Graphics:Color HTML

Description

Convert an html-color-code to an AWT-color.

Demo Code

/**/*from  w w  w . j  av  a  2 s .  c o  m*/
 * This file is part of OSMNavigation by Marcus Wolschon <a href="mailto:Marcus@Wolscon.biz">Marcus@Wolscon.biz</a>.
 * You can purchase support for a sensible hourly rate or
 * a commercial license of this file (unless modified by others) by contacting him directly.
 *
 *  OSMNavigation is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LibOSM is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OSMNavigation.  If not, see <http://www.gnu.org/licenses/>.
 *
 ***********************************
 * Editing this file:
 *  -For consistent code-quality this file should be checked with the
 *   checkstyle-ruleset enclosed in this project.
 *  -After the design of this file has settled it should get it's own
 *   JUnit-Test that shall be executed regularly. It is best to write
 *   the test-case BEFORE writing this class and to run it on every build
 *   as a regression-test.
 */
//package com.java2s;
import java.awt.Color;

public class Main {
    /**
     * 16.
     */
    private static final int HEX = 16;

    /**
     * Convert an html-color-code to an AWT-color.
     * @param html the code
     * @return the color it represents or null
     */
    public static Color html2color(final String html) {
        String code = html;
        if (code.length() > 0 && code.charAt(0) == '#')
            code = code.substring(1);
        if (code.length() != (2 + 2 + 2))
            return null;
        try {
            return new Color(Integer.parseInt(code.substring(0, 2), HEX),
                    Integer.parseInt(code.substring(2, 2 + 2), HEX),
                    Integer.parseInt(code.substring(2 + 2, 2 + 2 + 2), HEX));
        } catch (NumberFormatException e) {
            return null;
        }
    }
}

Related Tutorials