[name]

Class representing a color.

Code Examples

A Color can be initialised in any of the following ways:

//empty constructor - will default white const color1 = new THREE.Color(); //Hexadecimal color (recommended) const color2 = new THREE.Color( 0xff0000 ); //RGB string const color3 = new THREE.Color("rgb(255, 0, 0)"); const color4 = new THREE.Color("rgb(100%, 0%, 0%)"); //X11 color name - all 140 color names are supported. //Note the lack of CamelCase in the name const color5 = new THREE.Color( 'skyblue' ); //HSL string const color6 = new THREE.Color("hsl(0, 100%, 50%)"); //Separate RGB values between 0 and 1 const color7 = new THREE.Color( 1, 0, 0 );

Constructor

[name]( [param:Color_Hex_or_String r], [param:Float g], [param:Float b] )

[page:Color_Hex_or_String r] - (optional) If arguments [page:Float g] and [page:Float b] are defined, the red component of the color. If they are not defined, it can be a [link:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet hexadecimal triplet] (recommended), a CSS-style string, or another Color instance.
[page:Float g] - (optional) If it is defined, the green component of the color.
[page:Float b] - (optional) If it is defined, the blue component of the color.

Note that standard method of specifying color in three.js is with a [link:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet hexadecimal triplet], and that method is used throughout the rest of the documentation.

When all arguments are defined then [page:Color_Hex_or_String r] is the red component, [page:Float g] is the green component and [page:Float b] is the blue component of the color.
When only [page:Color_Hex_or_String r] is defined:

Properties

[property:Float r]

Red channel value between 0 and 1. Default is 1.

[property:Float g]

Green channel value between 0 and 1. Default is 1.

[property:Float b]

Blue channel value between 0 and 1. Default is 1.

Methods

[method:this add]( [param:Color color] )

Adds the RGB values of [page:Color color] to the RGB values of this color.

[method:this addColors]( [param:Color color1], [param:Color color2] )

Sets this color's RGB values to the sum of the RGB values of [page:Color color1] and [page:Color color2].

[method:this addScalar]( [param:Number s] )

Adds [page:Number s] to the RGB values of this color.

[method:Color clone]()

Returns a new Color with the same [page:.r r], [page:.g g] and [page:.b b] values as this one.

[method:this copy]( [param:Color color] )

Copies the [page:.r r], [page:.g g] and [page:.b b] parameters from [page:Color color] in to this color.

[method:this convertGammaToLinear]( [param:Float gammaFactor] )

[page:Float gammaFactor] - (optional). Default is *2.0*.

Converts this color from gamma space to linear space by taking [page:.r r], [page:.g g] and [page:.b b] to the power of [page:Float gammaFactor].

[method:this convertLinearToGamma]( [param:Float gammaFactor] )

[page:Float gammaFactor] - (optional). Default is *2.0*.

Converts this color from linear space to gamma space by taking [page:.r r], [page:.g g] and [page:.b b] to the power of 1 / [page:Float gammaFactor].

[method:this convertLinearToSRGB]()

Converts this color from linear space to sRGB space.

[method:this convertSRGBToLinear]()

Converts this color from sRGB space to linear space.

[method:this copyGammaToLinear]( [param:Color color], [param:Float gammaFactor] )

[page:Color color] — Color to copy.
[page:Float gammaFactor] - (optional). Default is *2.0*.

Copies the given color into this color, and then converts this color from gamma space to linear space by taking [page:.r r], [page:.g g] and [page:.b b] to the power of [page:Float gammaFactor].

[method:this copyLinearToGamma]( [param:Color color], [param:Float gammaFactor] )

[page:Color color] — Color to copy.
[page:Float gammaFactor] - (optional). Default is *2.0*.

Copies the given color into this color, and then converts this color from linear space to gamma space by taking [page:.r r], [page:.g g] and [page:.b b] to the power of 1 / [page:Float gammaFactor].

[method:this copyLinearToSRGB]( [param:Color color]] )

[page:Color color] — Color to copy.
Copies the given color into this color, and then converts this color from linear space to sRGB space.

[method:this copySRGBToLinear]( [param:Color color] )

[page:Color color] — Color to copy.
Copies the given color into this color, and then converts this color from sRGB space to linear space.

[method:Boolean equals]( [param:Color color] )

Compares the RGB values of [page:Color color] with those of this object. Returns true if they are the same, false otherwise.

[method:this fromArray]( [param:Array array], [param:Integer offset] )

[page:Array array] - [page:Array] of floats in the form [ [page:Float r], [page:Float g], [page:Float b] ].
[page:Integer offset] - An optional offset into the array.

Sets this color's components based on an array formatted like [ [page:Float r], [page:Float g], [page:Float b] ].

[method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )

[page:BufferAttribute attribute] - the source attribute.
[page:Integer index] - index in the attribute.

Sets this color's components from the [page:BufferAttribute attribute].

[method:Integer getHex]()

Returns the hexadecimal value of this color.

[method:String getHexString]()

Returns the hexadecimal value of this color as a string (for example, 'FFFFFF').

[method:Object getHSL]( [param:Object target] )

