Class ColorUtil
- java.lang.Object
-
- org.jdesktop.swingx.color.ColorUtil
-
public final class ColorUtil extends java.lang.Object
A collection of utility methods for working withColor
s.- Author:
- joshua.marinacci@sun.com, Karl George Schaefer
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.awt.Color
blend(java.awt.Color origin, java.awt.Color over)
Blends two colors to create a new color.static java.awt.Color
computeForeground(java.awt.Color bg)
Computes an appropriate foreground color (either white or black) for the given background color.static java.awt.Paint
getCheckerPaint()
Creates a newPaint
that is a checkered effect using the colorsgray
andColor.WHITE
.static java.awt.Paint
getCheckerPaint(java.awt.Color c1, java.awt.Color c2, int size)
Creates a newPaint
that is a checkered effect using the specified colors.static java.awt.Color
interpolate(java.awt.Color b, java.awt.Color a, float t)
static java.awt.Color
removeAlpha(java.awt.Color color)
Returns a new color equal to the old one, except that there is no alpha (transparency) channel.static java.awt.Color
setAlpha(java.awt.Color color, int alpha)
Returns a new color equal to the old one, except alpha (transparency) channel is set to the new value.static java.awt.Color
setBrightness(java.awt.Color color, float brightness)
Returns a new color equal to the old one, except the brightness is set to the new value.static java.awt.Color
setSaturation(java.awt.Color color, float saturation)
Returns a new color equal to the old one, except the saturation is set to the new value.static void
tileStretchPaint(java.awt.Graphics g, javax.swing.JComponent comp, java.awt.image.BufferedImage img, java.awt.Insets ins)
Draws an image on top of a component by doing a 3x3 grid stretch of the image using the specified insets.static java.lang.String
toHexString(java.awt.Color color)
Creates aString
that represents the supplied color as a hex-value RGB triplet, including the "#".
-
-
-
Method Detail
-
removeAlpha
public static java.awt.Color removeAlpha(java.awt.Color color)
Returns a new color equal to the old one, except that there is no alpha (transparency) channel.This method is a convenience and has the same effect as
setAlpha(color, 255)
.- Parameters:
color
- the color to remove the alpha (transparency) from- Returns:
- a new non-transparent
Color
- Throws:
java.lang.NullPointerException
- ifcolor
isnull
-
setAlpha
public static java.awt.Color setAlpha(java.awt.Color color, int alpha)
Returns a new color equal to the old one, except alpha (transparency) channel is set to the new value.- Parameters:
color
- the color to modifyalpha
- the new alpha (transparency) level. Must be an int between 0 and 255- Returns:
- a new alpha-applied
Color
- Throws:
java.lang.IllegalArgumentException
- ifalpha
is not between 0 and 255 inclusivejava.lang.NullPointerException
- ifcolor
isnull
-
setSaturation
public static java.awt.Color setSaturation(java.awt.Color color, float saturation)
Returns a new color equal to the old one, except the saturation is set to the new value. The new color will have the same alpha (transparency) as the original color.The color is modified using HSB calculations. The saturation must be a float between 0 and 1. If 0 the resulting color will be gray. If 1 the resulting color will be the most saturated possible form of the passed in color.
- Parameters:
color
- the color to modifysaturation
- the saturation to use in the new color- Returns:
- a new saturation-applied
Color
- Throws:
java.lang.IllegalArgumentException
- ifsaturation
is not between 0 and 1 inclusivejava.lang.NullPointerException
- ifcolor
isnull
-
setBrightness
public static java.awt.Color setBrightness(java.awt.Color color, float brightness)
Returns a new color equal to the old one, except the brightness is set to the new value. The new color will have the same alpha (transparency) as the original color.The color is modified using HSB calculations. The brightness must be a float between 0 and 1. If 0 the resulting color will be black. If 1 the resulting color will be the brightest possible form of the passed in color.
- Parameters:
color
- the color to modifybrightness
- the brightness to use in the new color- Returns:
- a new brightness-applied
Color
- Throws:
java.lang.IllegalArgumentException
- ifbrightness
is not between 0 and 1 inclusivejava.lang.NullPointerException
- ifcolor
isnull
-
toHexString
public static java.lang.String toHexString(java.awt.Color color)
Creates aString
that represents the supplied color as a hex-value RGB triplet, including the "#". The return value is suitable for use in HTML. The alpha (transparency) channel is neither include nor used in producing the string.- Parameters:
color
- the color to convert- Returns:
- the hex
String
-
computeForeground
public static java.awt.Color computeForeground(java.awt.Color bg)
Computes an appropriate foreground color (either white or black) for the given background color.- Parameters:
bg
- the background color- Returns:
Color.WHITE
orColor.BLACK
- Throws:
java.lang.NullPointerException
- ifbg
isnull
-
blend
public static java.awt.Color blend(java.awt.Color origin, java.awt.Color over)
Blends two colors to create a new color. Theorigin
color is the base for the new color and regardless of its alpha component, it is treated as fully opaque (alpha 255).- Parameters:
origin
- the base of the new colorover
- the alpha-enabled color to add to theorigin
color- Returns:
- a new color comprised of the
origin
andover
colors
-
interpolate
public static java.awt.Color interpolate(java.awt.Color b, java.awt.Color a, float t)
-
getCheckerPaint
public static java.awt.Paint getCheckerPaint()
Creates a newPaint
that is a checkered effect using the colorsgray
andColor.WHITE
.- Returns:
- a the checkered paint
-
getCheckerPaint
public static java.awt.Paint getCheckerPaint(java.awt.Color c1, java.awt.Color c2, int size)
Creates a newPaint
that is a checkered effect using the specified colors.While this method supports transparent colors, this implementation performs painting operations using the second color after it performs operations using the first color. This means that to create a checkered paint with a fully-transparent color, you MUST specify that color first.
- Parameters:
c1
- the first colorc2
- the second colorsize
- the size of the paint- Returns:
- a new
Paint
checkering the supplied colors
-
tileStretchPaint
public static void tileStretchPaint(java.awt.Graphics g, javax.swing.JComponent comp, java.awt.image.BufferedImage img, java.awt.Insets ins)
Draws an image on top of a component by doing a 3x3 grid stretch of the image using the specified insets.TODO this is image related; move to GraphicsUtilities
-
-