Color
final class Color
Extends: LockableValue<Color>
Package: dev.joid.lib.color · Module: core · Source: Color.java
An RGBA color with channels on 1, which can also hold a linear gradient or change on every frame.
Description
Section titled “Description”Every color setter of JOID takes a Color.
Create one from a hex string, from channels, or take a named constant (Create a color).
An animated color, such as Color.RAINBOW, carries an update function that JOID applies each time it draws the color.
Two colors are equal when their channels, their gradient and their update function are equal; the update function compares by identity.
Learn more
Section titled “Learn more”Creation
Section titled “Creation”| Returns | Signature | Summary |
|---|---|---|
Color | rgb(int value) factory | Creates an opaque color from a packed 0xRRGGBB value; the top byte is ignored. |
Color | argb(int value) factory | Creates a color from a packed 0xAARRGGBB value, alpha in the top byte. |
Color | decode(String nm) factory | Parses a color from text, with or without #: #RRGGBB (opaque), #RRGGBBAA (alpha last), rgb(r, g, b) with channels from 0 to 255, rgba(r, g, b, a) with an alpha on 1, gradient(from, to) for a left-to-right gradient or gradient(from, to, x1, y1, x2, y2) with a direction (see toGradient), and rainbow or loading for a copy of the animated constants. |
Color | RAINBOW() factory | Returns the color of the rainbow cycle at this instant of the JOID clock: a full turn of hues every 3 seconds. |
Color | RAINBOW(long time) factory | Returns the color of the rainbow cycle at time, in milliseconds of the JOID clock. |
Color | LOADING() factory | Returns the color of the loading pulse at this instant: a dark gray that brightens and darkens slightly, one cycle every 2 seconds. |
| Color(FloatBuffer buffer) | Reads red, green, blue and alpha on 1 from the current position of the buffer, which advances by four floats. | |
| Color(java.awt.Color color) | Converts a java.awt.Color. | |
| Color(ColorGradient gradient) | Creates a gradient color from a ColorGradient; its own channels are those of the start color. | |
| Color(int r, int g, int b) | Creates an opaque color from red, green and blue from 0 to 255. | |
| Color(float r, float g, float b) | Creates an opaque color from red, green and blue on 1. | |
| Color(int r, int g, int b, int a) | Creates a color from red, green, blue and alpha from 0 to 255. | |
| Color(float r, float g, float b, float a) | Creates a color from red, green, blue and alpha on 1. | |
| Color(float r, float g, float b, float a, UnaryOperator | Creates an animated color: each time JOID draws it, JOID passes this color to update and draws the color that it returns. |
Properties
Section titled “Properties”| Property | Type | Default | Summary |
|---|---|---|---|
| red | float | set at creation | The red channel: getRed() reads it from 0 to 255, getRedF() on 1. |
| blue | float | set at creation | The blue channel: getBlue() reads it from 0 to 255, getBlueF() on 1. |
| alpha | float | 255 | The opacity: getAlpha() reads it from 0 to 255, getAlphaF() on 1, where 0F is fully transparent. |
| green | float | set at creation | The green channel: getGreen() reads it from 0 to 255, getGreenF() on 1. |
Methods
Section titled “Methods”Reactivity
Section titled “Reactivity”| Returns | Signature | Summary |
|---|---|---|
void | bind() | Sets the color of the render state to the current frame of this color. |
void | bind(Runnable runnable, BoundingBox canvas) | Runs runnable with this color applied, then restores the previous color of the render state. |
void | bind(Runnable runnable, BoundingBox canvas, boolean hasTexture) | Runs runnable with this color applied, then restores the previous color; if hasTexture is true, a gradient multiplies the bound texture instead of replacing it, which tints an image. |
Queries
Section titled “Queries”| Returns | Signature | Summary |
|---|---|---|
int | getRGB() | Returns the color packed as 0xAARRGGBB. |
float | getRedF() | Returns the red channel on 1. |
float | getBlueF() | Returns the blue channel on 1. |
float | getAlphaF() | Returns the alpha channel on 1. |
float | getGreenF() | Returns the green channel on 1. |
this | to(Color target, float progress) fluent | Returns the color between this one and target at progress on 1: 0F gives a copy of this color, 1F a copy of target, alpha included. |
this | toGradient(Color target) fluent | Returns a left-to-right gradient from this color to target. |
Color | toGradient(Color target, Point startPoint, Point endPoint) | Returns a gradient from this color to target along the line from startPoint to endPoint. |
boolean | isGradient() | If true, this color holds a ColorGradient and draws as a gradient. |
String | toString() | Returns the channels from 0 to 255 and the #RRGGBBAA form, for logs. |
ColorGradient | getGradient() | Returns the gradient of this color, or null for a plain color. |
Actions
Section titled “Actions”| Returns | Signature | Summary |
|---|---|---|
String | encode() | Returns the color as #RRGGBBAA in upper case, the format that decode reads back. |
this | darker() fluent | Returns this color darkened by half (darker(0.5F)). |
this | darker(float scale) fluent | Returns this color with red, green and blue multiplied by 1 - scale; alpha is unchanged. |
this | brighter() fluent | Returns this color brightened by 20 % (brighter(0.2F)). |
this | brighter(float scale) fluent | Returns this color with red, green and blue multiplied by 1 + scale, clamped to 1F; alpha is unchanged. |
this | multiply(Color c) fluent | Returns this color multiplied channel by channel, alpha included, by c. |
this | add(Color c) fluent | Returns the sum of this color and c channel by channel, alpha included, clamped to 1F. |
this | scale(float value) fluent | Returns this color with every channel, alpha included, multiplied by value. |
float[] | RGBtoHSB(float[] hsbvals) | Returns the hue, saturation and brightness of this color, each on 1, in hsbvals when it is not null, otherwise in a new array. |
float[] | RGBtoHSB(int r, int g, int b, float[] hsbvals) static | Converts red, green and blue from 0 to 255 to hue, saturation and brightness on 1, written in hsbvals when it is not null, otherwise in a new array. |
this | withRed(float red) fluent | Returns a copy with the red channel set to red, on 1. |
this | withBlue(float blue) fluent | Returns a copy with the blue channel set to blue, on 1. |
Color | withAlpha(float alpha) | Returns a copy with the opacity set to alpha, on 1. |
this | withGreen(float green) fluent | Returns a copy with the green channel set to green, on 1. |
Color | copy() | Returns an unlocked copy that keeps the gradient and the update function. |
void | reset() static | Sets the color of the render state back to opaque white, the color that leaves textures and vertex colors unchanged. |
boolean | equals(Object other) | If true, the other object is of the same class with the same channels, update and gradient; the name of a constant is ignored. |
int | hashCode() | Returns a hash of the values that equals compares. |
this | update() fluent | Returns the frame of this color at this instant: the color itself when it is not animated, otherwise a fixed color computed by its update function. |
Inherited from LockableValue (2)
Constants and fields
Section titled “Constants and fields”| Type | Name | Value | Summary |
|---|---|---|---|
Color | RED | new Color(1F, 0F, 0F, 1F).constant("RED") | Opaque red, #FF0000FF. |
Color | BLUE | new Color(0F, 0F, 1F, 1F).constant("BLUE") | Opaque blue, #0000FFFF. |
Color | GRAY | new Color(0.5F, 0.5F, 0.5F, 1F).constant("GRAY") | Opaque mid gray, 0.5F on every channel. |
Color | CYAN | new Color(0F, 1F, 1F, 1F).constant("CYAN") | Opaque cyan, #00FFFFFF. |
Color | PINK | new Color(1F, 0.7F, 0.7F, 1F).constant("PINK") | Opaque light pink: red 1F, green and blue 0.7F. |
Color | WHITE | new Color(1F, 1F, 1F, 1F).constant("WHITE") | Opaque white, #FFFFFFFF. |
Color | GREEN | new Color(0F, 1F, 0F, 1F).constant("GREEN") | Opaque green, #00FF00FF. |
Color | BLACK | new Color(0F, 0F, 0F, 1F).constant("BLACK") | Opaque black, #000000FF. |
Color | YELLOW | new Color(1F, 1F, 0F, 1F).constant("YELLOW") | Opaque yellow, #FFFF00FF. |
Color | ORANGE | new Color(1F, 0.5F, 0F, 1F).constant("ORANGE") | Opaque orange: red 1F, green 0.5F, blue 0F. |
Color | MAGENTA | new Color(1F, 0F, 1F, 1F).constant("MAGENTA") | Opaque magenta, #FF00FFFF. |
Color | DARKGRAY | new Color(0.3F, 0.3F, 0.3F, 1F).constant("DARKGRAY") | Opaque dark gray, 0.3F on every channel. |
Color | LIGHTGRAY | new Color(0.7F, 0.7F, 0.7F, 1F).constant("LIGHTGRAY") | Opaque light gray, 0.7F on every channel. |
Color | TRANSPARENT | new Color(0F, 0F, 0F, 0F).constant("TRANSPARENT") | Fully transparent black: draws nothing. |
Color | RAINBOW | An animated color that cycles through the hues every 3 seconds, at 80 % saturation and brightness; its alpha is kept. | |
Color | LOADING | An animated color that pulses slightly around a dark gray every 2 seconds, the look of a loading placeholder; its alpha is kept. |
Creation details
Section titled “Creation details”Color rgb(int value) factory
Creates an opaque color from a packed 0xRRGGBB value; the top byte is ignored.
Color argb(int value) factory
Creates a color from a packed 0xAARRGGBB value, alpha in the top byte.
decode
Section titled “decode”Color decode(String nm) factory
Throws:
NumberFormatException: The color … cannot be decoded, write it as #RRGGBB, #RRGGBBAA, rgb(r, g, b), rgba(r, g, b, a) or gradient(from, to)
Parses a color from text, with or without #: #RRGGBB (opaque), #RRGGBBAA (alpha last), rgb(r, g, b) with channels from 0 to 255, rgba(r, g, b, a) with an alpha on 1, gradient(from, to) for a left-to-right gradient or gradient(from, to, x1, y1, x2, y2) with a direction (see toGradient), and rainbow or loading for a copy of the animated constants.
Throws NumberFormatException for any other text.
RAINBOW
Section titled “RAINBOW”Color RAINBOW() factory
Returns the color of the rainbow cycle at this instant of the JOID clock: a full turn of hues every 3 seconds.
The result is a plain color that does not change: pass the constant Color.RAINBOW to a setter instead, so that the color cycles by itself.
Color RAINBOW(long time) factory
Returns the color of the rainbow cycle at time, in milliseconds of the JOID clock.
LOADING
Section titled “LOADING”Color LOADING() factory
Returns the color of the loading pulse at this instant: a dark gray that brightens and darkens slightly, one cycle every 2 seconds.
The result is a plain color: pass the constant Color.LOADING to a setter instead, so that the color pulses by itself.
Color(FloatBuffer buffer)
Reads red, green, blue and alpha on 1 from the current position of the buffer, which advances by four floats.
Color(java.awt.Color color)
Converts a java.awt.Color.
Color(ColorGradient gradient)
Creates a gradient color from a ColorGradient; its own channels are those of the start color.
Color(int r, int g, int b)
Creates an opaque color from red, green and blue from 0 to 255.
Color(float r, float g, float b)
Creates an opaque color from red, green and blue on 1.
Color(int r, int g, int b, int a)
Creates a color from red, green, blue and alpha from 0 to 255.
Color(float r, float g, float b, float a)
Creates a color from red, green, blue and alpha on 1.
Every channel is clamped between 0F and 1F.
Color(float r, float g, float b, float a, UnaryOperator<Color> update)
Creates an animated color: each time JOID draws it, JOID passes this color to update and draws the color that it returns.
A channel that you set later with red, green, blue or alpha overrides that channel on every frame.
Property details
Section titled “Property details”this red(float red)fluentint getRed()
Default: set at creation
The red channel: getRed() reads it from 0 to 255, getRedF() on 1.
red(float) sets it in place, on 1, and throws IllegalStateException on a locked color such as a named constant.
this blue(float blue)fluentint getBlue()
Default: set at creation
The blue channel: getBlue() reads it from 0 to 255, getBlueF() on 1.
blue(float) sets it in place, on 1, and throws IllegalStateException on a locked color.
this alpha(float alpha)fluentint getAlpha()
Default: 255
The opacity: getAlpha() reads it from 0 to 255, getAlphaF() on 1, where 0F is fully transparent.
alpha(float) sets it in place, on 1, and throws IllegalStateException on a locked color.
this green(float green)fluentint getGreen()
Default: set at creation
The green channel: getGreen() reads it from 0 to 255, getGreenF() on 1.
green(float) sets it in place, on 1, and throws IllegalStateException on a locked color.
Method details
Section titled “Method details”void bind()
Sets the color of the render state to the current frame of this color.
Draws without a color of their own, such as images and Tessellator batches without vertex colors, are tinted by it.
void bind(Runnable runnable, BoundingBox canvas)
Runs runnable with this color applied, then restores the previous color of the render state.
For a gradient, it binds the gradient shader instead, spread over canvas in the coordinates you draw in.
void bind(Runnable runnable, BoundingBox canvas, boolean hasTexture)
Runs runnable with this color applied, then restores the previous color; if hasTexture is true, a gradient multiplies the bound texture instead of replacing it, which tints an image.
getRGB
Section titled “getRGB”int getRGB()
Returns the color packed as 0xAARRGGBB.
getRedF
Section titled “getRedF”float getRedF()
Returns the red channel on 1.
getBlueF
Section titled “getBlueF”float getBlueF()
Returns the blue channel on 1.
getAlphaF
Section titled “getAlphaF”float getAlphaF()
Returns the alpha channel on 1.
getGreenF
Section titled “getGreenF”float getGreenF()
Returns the green channel on 1.
this to(Color target, float progress) fluent
Returns the color between this one and target at progress on 1: 0F gives a copy of this color, 1F a copy of target, alpha included.
Two gradients blend stop by stop and point by point; a plain color blends with each stop of a gradient.
When either side is animated, the result is animated and blends the current frame of both sides.
toGradient
Section titled “toGradient”this toGradient(Color target) fluent
Returns a left-to-right gradient from this color to target.
Color toGradient(Color target, Point startPoint, Point endPoint)
Returns a gradient from this color to target along the line from startPoint to endPoint.
The points are on 1 of the drawn area: (0, 0) is its top-left corner and (1, 1) its bottom-right corner, so (0, 0) to (0, 1) runs from top to bottom.
isGradient
Section titled “isGradient”boolean isGradient()
If true, this color holds a ColorGradient and draws as a gradient.
toString
Section titled “toString”String toString()
Returns the channels from 0 to 255 and the #RRGGBBAA form, for logs.
getGradient
Section titled “getGradient”ColorGradient getGradient()
Returns the gradient of this color, or null for a plain color.
encode
Section titled “encode”String encode()
Returns the color as #RRGGBBAA in upper case, the format that decode reads back.
darker
Section titled “darker”this darker() fluent
Returns this color darkened by half (darker(0.5F)).
this darker(float scale) fluent
Returns this color with red, green and blue multiplied by 1 - scale; alpha is unchanged.
brighter
Section titled “brighter”this brighter() fluent
Returns this color brightened by 20 % (brighter(0.2F)).
this brighter(float scale) fluent
Returns this color with red, green and blue multiplied by 1 + scale, clamped to 1F; alpha is unchanged.
multiply
Section titled “multiply”this multiply(Color c) fluent
Returns this color multiplied channel by channel, alpha included, by c.
this add(Color c) fluent
Returns the sum of this color and c channel by channel, alpha included, clamped to 1F.
this scale(float value) fluent
Returns this color with every channel, alpha included, multiplied by value.
RGBtoHSB
Section titled “RGBtoHSB”float[] RGBtoHSB(float[] hsbvals)
Returns the hue, saturation and brightness of this color, each on 1, in hsbvals when it is not null, otherwise in a new array.
float[] RGBtoHSB(int r, int g, int b, float[] hsbvals) static
Converts red, green and blue from 0 to 255 to hue, saturation and brightness on 1, written in hsbvals when it is not null, otherwise in a new array.
withRed
Section titled “withRed”this withRed(float red) fluent
Returns a copy with the red channel set to red, on 1.
withBlue
Section titled “withBlue”this withBlue(float blue) fluent
Returns a copy with the blue channel set to blue, on 1.
withAlpha
Section titled “withAlpha”Color withAlpha(float alpha)
Returns a copy with the opacity set to alpha, on 1.
On a gradient, both stops scale by the same factor, so their relative opacity is kept.
withGreen
Section titled “withGreen”this withGreen(float green) fluent
Returns a copy with the green channel set to green, on 1.
Color copy()
Returns an unlocked copy that keeps the gradient and the update function.
Copy a named constant before you change it with red, green, blue or alpha.
void reset() static
Sets the color of the render state back to opaque white, the color that leaves textures and vertex colors unchanged.
equals
Section titled “equals”boolean equals(Object other)
If true, the other object is of the same class with the same channels, update and gradient; the name of a constant is ignored.
hashCode
Section titled “hashCode”int hashCode()
Returns a hash of the values that equals compares.
update
Section titled “update”this update() fluent
Returns the frame of this color at this instant: the color itself when it is not animated, otherwise a fixed color computed by its update function.
Constant and field details
Section titled “Constant and field details”Color RED
Opaque red, #FF0000FF.
Color BLUE
Opaque blue, #0000FFFF.
Color GRAY
Opaque mid gray, 0.5F on every channel.
Color CYAN
Opaque cyan, #00FFFFFF.
Color PINK
Opaque light pink: red 1F, green and blue 0.7F.
Color WHITE
Opaque white, #FFFFFFFF.
Color GREEN
Opaque green, #00FF00FF.
Color BLACK
Opaque black, #000000FF.
YELLOW
Section titled “YELLOW”Color YELLOW
Opaque yellow, #FFFF00FF.
ORANGE
Section titled “ORANGE”Color ORANGE
Opaque orange: red 1F, green 0.5F, blue 0F.
MAGENTA
Section titled “MAGENTA”Color MAGENTA
Opaque magenta, #FF00FFFF.
DARKGRAY
Section titled “DARKGRAY”Color DARKGRAY
Opaque dark gray, 0.3F on every channel.
LIGHTGRAY
Section titled “LIGHTGRAY”Color LIGHTGRAY
Opaque light gray, 0.7F on every channel.
TRANSPARENT
Section titled “TRANSPARENT”Color TRANSPARENT
Fully transparent black: draws nothing.
RAINBOW
Section titled “RAINBOW”Color RAINBOW
An animated color that cycles through the hues every 3 seconds, at 80 % saturation and brightness; its alpha is kept.
LOADING
Section titled “LOADING”Color LOADING
An animated color that pulses slightly around a dark gray every 2 seconds, the look of a loading placeholder; its alpha is kept.
fluent returns the instance, typed for chaining · factory creates an instance · signal also accepts a value that follows signals, as a signal, an expression or a lambda (When does JOID compute this value again?) · hook called by JOID, override it · abstract you implement it · default interface method with a body · static called on the class · protected visible to subclasses