[page:Object target] — the result will be copied into this Object. Adds h, s and l keys to the object (if not already present).

Convert this Color's [page:.r r], [page:.g g] and [page:.b b] values to [link:https://en.wikipedia.org/wiki/HSL_and_HSV HSL] format and returns an object of the form: { h: 0, s: 0, l: 0 }

[method:String getStyle]()

Returns the value of this color as a CSS style string. Example: 'rgb(255,0,0)'.

[method:this lerp]( [param:Color color], [param:Float alpha] )

[page:Color color] - color to converge on.
[page:Float alpha] - interpolation factor in the closed interval [0, 1].

Linearly interpolates this color's RGB values toward the RGB values of the passed argument. The alpha argument can be thought of as the ratio between the two colors, where 0.0 is this color and 1.0 is the first argument.

[method:this lerpColors]( [param:Color color1], [param:Color color2], [param:Float alpha] )

[page:Color color1] - the starting [page:Color].
[page:Color color2] - [page:Color] to interpolate towards.
[page:Float alpha] - interpolation factor, typically in the closed interval [0, 1].

Sets this color to be the color linearly interpolated between [page:Color color1] and [page:Color color2] where alpha is the percent distance along the line connecting the two colors - alpha = 0 will be [page:Color color1], and alpha = 1 will be [page:Color color2].

[method:this lerpHSL]( [param:Color color], [param:Float alpha] )

[page:Color color] - color to converge on.
[page:Float alpha] - interpolation factor in the closed interval [0, 1].

Linearly interpolates this color's HSL values toward the HSL values of the passed argument. It differs from the classic [page:.lerp] by not interpolating straight from one color to the other, but instead going through all the hues in between those two colors. The alpha argument can be thought of as the ratio between the two colors, where 0.0 is this color and 1.0 is the first argument.

[method:this multiply]( [param:Color color] )

Multiplies this color's RGB values by the given [page:Color color]'s RGB values.

[method:this multiplyScalar]( [param:Number s] )

Multiplies this color's RGB values by [page:Number s].

[method:this offsetHSL]( [param:Float h], [param:Float s], [param:Float l] )

Adds the given [page:Float h], [page:Float s], and [page:Float l] to this color's values. Internally, this converts the color's [page:.r r], [page:.g g] and [page:.b b] values to HSL, adds [page:Float h], [page:Float s], and [page:Float l], and then converts the color back to RGB.

[method:this set]( [param:Color_Hex_or_String value] )

[page:Color_Hex_or_String value] - Value to set this color to.

See the Constructor above for full details of what [page:Color_Hex_or_String value] can be. Delegates to [page:.copy], [page:.setStyle], or [page:.setHex] depending on input type.

[method:this setHex]( [param:Integer hex] )

[page:Integer hex] — [link:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet hexadecimal triplet] format.

Sets this color from a hexadecimal value.

[method:this setHSL]( [param:Float h], [param:Float s], [param:Float l] )

[page:Float h] — hue value between 0.0 and 1.0
[page:Float s] — saturation value between 0.0 and 1.0
[page:Float l] — lightness value between 0.0 and 1.0

Sets color from HSL values.

[method:this setRGB]( [param:Float r], [param:Float g], [param:Float b] )

[page:Float r] — Red channel value between 0.0 and 1.0.
[page:Float g] — Green channel value between 0.0 and 1.0.
[page:Float b] — Blue channel value between 0.0 and 1.0.

Sets this color from RGB values.

[method:this setScalar]( [param:Float scalar] )

[page:Float scalar] — a value between 0.0 and 1.0.

Sets all three color components to the value [page:Float scalar].

[method:this setStyle]( [param:String style] )

[page:String style] — color as a CSS-style string.

Sets this color from a CSS-style string. For example, "rgb(250, 0,0)", "rgb(100%, 0%, 0%)", "hsl(0, 100%, 50%)", "#ff0000", "#f00", or "red" ( or any [link:https://en.wikipedia.org/wiki/X11_color_names#Color_name_chart X11 color name] - all 140 color names are supported ).
Translucent colors such as "rgba(255, 0, 0, 0.5)" and "hsla(0, 100%, 50%, 0.5)" are also accepted, but the alpha-channel coordinate will be discarded.

Note that for X11 color names, multiple words such as Dark Orange become the string 'darkorange'.

[method:this setColorName]( [param:String style] )

[page:String style] — color name ( from [link:https://en.wikipedia.org/wiki/X11_color_names#Color_name_chart X11 color names] ).

Sets this color from a color name. Faster than [page:.setStyle] method if you don't need the other CSS-style formats.

For convenience, the list of names is exposed in Color.NAMES as a hash: Color.NAMES.aliceblue // returns 0xF0F8FF

[method:this sub]( [param:Color color] )

Subtracts the RGB components of the given color from the RGB components of this color. If this results in a negative component, that component is set to zero.

[method:Array toArray]( [param:Array array], [param:Integer offset] )

[page:Array array] - An optional array to store the color to.
[page:Integer offset] - An optional offset into the array.

Returns an array of the form [ r, g, b ].

Source

[link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]