com.woven_media.util.color
Class ColorUtil

java.lang.Object
  extended by com.woven_media.util.color.ColorUtil

public class ColorUtil
extends java.lang.Object

ColorUtil is a static class for calculating color conversions and color models.

Since:
1.0
Author:
Brent Allen Parrish

Constructor Summary
ColorUtil()
           
 
Method Summary
static int[] buildRGBarray(java.lang.String hex)
          Builds an array of RGB values based on a hexadecimal triplet.
static double[] cmykAsPercent(int c, int m, int y, int k)
          Converts CMYK values between 0 - 255 to values between 0 - 100 that can be expressed as percentages.
static int[] cmykToRGB(int c, int m, int y, int k)
          Converts CMYK values to RGB.
static int getBlue(java.lang.String blue)
          Converts strings representing Blue RGB values to integers.
static int getGreen(java.lang.String green)
          Converts strings representing Green RGB values to integers.
static java.lang.String[] getHSBvalues(int r, int g, int b)
          Get HSB values from RGB color model.
static java.awt.Color getNearestSafeColor(int[] rgbArray)
          Returns a java.awt.Color object representing the nearest safe color.
static int getRed(java.lang.String red)
          Converts strings representing Red RGB values to integers.
static java.awt.Color getRGBColor(java.lang.String hex)
          Returns a RGB java.awt.Color object by parsing the hex string parameter.
static java.awt.Color getRGBColor(java.lang.String red, java.lang.String grn, java.lang.String blu)
          Returns a java.awt.Color object from RGB strings.
static int HEXtoRGB(java.lang.String hex)
          Convenience routine that converts individual triplet values of a hexadecimal string to an integer.
static float[] HSVtoRGB(float r, float g, float b, float h, float s, float v)
          Converts to RGB color model from HSV model.
static boolean isColorSafe(java.lang.String hex)
          Determines whether a hexadecimal string is a safe color equivalent.
static boolean isValidHex(java.lang.String hex)
          Tests whether a string is a valid hexadecimal value.
static boolean isValidString(java.lang.String inString, java.lang.String filterString)
          Tests whether a string contains the required characters.
 int luminance(float r, float g, float b)
          Grab the luminance the same way GIMP does.
static int[] nearestSafeColor(int[] rgbArray)
          Algorithm for determining the nearest safe color based on the values of a valid RGB array of unsigned integer values.
static int[] parseAWTColor(java.awt.Color c)
          Parses a java.awt.Color object and returns an array of RGB integers.
static java.lang.String[] parseHex(java.lang.String hex)
          Returns a java.lang.String array of the seperate red, green and blue hexadecimal values and checks for the abbreviated form.
static int[] rgbToCMYK(int red, int grn, int blu)
          Approximation algorithm only for CMYK color! Converts RGB values to CMYK array of values.
static java.lang.String RGBtoHEX(int red, int green, int blue)
          Converts RGB integer values to a hexadecimal triplet and return as java.lang.String.
static float[] RGBtoHSV(float r, float g, float b, float h, float s, float v)
          Converts to HSV color model from RGB model.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ColorUtil

public ColorUtil()
Method Detail

RGBtoHEX

public static java.lang.String RGBtoHEX(int red,
                                        int green,
                                        int blue)
Converts RGB integer values to a hexadecimal triplet and return as java.lang.String.

Parameters:
red - int value representing red.
green - int value representing green.
blue - int value representing blue.
Since:
1.0

isValidHex

public static boolean isValidHex(java.lang.String hex)
Tests whether a string is a valid hexadecimal value.

Parameters:
hex - string representing a hexadecimal triplet number.
Returns:
boolean value indicating a valid or invalid hexadecimal string.
Since:
1.0

isValidString

public static boolean isValidString(java.lang.String inString,
                                    java.lang.String filterString)
Tests whether a string contains the required characters.

Parameters:
inString - string to check.
filterString - string of required characters.
Returns:
boolean value indicating a valid or invalid string.
Since:
1.0

getRGBColor

public static java.awt.Color getRGBColor(java.lang.String hex)
Returns a RGB java.awt.Color object by parsing the hex string parameter. An abbreviated, three (hexadecimal) digit form can be used, as well as the standard six-digit form.

e.g.

09C becomes 0099CC
FFF becomes FFFFFF
CDA becomes CCDDAA

Parameters:
hex - string representing a hexadecimal triplet or abbreviated triplet.
Returns:
java.awt.Color object created from the hex string equivalent.
Since:
1.0
See Also:
parseHex(java.lang.String)

parseHex

public static java.lang.String[] parseHex(java.lang.String hex)
Returns a java.lang.String array of the seperate red, green and blue hexadecimal values and checks for the abbreviated form. If the hex parameter is an abbreviated form, a six-digit standard form hexadecimal triplet is created and returned as an array of strings.

Parameters:
hex - hexadecimal triplet string, either six-digit or three-digit form.
Returns:
string array of the seperate red, green and blue hexadecimal triplet values, or an empty array if an invalid hex parameter is passed to the function.
Since:
1.0
See Also:
getRGBColor(java.lang.String)

getRGBColor

public static java.awt.Color getRGBColor(java.lang.String red,
                                         java.lang.String grn,
                                         java.lang.String blu)
Returns a java.awt.Color object from RGB strings.

