ColorUtils.java Source code

Java tutorial

Introduction

Here is the source code for ColorUtils.java

Source

/*
 * ColorUtils.java Created Nov 17, 2010 by Andrew Butler, PSL
 */
//package prisms.util;

import java.awt.Color;

/** A set of tools for analyzing and manipulating colors */
public class ColorUtils {

    /**
     * Parses a java.awt.Color from an HTML color string in the form '#RRGGBB' where RR, GG, and BB
     * are the red, green, and blue bytes in hexadecimal form
     * 
     * @param htmlColor The HTML color string to parse
     * @return The java.awt.Color represented by the HTML color string
     */
    public static Color fromHTML(String htmlColor) {
        int r, g, b;
        if (htmlColor.length() != 7 || htmlColor.charAt(0) != '#')
            throw new IllegalArgumentException(htmlColor + " is not an HTML color string");
        r = Integer.parseInt(htmlColor.substring(1, 3), 16);
        g = Integer.parseInt(htmlColor.substring(3, 5), 16);
        b = Integer.parseInt(htmlColor.substring(5, 7), 16);
        return new Color(r, g, b);
    }
}