Package toxi.color
Class TColor
java.lang.Object
toxi.color.TColor
- All Implemented Interfaces:
ReadonlyTColor
Floating point color class with implicit RGB, HSV, CMYK access modes,
conversion and color theory utils. Based on the Colors library
for NodeBox
-
Field Summary
Modifier and TypeFieldDescriptionfloat
static final ReadonlyTColor
static float
Maximum rgb component value for a color to be classified as black.static final ReadonlyTColor
static final ReadonlyTColor
static final ReadonlyTColor
static float
Maximum saturations value for a color to be classified as greystatic final ReadonlyTColor
static final ReadonlyTColor
static final ReadonlyTColor
static float
Minimum rgb component value for a color to be classified as white.static final ReadonlyTColor
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionadjustConstrast
(float amount) Changes the brightness of the color by the given amount in the direction towards either the black or white point (depending on if current brightness >= 50%)adjustHSV
(float h, float s, float v) Adds the given HSV values as offsets to the current color.adjustRGB
(float r, float g, float b) Adds the given RGB values as offsets to the current color.float
alpha()
analog
(float theta, float delta) Rotates this color by a random amount (not exceeding the one specified) and creates variations in saturation and brightness based on the 2nd parameter.analog
(int angle, float delta) float
black()
blend
(ReadonlyTColor c, float t) Blends the color with the given one by the stated amountfloat
blue()
float
static final float[]
cmykToRGB
(float c, float m, float y, float k) Converts CMYK floats into an RGB array.static final float[]
cmykToRGB
(float c, float m, float y, float k, float[] rgb) Converts CMYK floats into the given RGB array.copy()
float
cyan()
darken
(float step) Reduces the color's brightness by the given amount (e.g 0.1 = 10% darker).desaturate
(float step) Reduced the color's saturation by the given amount.float
Calculates the CMYK distance to the given color.float
Calculates the HSV distance to the given color.float
Calculates the RGB distance to the given color.boolean
getAnalog
(float theta, float delta) getAnalog
(int angle, float delta) getBlended
(ReadonlyTColor c, float t) getClosestHue
(boolean primaryOnly) float
getComponentValue
(AccessCriteria criteria) getDarkened
(float step) getDesaturated
(float step) getLightened
(float step) getRotatedRYB
(float theta) getRotatedRYB
(int angle) getSaturated
(float step) float
green()
int
hashCode()
static final float[]
Converts hex string into a RGB array.static final float[]
static final float[]
hsvToRGB
(float h, float s, float v) Converts HSV values into RGB array.static final float[]
hsvToRGB
(float h, float s, float v, float[] rgb) float
hue()
invert()
Inverts the color.boolean
isBlack()
boolean
isGrey()
boolean
boolean
isWhite()
static final float[]
labToRGB
(float l, float a, float b) static final float[]
labToRGB
(float l, float a, float b, float[] rgb) Converts CIE Lab to RGB components.lighten
(float step) Lightens the color by stated amount.float
Computes the color's luminance using this formula: lum=0.299*red + 0.587*green + 0.114 *bluefloat
magenta()
static final TColor
newARGB
(int argb) Factory method.static final TColor
newCMYK
(float c, float m, float y, float k) Factory method.static final TColor
newCMYKA
(float c, float m, float y, float k, float a) Factory method.static final TColor
newGray
(float gray) Factory method.static final TColor
newGrayAlpha
(float gray, float alpha) Factory method.static final TColor
Factory method.static final TColor
newHSV
(float h, float s, float v) Factory method.static ReadonlyTColor
static final TColor
newHSVA
(float h, float s, float v, float a) static final TColor
Factory method.static final TColor
newRGB
(float r, float g, float b) Factory method.static final TColor
newRGBA
(float r, float g, float b, float a) float
red()
static final float[]
rgbToCMYK
(float r, float g, float b) Converts the RGB values into a CMYK array.static final float[]
rgbToCMYK
(float r, float g, float b, float[] cmyk) static final String
rgbToHex
(float r, float g, float b) Formats the RGB float values into hex integers.static final float[]
rgbToHSV
(float r, float g, float b) Converts the RGB values into an HSV array.static final float[]
rgbToHSV
(float r, float g, float b, float[] hsv) rotateRYB
(float theta) rotateRYB
(int theta) Rotates the color by x degrees along the RYB color wheelsaturate
(float step) Adds the given value to the current saturation component.float
setAlpha
(float alpha) setARGB
(int argb) Sets all color components to new values interpreted from the given packed ARGB 32bit color value.setBlack
(float val) setBlue
(float blue) setBrightness
(float brightness) setCMYK
(float[] newCMYK) setCMYK
(float c, float m, float y, float k) setComponent
(AccessCriteria criteria, float val) Sets a color component by name.setCyan
(float val) setGreen
(float green) setHSV
(float[] newHSV) setHSV
(float h, float s, float v) setHue
(float hue) setMagenta
(float val) setRed
(float red) setRGB
(float[] newRGB) setRGB
(float r, float g, float b) setSaturation
(float saturation) setYellow
(float val) int
toARGB()
Converts the color into a packed ARGB int.float[]
toCMYKAArray
(float[] cmyka) Copies the current CMYKA values into the given array (or constucts a new one with these values).toHex()
Converts the color into a 6 digit hex string.float[]
toHSVAArray
(float[] hsva) float[]
toRGBAArray
(float[] rgba) Copies the current RGBA value into the given array (or constucts a new one with these values).float[]
toRGBAArray
(float[] rgba, int offset) Copies the current RGBA value into the given array starting the given offset.toString()
float
yellow()
-
Field Details
-
BLACK_POINT
public static float BLACK_POINTMaximum rgb component value for a color to be classified as black.- See Also:
-
WHITE_POINT
public static float WHITE_POINTMinimum rgb component value for a color to be classified as white.- See Also:
-
GREY_THRESHOLD
public static float GREY_THRESHOLDMaximum saturations value for a color to be classified as grey- See Also:
-
RED
-
GREEN
-
BLUE
-
CYAN
-
MAGENTA
-
YELLOW
-
BLACK
-
WHITE
-
alpha
public float alpha
-
-
Constructor Details
-
TColor
Creates a deep copy of the given color.- Parameters:
c
-
-
-
Method Details
-
cmykToRGB
public static final float[] cmykToRGB(float c, float m, float y, float k) Converts CMYK floats into an RGB array.- Parameters:
c
-m
-y
-k
-- Returns:
- rgb array
-
cmykToRGB
public static final float[] cmykToRGB(float c, float m, float y, float k, float[] rgb) Converts CMYK floats into the given RGB array.- Parameters:
c
-m
-y
-k
-rgb
-- Returns:
- rgb array
-
hexToRGB
Converts hex string into a RGB array.- Parameters:
hexRGB
-- Returns:
- rgb array
-
hexToRGB
-
hsvToRGB
public static final float[] hsvToRGB(float h, float s, float v) Converts HSV values into RGB array.- Parameters:
h
-s
-v
-- Returns:
- rgb array
-
hsvToRGB
public static final float[] hsvToRGB(float h, float s, float v, float[] rgb) -
labToRGB
public static final float[] labToRGB(float l, float a, float b) -
labToRGB
public static final float[] labToRGB(float l, float a, float b, float[] rgb) Converts CIE Lab to RGB components. First we have to convert to XYZ color space. Conversion involves using a white point, in this case D65 which represents daylight illumination. Algorithm adopted from: http://www.easyrgb.com/math.php- Parameters:
l
-a
-b
-rgb
-- Returns:
- rgb array
-
newARGB
Factory method. Creates new color from ARGB int.- Parameters:
argb
-- Returns:
- new color
-
newCMYK
Factory method. Creates new color from CMYK values.- Parameters:
c
-m
-y
-k
-- Returns:
- new color
-
newCMYKA
Factory method. Creates new color from CMYK + alpha values.- Parameters:
c
-m
-y
-k
-a
-- Returns:
- new color
-
newGray
Factory method. Creates a new shade of gray with alpha set to 100%.- Parameters:
gray
-- Returns:
- new color.
-
newGrayAlpha
Factory method. Creates a new shade of gray + alpha.- Parameters:
gray
-alpha
-- Returns:
- new color.
-
newHex
Factory method. Creates a new color from hex string. Supports standard 6 digit RGB colors or 8 digit ARGB.- Parameters:
hexRGB
-- Returns:
- new color
-
newHSV
Factory method. New color from hsv values.- Parameters:
h
-s
-v
-- Returns:
- new color
-
newHSV
-
newHSVA
-
newRandom
Factory method. Creates new random color. Alpha is always 1.0.- Returns:
- random color
-
newRGB
Factory method. Creates new color from RGB values. Alpha is set to 1.0.- Parameters:
r
-g
-b
-- Returns:
- new color
-
newRGBA
-
rgbToCMYK
public static final float[] rgbToCMYK(float r, float g, float b) Converts the RGB values into a CMYK array.- Parameters:
r
-g
-b
-- Returns:
- cmyk array
-
rgbToCMYK
public static final float[] rgbToCMYK(float r, float g, float b, float[] cmyk) -
rgbToHex
Formats the RGB float values into hex integers.- Parameters:
r
-g
-b
-- Returns:
- hex string
-
rgbToHSV
public static final float[] rgbToHSV(float r, float g, float b) Converts the RGB values into an HSV array.- Parameters:
r
-g
-b
-- Returns:
- hsv array
-
rgbToHSV
public static final float[] rgbToHSV(float r, float g, float b, float[] hsv) -
add
-
addSelf
-
adjustConstrast
Changes the brightness of the color by the given amount in the direction towards either the black or white point (depending on if current brightness >= 50%)- Parameters:
amount
-- Returns:
- itself
-
adjustHSV
Adds the given HSV values as offsets to the current color. Hue will automatically wrap.- Parameters:
h
-s
-v
-- Returns:
- itself
-
adjustRGB
Adds the given RGB values as offsets to the current color. TColor will clip at black or white.- Parameters:
r
-g
-b
-- Returns:
- itself
-
alpha
public float alpha()- Specified by:
alpha
in interfaceReadonlyTColor
- Returns:
- the color's alpha component
-
analog
Rotates this color by a random amount (not exceeding the one specified) and creates variations in saturation and brightness based on the 2nd parameter.- Parameters:
theta
- max. rotation angle (in radians)delta
- max. sat/bri variance- Returns:
- itself
-
analog
-
black
public float black()- Specified by:
black
in interfaceReadonlyTColor
- Returns:
- the color's black component
-
blend
Blends the color with the given one by the stated amount- Parameters:
c
- target colort
- interpolation factor- Returns:
- itself
-
blue
public float blue()- Specified by:
blue
in interfaceReadonlyTColor
- Returns:
- the color's blue component
-
brightness
public float brightness()- Specified by:
brightness
in interfaceReadonlyTColor
- Returns:
- color HSV brightness (not luminance!)
-
complement
- Returns:
- ifself, as complementary color
-
copy
- Specified by:
copy
in interfaceReadonlyTColor
- Returns:
- a mutable copy of the current color
-
cyan
public float cyan()- Specified by:
cyan
in interfaceReadonlyTColor
- Returns:
- the color's cyan component
-
darken
Reduces the color's brightness by the given amount (e.g 0.1 = 10% darker).- Parameters:
step
-- Returns:
- itself
-
desaturate
Reduced the color's saturation by the given amount.- Parameters:
step
-- Returns:
- itself
-
differenceTo
-
distanceToCMYK
Description copied from interface:ReadonlyTColor
Calculates the CMYK distance to the given color.- Specified by:
distanceToCMYK
in interfaceReadonlyTColor
- Parameters:
c
- target color- Returns:
- distance
-
distanceToHSV
Description copied from interface:ReadonlyTColor
Calculates the HSV distance to the given color.- Specified by:
distanceToHSV
in interfaceReadonlyTColor
- Parameters:
c
- target color- Returns:
- distance
-
distanceToRGB
Description copied from interface:ReadonlyTColor
Calculates the RGB distance to the given color.- Specified by:
distanceToRGB
in interfaceReadonlyTColor
- Parameters:
c
- target color- Returns:
- distance
-
equals
-
getAnalog
- Specified by:
getAnalog
in interfaceReadonlyTColor
-
getAnalog
- Specified by:
getAnalog
in interfaceReadonlyTColor
-
getBlended
- Specified by:
getBlended
in interfaceReadonlyTColor
-
getClosestHue
- Specified by:
getClosestHue
in interfaceReadonlyTColor
- Returns:
- an instance of the closest named hue to this color.
-
getClosestHue
- Specified by:
getClosestHue
in interfaceReadonlyTColor
- Parameters:
primaryOnly
- if true, only primary color hues are considered- Returns:
- an instance of the closest named (primary) hue to this color.
-
getComplement
- Specified by:
getComplement
in interfaceReadonlyTColor
-
getComponentValue
- Specified by:
getComponentValue
in interfaceReadonlyTColor
-
getDarkened
- Specified by:
getDarkened
in interfaceReadonlyTColor
- Returns:
- a darkened copy
-
getDesaturated
- Specified by:
getDesaturated
in interfaceReadonlyTColor
- Returns:
- a desaturated copy
-
getDifferenceTo
-
getInverted
- Specified by:
getInverted
in interfaceReadonlyTColor
- Returns:
- an inverted copy
-
getLightened
- Specified by:
getLightened
in interfaceReadonlyTColor
- Returns:
- a lightened copy
-
getRotatedRYB
- Specified by:
getRotatedRYB
in interfaceReadonlyTColor
- Parameters:
theta
- rotation angle in radians- Returns:
- a RYB rotated copy
-
getRotatedRYB
- Specified by:
getRotatedRYB
in interfaceReadonlyTColor
- Parameters:
angle
- rotation angle in degrees- Returns:
- a RYB rotated copy
-
getSaturated
- Specified by:
getSaturated
in interfaceReadonlyTColor
- Returns:
- a saturated copy
-
green
public float green()- Specified by:
green
in interfaceReadonlyTColor
- Returns:
- the color's green component
-
hashCode
public int hashCode() -
hue
public float hue()- Specified by:
hue
in interfaceReadonlyTColor
- Returns:
- the color's hue
-
invert
Inverts the color.- Returns:
- itself
-
isBlack
public boolean isBlack()- Specified by:
isBlack
in interfaceReadonlyTColor
- Returns:
- true, if all rgb component values are equal and less than
BLACK_POINT
-
isGrey
public boolean isGrey()- Specified by:
isGrey
in interfaceReadonlyTColor
- Returns:
- true, if the saturation component value is less than
GREY_THRESHOLD
-
isPrimary
public boolean isPrimary()- Specified by:
isPrimary
in interfaceReadonlyTColor
- Returns:
- true, if this colors hue is matching one of the 7 defined primary hues.
-
isWhite
public boolean isWhite()- Specified by:
isWhite
in interfaceReadonlyTColor
- Returns:
- true, if all rgb component values are equal and greater than
WHITE_POINT
-
lighten
Lightens the color by stated amount.- Parameters:
step
- lighten amount- Returns:
- itself
-
luminance
public float luminance()Description copied from interface:ReadonlyTColor
Computes the color's luminance using this formula: lum=0.299*red + 0.587*green + 0.114 *blue- Specified by:
luminance
in interfaceReadonlyTColor
- Returns:
- luminance
-
magenta
public float magenta()- Specified by:
magenta
in interfaceReadonlyTColor
- Returns:
- the color's magenta component
-
red
public float red()- Specified by:
red
in interfaceReadonlyTColor
- Returns:
- the color's red component
-
rotateRYB
-
rotateRYB
Rotates the color by x degrees along the RYB color wheel- Parameters:
theta
-- Returns:
- itself
-
saturate
Adds the given value to the current saturation component.- Parameters:
step
-- Returns:
- itself
-
saturation
public float saturation()- Specified by:
saturation
in interfaceReadonlyTColor
- Returns:
- the color's saturation
-
setAlpha
-
setARGB
Sets all color components to new values interpreted from the given packed ARGB 32bit color value.- Parameters:
argb
-- Returns:
- itself
-
setBlack
-
setBlue
-
setBrightness
-
setCMYK
-
setCMYK
-
setComponent
Sets a color component by name.- Parameters:
criteria
-val
-- Returns:
- itself
- See Also:
-
setCyan
-
setGreen
-
setHSV
-
setHSV
-
setHue
-
setMagenta
-
setRed
-
setRGB
-
setRGB
-
setSaturation
-
setYellow
-
sub
-
subSelf
-
toARGB
public int toARGB()Description copied from interface:ReadonlyTColor
Converts the color into a packed ARGB int.- Specified by:
toARGB
in interfaceReadonlyTColor
- Returns:
- color as int
-
toCMYKAArray
public float[] toCMYKAArray(float[] cmyka) Description copied from interface:ReadonlyTColor
Copies the current CMYKA values into the given array (or constucts a new one with these values).- Specified by:
toCMYKAArray
in interfaceReadonlyTColor
- Parameters:
cmyka
- result array (or null)- Returns:
- array in this order: c,m,y,k,a
-
toHex
Converts the color into a 6 digit hex string.- Returns:
- color as hex
-
toHSVAArray
public float[] toHSVAArray(float[] hsva) - Specified by:
toHSVAArray
in interfaceReadonlyTColor
- Parameters:
hsva
- result array (or null)- Returns:
- Copies the the current HSV values into the given array (or constucts a new one with these values).
-
toRGBAArray
public float[] toRGBAArray(float[] rgba) Description copied from interface:ReadonlyTColor
Copies the current RGBA value into the given array (or constucts a new one with these values).- Specified by:
toRGBAArray
in interfaceReadonlyTColor
- Parameters:
rgba
- result array (or null)- Returns:
- array in this order: r,g,b,a (OpenGL format)
-
toRGBAArray
public float[] toRGBAArray(float[] rgba, int offset) Description copied from interface:ReadonlyTColor
Copies the current RGBA value into the given array starting the given offset. If the array is null, the method constucts a new one with these values and the offset value is ignored (set to zero).- Specified by:
toRGBAArray
in interfaceReadonlyTColor
- Returns:
- float array
-
toString
-
yellow
public float yellow()- Specified by:
yellow
in interfaceReadonlyTColor
- Returns:
- the color's yellow component
-