Color

A color represented by RGB components in the linear working color space, which defaults to ColorSpace.LinearSRGB. Inputs conventionally using ColorSpace.SRGB (such as hexadecimals) are converted to the working color space automatically. If ColorManagement.enabled is false, no conversions occur.

Colors are mutable and not thread-safe; confine an instance (and any object graph holding it) to a single thread, exactly as in three.js. Most methods mutate this and return it for chaining.

Iterating a color yields its components (r, g, b) in order.

Deferred CSS-string / named-color paths

three.js's setStyle (CSS-string parsing: rgb(), hsl(), #hex), the X11 setColorName lookup with its 140-entry _colorKeywords table, Color.NAMES, and getStyle (CSS-string output) are deferred: they require a large color name table and regex-based CSS parsing that add no value to the pure math layer yet. The numeric constructors (Color with r,g,b or a hex Int), setHex, setRGB and setHSL cover the algebraic paths.

Constructors

Link copied to clipboard
constructor()

Constructs a white color (r = g = b = 1).

constructor(r: Double, g: Double, b: Double)

Constructs a color from the RGB components r, g, b (via setRGB, i.e. interpreted in the working color space).

constructor(hex: Int)

Constructs a color from the hexadecimal value hex (via setHex, i.e. interpreted as ColorSpace.SRGB by default).

constructor(color: Color)

Constructs a color as a copy of color.

Properties

Link copied to clipboard
var b: Double

The blue component.

Link copied to clipboard
var g: Double

The green component.

Link copied to clipboard
var r: Double

The red component.

Functions

Link copied to clipboard
fun add(color: Color): Color

Adds the RGB values of color to this color.

Link copied to clipboard
fun addColors(color1: Color, color2: Color): Color

Adds the RGB values of color1 and color2 and stores the result here.

Link copied to clipboard

Adds the scalar value s to this color's RGB values.

Link copied to clipboard

Transforms this color with the 3x3 matrix m.

Link copied to clipboard
fun clone(): Color

Returns a new color with copied values from this instance.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
fun copy(color: Color): Color

Copies the values of color into this instance.

Link copied to clipboard

Copies color into this color, then converts from ColorSpace.LinearSRGB to ColorSpace.SRGB.

Link copied to clipboard

Copies color into this color, then converts from ColorSpace.SRGB to ColorSpace.LinearSRGB.

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean

Structural equality: true when other is a Color with equal components (component-wise ==, so NaN != NaN).

Link copied to clipboard
fun fromArray(array: DoubleArray, offset: Int = 0): Color

Sets this color's RGB components from array at offset.

Link copied to clipboard
fun fromBufferAttribute(attribute: AttributeLike, index: Int): Color

Sets this color's components from attribute at index.

Link copied to clipboard
fun getHex(colorSpace: ColorSpace = ColorSpace.SRGB): Int

Returns the hexadecimal value of this color (in colorSpace, default ColorSpace.SRGB).

Link copied to clipboard
fun getHexString(colorSpace: ColorSpace = ColorSpace.SRGB): String

Returns the hexadecimal value of this color as a 6-digit lowercase string (for example, "ffffff").

Link copied to clipboard
fun getHSL(target: HSL, colorSpace: ColorSpace = ColorManagement.workingColorSpace): HSL

Converts this color's RGB values into HSL and stores them into target.

Link copied to clipboard
fun getRGB(target: Color, colorSpace: ColorSpace = ColorManagement.workingColorSpace): Color

Returns the RGB values of this color and stores them into target.

Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
open operator override fun iterator(): Iterator<Double>
Link copied to clipboard
fun lerp(color: Color, alpha: Double): Color

Linearly interpolates this color's RGB values toward color by alpha (0.0 is this color, 1.0 is color).

Link copied to clipboard
fun lerpColors(color1: Color, color2: Color, alpha: Double): Color

Sets this color to the linear interpolation of color1 and color2 by alpha (0.0 is color1, 1.0 is color2).

Link copied to clipboard
fun lerpHSL(color: Color, alpha: Double): Color

Linearly interpolates this color's HSL values toward color's HSL values by alpha, going through all the intermediate hues.

Link copied to clipboard
fun multiply(color: Color): Color

Multiplies this color's RGB values by color's (component-wise).

Link copied to clipboard

Multiplies this color's RGB values by the scalar s.

Link copied to clipboard

Adds the given HSL values to this color's values (via a round-trip through HSL).

Link copied to clipboard
fun set(color: Color): Color

Copies the components of color into this instance. Kotlin equivalent of three.js's set(Color) path.

fun set(hex: Int): Color

Sets this color from the hexadecimal value hex (interpreted in colorSpace). Kotlin equivalent of three.js's set(number) path.

fun set(r: Double, g: Double, b: Double): Color

Sets this color from the RGB components r, g, b (in the working color space). Kotlin equivalent of three.js's numeric set(r, g, b) path.

Link copied to clipboard

Sets this color's RGB components from the 3D vector v (r = x, g = y, b = z).

Link copied to clipboard
fun setHex(hex: Int, colorSpace: ColorSpace = ColorSpace.SRGB): Color

Sets this color from a hexadecimal value.

Link copied to clipboard
fun setHSL(h: Double, s: Double, l: Double, colorSpace: ColorSpace = ColorManagement.workingColorSpace): Color

Sets this color from HSL values.

Link copied to clipboard
fun setRGB(r: Double, g: Double, b: Double, colorSpace: ColorSpace = ColorManagement.workingColorSpace): Color

Sets this color from RGB values.

Link copied to clipboard
fun setScalar(scalar: Double): Color

Sets all components to the scalar value scalar.

Link copied to clipboard
fun sub(color: Color): Color

Subtracts the RGB values of color from this color, clamping each component at 0.

Link copied to clipboard
fun toArray(array: MutableList<Double> = mutableListOf(), offset: Int = 0): MutableList<Double>

Writes this color's RGB components into array at offset, growing the list as needed, and returns it.

Link copied to clipboard
fun toJSON(): Int

Returns this color's serialization result: its hexadecimal value. Kotlin equivalent of three.js's toJSON().

Link copied to clipboard
open override fun toString(): String