public final class PdColor extends PsObject
The class is derived from PsObject rather than java.awt.Color since the focus of this class is the update functionality and handling of an info panel for interactive modifications. This class allows interactive modification of a color similar to the class PuDouble and PuInteger allow for double and integer numbers. Also, java.awt.Color cannot be modified after construction.
PdColor_IP
,
PdColor_Dialog
,
Serialized FormHAS_BOUNDARY_PANEL, HAS_CONFIG_PANEL, HAS_INFO_PANEL, HAS_LABEL_PANEL, HAS_MATERIAL_PANEL, HAS_TEXTURE_PANEL, HAS_VECTOR_PANEL, INSPECTOR_INFO, INSPECTOR_INFO_EXT, IS_DELETED, IS_FIXED, IS_FOCUSSED, IS_PICKED, IS_SELECTED, IS_USED, NUM_TAGS
Constructor and Description |
---|
PdColor()
Constructor of color in RGB mode with alpha disabled.
|
PdColor(int size,
boolean bAlpha)
Constructor of color in RGB mode with number of color components and possible alpha channel.
|
PdColor(java.lang.String name,
PsUpdateIf parent)
Constructor of color in RGB mode with alpha disabled.
|
Modifier and Type | Method and Description |
---|---|
static java.awt.Color |
blend(double a,
java.awt.Color v,
double b,
java.awt.Color w)
Interpolate between two colors with given weights.
|
static int |
blend(double a,
int v,
double b,
int w)
Interpolate between two colors with given weights.
|
void |
blend(double a,
PdColor v,
double b,
PdColor w)
Interpolate between two colors with given weights.
|
void |
blendBase(PdColor v,
double b,
PdColor w)
Interpolate between two colors with given weights.
|
static java.awt.Color |
brighter(java.awt.Color color)
Modified against official Java implementation to
ensure that the brighter color is visually different.
|
java.lang.Object |
clone()
Duplicate color and data array.
|
static java.awt.Color |
cmyk2rgb(int c,
int m,
int y,
int k)
Convert a set of (cyan, magenta, yellow, black) to RGB color.
|
static boolean |
copy(java.awt.Color[] dataDest,
int destInd,
java.awt.Color[] dataSrc,
int srcInd,
int size)
Assign some colors of a given array of colors to an existing destination array.
|
void |
copy(double[] anArray,
int aSize)
Copy color components excluding alpha.
|
void |
copy(int[] anArray,
int aSize)
Copy color components excluding alpha.
|
void |
copy(PdColor v)
Copy color components including alpha.
|
static boolean |
copy(PdColor[] dataDest,
int destInd,
PdColor[] dataSrc,
int srcInd,
int size)
Copies some colors of a given array of colors into an existing destination array.
|
static java.awt.Color[] |
copyNew(java.awt.Color[] data)
Create an new array with copies of all colors of a given array of colors.
|
static java.awt.Color[] |
copyNew(java.awt.Color[] data,
int size)
Create an new array with copies of the first 'size' colors of a given array of colors.
|
static PdColor |
copyNew(PdColor v)
Create a new color as clone of argument color.
|
static PdColor[] |
copyNew(PdColor[] data)
Create an new array with copies of all colors of a given array of colors.
|
static PdColor[] |
copyNew(PdColor[] data,
int size)
Create an new array with copies of the first 'size' colors of a given array of colors.
|
void |
enableAlpha(boolean flag)
Enable or disable alpha channel.
|
boolean |
equals(java.awt.Color col)
Determines whether another color is equal to this color.
|
boolean |
equals(PdColor col)
Determines whether another color is equal to this color.
|
int |
getAlpha()
Get alpha component if alpha is enabled, otherwise result is 0.
|
int |
getBlue()
Get blue color component if
size==3 , otherwise result is 0. |
java.awt.Color |
getColor()
Convert color components in a java.awt.Color instance.
|
static int |
getColor(int a,
int[] rgb)
Convert RGBA color components in range [0,255] to integer color.
|
static int |
getColor(int a,
int r,
int g,
int b)
Convert RGBA color components in range [0,255] to integer color.
|
static java.awt.Color |
getColorFromName(java.lang.String color)
Convert a verbal color descriptor into a color instance.
|
static java.awt.Color |
getDimmedColor(java.awt.Color col,
double dim)
Get a color whose components are dimmed by a factor.
|
static java.awt.Color |
getDimmedColor(java.awt.Color col,
double dim,
double offset)
Get a color whose components are dimmed by a factor and shifted by an offset.
|
static java.awt.Color |
getDimmedColor(java.awt.Color col,
int dim)
Get a color whose components are dimmed by a factor.
|
static int |
getDimmedColor(int[] rgb,
int dim)
Get a color whose components are dimmed by a factor.
|
static int |
getDimmedColor(int rgb,
double dim)
Get a color whose components are dimmed by a factor.
|
static int |
getDimmedColor(int rgb,
double dim,
double offset)
Get a color whose components are dimmed by a factor and shifted by an offset.
|
static int |
getDimmedColor(int rgb,
int dim)
Get a color whose components are dimmed by a factor.
|
int |
getEntry(int ind)
Get a color component with given index.
|
int |
getGreen()
Get green color component if
size==3 , otherwise result is 0. |
int |
getGrey()
Get grey color if
size==1 , otherwise result is 0. |
static int |
getMax(int rgb)
Get maximal value of all components.
|
static int |
getMin(int rgb)
Get minimal value of all components.
|
int |
getRed()
Get red color component if
size==3 , otherwise result is 0. |
int |
getSize()
Return number of components excluding alpha.
|
static java.awt.Color |
getXORColor(java.awt.Color colCurr,
java.awt.Color colPref,
java.awt.Color colAlt,
int eps)
Choose a new color depending on the distance to an existing color.
|
boolean |
hasAlpha()
Check whether alpha channel is used.
|
static java.awt.Color |
hsl2rgb(int hi,
int li,
int si) |
static java.awt.Color |
hsv2rgb(java.awt.Color hsv)
Conversion of color spaces.
|
static java.awt.Color |
hsv2rgb(int h,
int s,
int v)
Conversion of color spaces.
|
static void |
hsv2rgb(PdColor rgb,
PdColor hsv)
Conversion of color spaces.
|
static int |
hsv2rgbAsInt(int h,
int s,
int v)
Conversion of color spaces.
|
int |
indexOfMax()
Find component in color with maximal value.
|
int |
indexOfMin()
Find component in color with minimal value.
|
void |
init()
Initialize and reset components to 0.
|
boolean |
isEnabled()
Check whether color dialog is enabled or disabled.
|
int |
max()
Find maximal value of component in color.
|
int |
min()
Find minimal value of component in color.
|
static java.awt.Color |
parseColor(java.lang.String colorString)
Parse rgb color from a given string where each component is
integer in the range 0..255 or a float in the range [0.,1.]
|
static java.awt.Color |
parseNamedColor(java.lang.String str)
Parse a named color string into a java.awt.Color.
|
static java.awt.Color[] |
realloc(java.awt.Color[] data,
int arraySize)
Allocate an array of Java colors and assign default color black.
|
static PdColor[] |
realloc(PdColor[] data,
int arraySize,
int colorSize)
Allocate an array of colors, where all colors have the same dimension.
|
static PdColor[] |
realloc(PdColor[] data,
int arraySize,
int colorSize,
boolean bAlpha)
Allocate an array of colors, where all colors have the same dimension.
|
static int[] |
rgb2cmyk(int r,
int g,
int b)
Convert a set of (cyan, magenta, yellow, black) to RGB color.
|
static java.awt.Color |
rgb2hsl(int ri,
int gi,
int bi) |
static void |
rgb2hsv(PdColor hsv,
PdColor rgb)
Conversion of color spaces.
|
static java.awt.Color |
rgb2yiq(int ri,
int gi,
int bi) |
void |
set(double grey)
Set grey component of this color using double arguments in [0., 1.].
|
void |
set(double red,
double green,
double blue)
Set components of this color using double arguments in [0., 1.].
|
void |
set(double red,
double green,
double blue,
double alpha)
Set components of this color using double arguments in [0., 1.].
|
void |
set(int grey)
Set grey component of this color using integer argument in [0, 255].
|
void |
set(int red,
int green,
int blue)
Set components of this color using integer argument in [0, 255].
|
void |
set(int red,
int green,
int blue,
int alpha)
Set components of this color using double arguments in [0, 255].
|
void |
setAlpha(double alpha)
Set alpha component of this color using double argument in [0., 1.].
|
void |
setAlpha(int alpha)
Set alpha component of this color using integer argument in [0, 255].
|
void |
setColor(java.awt.Color aColor)
Set color components to components of argument color.
|
void |
setConstant(int value)
Set all color components excluding alpha to the same value.
|
void |
setEnabled(boolean flag)
Enable or disable the color dialog for interactive use.
|
void |
setEntry(int ind,
double value)
Set a color component with given index to value.
|
void |
setEntry(int ind,
int value)
Set a color component with given index to value.
|
void |
setSize(int aSize)
Set number of components of color excluding alpha.
|
static java.lang.String |
toString(java.awt.Color col,
java.lang.String sep)
Get string representation of color components with given separator string.
|
static java.lang.String |
toStringAsFloat(java.awt.Color col,
java.lang.String sep)
Get string representation of color components with given separator string
and range of color components scaled to interval [0.,1.].
|
static java.awt.Color |
yiq2rgb(int yi,
int ii,
int qi) |
addInspector, addUpdateListener, assureInspector, clearTag, clone, clone, copy, getFather, getInfoPanel, getInspector, getName, getNumObjects, getSymbol, hasInspector, hasTag, hasUpdateListener, instanceOf, instanceOf, newInspector, newInspector, removeInspector, removeInspector, removeUpdateListener, setName, setParent, setSymbol, setTag, toString, update, updatePanels
public PdColor()
public PdColor(int size, boolean bAlpha)
size
- number of color components excluding alphabAlpha
- true
to enable alpha channelpublic PdColor(java.lang.String name, PsUpdateIf parent)
name
- name of color, e.g. used as header in info panelparent
- parent receives update events, e.g. when color changes through info panelpublic boolean isEnabled()
public void setEnabled(boolean flag)
setValue(double)
, and the color dialog
is always updated.public static int getColor(int a, int r, int g, int b)
public static int getColor(int a, int[] rgb)
public static java.awt.Color getColorFromName(java.lang.String color)
color
- Name of color, e.g. white, gray, orangepublic int getSize()
public void setSize(int aSize)
aSize
- number of color components, e.g. 3 in rgb mode.public boolean equals(java.awt.Color col)
true
if both colors have same color components
excluding alpha. Result is also true
if argument color is null and this color has no entries.
Alpha is not compared since Java1.1 does not support alpha in java.awt.Color.
col
- the color to compare withtrue
if the color component are the same; false
otherwise.public boolean equals(PdColor col)
true
if both colors have same size and
equal components including alpha. Result is also true
if argument color is null and this color has no entries.col
- the color to compare withtrue
if the objects are the same; false
otherwise.public int getGrey()
size==1
, otherwise result is 0.public int getRed()
size==3
, otherwise result is 0.public int getGreen()
size==3
, otherwise result is 0.public int getBlue()
size==3
, otherwise result is 0.public int getAlpha()
public static int getMax(int rgb)
public static int getMin(int rgb)
public static java.awt.Color brighter(java.awt.Color color)
x
is mapped to 127+x/2
.
As a consequence we have:
1. black.brighter() returns grey=(128,128,128)
2. applying brighter to blue returns lighter blue = (128,128,255)
3. non pure color (non zero rgb) will eventually return white
This method is intended as a replacement of java.awt.Color#brighter. The original method simply multiplies each component with something like 1.4 which has no visual consequences for dark colors.
public static int getDimmedColor(int[] rgb, int dim)
rgb
- color to modify.dim
- factor in [0,255] to be multiplied to all color components.public static int getDimmedColor(int rgb, int dim)
rgb
- color to modify.dim
- factor in [0,255] to be multiplied to all color components.public static int getDimmedColor(int rgb, double dim)
rgb
- color to modify.dim
- factor in [0,1] to be multiplied to all color components.public static java.awt.Color getDimmedColor(java.awt.Color col, int dim)
col
- color to modify.dim
- factor in [0,255] to be multiplied to all color components.public static java.awt.Color getDimmedColor(java.awt.Color col, double dim)
col
- color to modify.dim
- factor in [0,1] to be multiplied to all color components.public static java.awt.Color getDimmedColor(java.awt.Color col, double dim, double offset)
col
- color to modify.dim
- factor in [0,1] to be multiplied to all color components.offset
- constant in [0,255] to be added to all color components after dimming.public static int getDimmedColor(int rgb, double dim, double offset)
rgb
- color to modify.dim
- factor in [0,1] to be multiplied to all color components.offset
- constant in [0,255] to be added to all color components after dimming.public java.awt.Color getColor()
null
if less than 3 components available.public void setColor(java.awt.Color aColor)
aColor
- color to be assigned.public boolean hasAlpha()
true
of alpha channel is used; false
otherwise.enableAlpha(boolean)
public void enableAlpha(boolean flag)
flag
- true
to enable alpha channel.setSize(int)
public int getEntry(int ind)
ind
- index of requested color componentpublic void setEntry(int ind, int value)
ind
- index of color component to be modifiedvalue
- value in [0,255] for all components of colorpublic void setEntry(int ind, double value)
ind
- index of color component to be modifiedvalue
- value in [0.,1.] for all components of colorpublic void setConstant(int value)
value
- value in [0,255] for all components of colorpublic void setAlpha(int alpha)
alpha
- new alpha component in [0, 255]public void setAlpha(double alpha)
alpha
- new alpha component in [0., 1.]public void set(int grey)
size == 1
excluding alpha.
If this color has too few components then a warning is issued without changes.grey
- new grey component in [0, 255]public void set(int red, int green, int blue)
size == 3
excluding alpha.
If this color has too few components then a warning is issued without changes.red
- new red color component in [0, 255]green
- new green color component in [0, 255]blue
- new blue color component in [0, 255]public void set(int red, int green, int blue, int alpha)
red
- new red color component in [0, 255]green
- new green color component in [0, 255]blue
- new blue color component in [0, 255]alpha
- new alpha components in [0, 255]public void set(double grey)
size == 1
excluding alpha.
If this color has too few components then a warning is issued without changes.grey
- new grey component in [0., 1.]public void set(double red, double green, double blue)
red
- new red color component in [0., 1.]green
- new green color component in [0., 1.]blue
- new blue color component in [0., 1.]public void set(double red, double green, double blue, double alpha)
red
- new red color component in [0., 1.]green
- new green color component in [0., 1.]blue
- new blue color component in [0., 1.]alpha
- new alpha components in [0., 1.]public java.lang.Object clone()
clone
in class PsObject
PsObject.copy(PsObject)
public static PdColor copyNew(PdColor v)
v
- source color to clonepublic void copy(PdColor v)
v
- source color to copypublic void copy(int[] anArray, int aSize)
anArray
- new color components in [0, 255]aSize
- number of entries in arraypublic void copy(double[] anArray, int aSize)
anArray
- new color components in [0., 1.]aSize
- number of entries in arraypublic int max()
min()
,
indexOfMax()
public int min()
min()
,
indexOfMax()
public int indexOfMax()
public int indexOfMin()
public static int blend(double a, int v, double b, int w)
this = v*a + w*b
including alpha component.
Avoid negative values, method uses absolute value of each weight.
a
- weight of first color vv
- first colorb
- weight of second color ww
- second colorblend(double, PdColor, double, PdColor)
public static java.awt.Color blend(double a, java.awt.Color v, double b, java.awt.Color w)
this = v*a + w*b
including alpha component.
If result color 'this' is empty then its size is set to size if color v,
otherwise no interpolation is performed.
Avoid negative values, method uses absolute value of each weight.
a
- weight of first color vv
- first colorb
- weight of second color ww
- second colorblend(double, PdColor, double, PdColor)
public void blend(double a, PdColor v, double b, PdColor w)
this = v*a + w*b
including alpha component.
If result color 'this' is empty then its size is set to size if color v,
otherwise no interpolation is performed.
Avoid negative values, method uses absolute value of each weight. All new color components are clipped to the range [0,255].
a
- weight of first color vv
- first colorb
- weight of second color ww
- second colorblendBase(PdColor, double, PdColor)
public void blendBase(PdColor v, double b, PdColor w)
this = v + w*b
including alpha component.
If result color 'this' is empty then its size is set to size if color v,
otherwise no interpolation is performed.v
- first colorb
- weight of second color ww
- second colorblend(double, PdColor, double, PdColor)
public static java.awt.Color[] realloc(java.awt.Color[] data, int arraySize)
data!=null
then as most as possible of the content of
existing colors is copied into (possibly reused) colors of the
reallocated array.
Assignment of default color does not waist memory since assignment is
made to a static class.
If requested array size is 0 then method returns an array of length 0.
data
- possibly empty array of colorsarraySize
- number of colors in new arraypublic static PdColor[] realloc(PdColor[] data, int arraySize, int colorSize)
data!=null
then as most as possible of the content of
existing colors is copied into (possibly reused) colors of the
reallocated array. Colors have no alpha channel.data
- possibly empty array of colorsarraySize
- number of colors in new arraycolorSize
- dimension of colors in new arraypublic static PdColor[] realloc(PdColor[] data, int arraySize, int colorSize, boolean bAlpha)
data!=null
then as most as possible of the content of
existing colors is copied into (possibly reused) colors of the
reallocated array.data
- possibly empty array of colorsarraySize
- number of colors in new arraycolorSize
- dimension of colors in new arraybAlpha
- flag whether alpha is enabledpublic static PdColor[] copyNew(PdColor[] data)
data
- array of colorscopyNew(PdColor [], int)
,
copy(PdColor [], int, PdColor [], int, int)
public static PdColor[] copyNew(PdColor[] data, int size)
data
- array of colorssize
- number of copied colors of arraycopyNew(PdColor [])
,
copy(PdColor [], int, PdColor [], int, int)
public static java.awt.Color[] copyNew(java.awt.Color[] data)
data
- array of colorscopyNew(Color [], int)
,
copy(Color [], int, Color [], int, int)
public static java.awt.Color[] copyNew(java.awt.Color[] data, int size)
data
- array of colorssize
- number of copied colors of arraycopyNew(Color [])
,
copy(Color [], int, Color [], int, int)
public static boolean copy(java.awt.Color[] dataDest, int destInd, java.awt.Color[] dataSrc, int srcInd, int size)
for (int i=0; iDestination array must be large enough, i.e. dataDest.length >= destInd+size
.
dataDest
- destination array of colorsdestInd
- index of first destination color in dataDestdataSrc
- source array of colorssrcInd
- index of first source color in dataSrc used for copyingsize
- number of assigned colors of arraytrue
if destination array is large enoughcopy(PdColor)
public static boolean copy(PdColor[] dataDest, int destInd, PdColor[] dataSrc, int srcInd, int size)
for (int i=0; iDestination array must be large enough, i.e. dataDest.length >= destInd+size
.
dataDest
- destination array of colorsdestInd
- index of first destination color in dataDestdataSrc
- source array of colorssrcInd
- index of first source color in dataSrc used for copyingsize
- number of copied colors of arraytrue
if destination array is large enoughcopy(PdColor)
public static java.awt.Color hsv2rgb(java.awt.Color hsv)
public static int hsv2rgbAsInt(int h, int s, int v)
public static java.awt.Color hsv2rgb(int h, int s, int v)
public static java.awt.Color hsl2rgb(int hi, int li, int si)
public static java.awt.Color rgb2hsl(int ri, int gi, int bi)
public static java.awt.Color yiq2rgb(int yi, int ii, int qi)
public static java.awt.Color rgb2yiq(int ri, int gi, int bi)
public static java.awt.Color cmyk2rgb(int c, int m, int y, int k)
public static int[] rgb2cmyk(int r, int g, int b)
public static java.awt.Color parseColor(java.lang.String colorString)
Colors are interpreted as HEX colors if the string starts with '#' character. Otherwise they are interpreted as float color if a '.' is found within the string, or else as integer colors.
Color components in int of float representation may be separated using either blanks, commas or semicolons.
colorString
- string with integer color componentsnull
if less than 3 components available.public static java.awt.Color parseNamedColor(java.lang.String str)
black blue cyan gray darkGray lightGray grey darkGrey lightGrey green magenta orange pink red white yellowand optionally the name can be preprended with a blank separated prefix
dark light. For example, 'dark red'.
public static java.awt.Color getXORColor(java.awt.Color colCurr, java.awt.Color colPref, java.awt.Color colAlt, int eps)
colCurr
- current color against which the new color is compared.colPref
- new preferred colorcolAlt
- alternative color choice returned if preferred color is eps-close to current coloreps
- distance threshold in [0,255] compared to current color.public static java.lang.String toString(java.awt.Color col, java.lang.String sep)
public static java.lang.String toStringAsFloat(java.awt.Color col, java.lang.String sep)
"