Parameters:
red - string representing red.
grn - string representing green.
blu - string representing blue.
Returns:
java.awt.Color object created from RGB string values.
Since:
1.0
See Also:
NumberFormatException, Integer

isColorSafe

public static boolean isColorSafe(java.lang.String hex)
Determines whether a hexadecimal string is a safe color equivalent.

Parameters:
hex - string representing a hexadecimal triplet number.
Returns:
boolean value indicating whether a color is a safe color or not.
Since:
1.0

buildRGBarray

public static int[] buildRGBarray(java.lang.String hex)
Builds an array of RGB values based on a hexadecimal triplet.

Parameters:
hex - string representing a hexadecimal triplet number.
Returns:
int array of RGB values.
Since:
1.0

nearestSafeColor

public static int[] nearestSafeColor(int[] rgbArray)
Algorithm for determining the nearest safe color based on the values of a valid RGB array of unsigned integer values.

Parameters:
rgbArray - int array of representing RGB values.
Returns:
int array of RGB values converted to a safe color equivalent.
Since:
1.0

rgbToCMYK

public static int[] rgbToCMYK(int red,
                              int grn,
                              int blu)
Approximation algorithm only for CMYK color! Converts RGB values to CMYK array of values. Values are between 0 - 255.

Parameters:
red - int value between (0-255) representing red.
grn - int value between (0-255) representing green.
blu - int value between (0-255) representing blue.
Returns:
int array of CMYK values converted from RGB.
Since:
1.0

parseAWTColor

public static int[] parseAWTColor(java.awt.Color c)
Parses a java.awt.Color object and returns an array of RGB integers.

Parameters:
c - java.awt.Color object.
Returns:
int array of RGB values converted from a java.awt.Color object.
Since:
1.0
See Also:
StringTokenizer, StringBuffer

cmykAsPercent

public static double[] cmykAsPercent(int c,
                                     int m,
                                     int y,
                                     int k)
Converts CMYK values between 0 - 255 to values between 0 - 100 that can be expressed as percentages.

Parameters:
c - int representing cyan.
m - int representing magenta.
y - int representing yellow.
k - int representing black.
Returns:
double array of CMYK percentages.
Since:
1.0
See Also:
NumberFormat

cmykToRGB

public static int[] cmykToRGB(int c,
                              int m,
                              int y,
                              int k)
Converts CMYK values to RGB.

Parameters:
c - int representing cyan.
m - int representing magenta.
y - int representing yellow.
k - int representing black.
Returns:
int array of RGB values converted from CMYK.
Since:
1.0

getHSBvalues

public static java.lang.String[] getHSBvalues(int r,
                                              int g,
                                              int b)
Get HSB values from RGB color model.

Parameters:
r - int representing red.
g - int representing green.
b - int representing blue.
Returns:
string array of HSB values converted from RGB.
Since:
1.0

getNearestSafeColor

public static java.awt.Color getNearestSafeColor(int[] rgbArray)
Returns a java.awt.Color object representing the nearest safe color.

Parameters:
rgbArray - array of valid RGB values.
Returns:
java.awt.Color object
Since:
1.0

HEXtoRGB

public static int HEXtoRGB(java.lang.String hex)
Convenience routine that converts individual triplet values of a hexadecimal string to an integer. e.g. HEXtoRGB("FF") not HEXtoRGB("FFFFFF")

Parameters:
hex - string representing a single hexadecimal triplet number.
Returns:
int value of either red, green or blue.
Since:
1.0

getRed

public static int getRed(java.lang.String red)
Converts strings representing Red RGB values to integers.

Parameters:
red - string representing red
Returns:
int value representing red converted from a string.
Since:
1.0

getGreen

public static int getGreen(java.lang.String green)
Converts strings representing Green RGB values to integers.

Parameters:
green - string representing green.
Returns:
int value representing green converted from a string.
Since:
1.0

getBlue

public static int getBlue(java.lang.String blue)
Converts strings representing Blue RGB values to integers.

Parameters:
blue - string representing blue.
Returns:
int value representing blue converted from a string.
Since:
1.0

luminance

public int luminance(float r,
                     float g,
                     float b)
Grab the luminance the same way GIMP does.

i.e. Math.round(0.3 * r + 0.59 * g + 0.11 * b)

Returns:
int value representing the luminance.
Since:
1.0

RGBtoHSV

public static float[] RGBtoHSV(float r,
                               float g,
                               float b,
                               float h,
                               float s,
                               float v)
Converts to HSV color model from RGB model.

Parameters:
r - represents red as a float value [0 - 1]
g - represents green as a float value [0 - 1]
b - represents blue as a float value [0 - 1]
h - represents hue as a float value [0,360]
s - represents saturation as a float value [0,1]
v - represents value as a float value [0,1]
Returns:
float array of HSV values converted from RGB.
Since:
1.0

HSVtoRGB

public static float[] HSVtoRGB(float r,
                               float g,
                               float b,
                               float h,
                               float s,
                               float v)
Converts to RGB color model from HSV model.

Parameters:
r - represents red as a float value [0 - 1]
g - represents green as a float value [0 - 1]
b - represents blue as a float value [0 - 1]
h - represents hue as a float value [0,360]
s - represents saturation as a float value [0,1]
v - represents value as a float value [0,1]
Returns:
float array of RGB values converted from HSV.
Since:
1.0