Color
Class representing a color.
Class representing a color.
Constructors
Constructor
new Color(
color_or_r?,g?,b?):Color
Parameters
color_or_r?
string | number | Color
g?
number
b?
number
Returns
Color
Properties
r
r:
number
Red channel value between 0 and 1.
Default Value
1.
g
g:
number
Green channel value between 0 and 1.
Default Value
1.
b
b:
number
Blue channel value between 0 and 1.
Default Value
1.
isColor
isColor:
boolean
Check the type whether it belongs to Color. This value should not be changed by user.
Accessors
x
Get Signature
get x():
number
Returns
number
y
Get Signature
get y():
number
Returns
number
z
Get Signature
get z():
number
Returns
number
Methods
setRGB()
setRGB(
r,g,b):Color
Sets this color from RGB values.
Parameters
r
number
Red channel value between 0.0 and 1.0.
g
number
Green channel value between 0.0 and 1.0.
b
number
Blue channel value between 0.0 and 1.0.
Returns
Color
copy()
copy(
color):Color
Copies the r, g and b parameters from color in to this color.
Parameters
color
Color
Returns
Color
setHex()
setHex(
hex):Color
Sets this color from a hexadecimal value.
Parameters
hex
number
hexadecimal triplet format.
Returns
Color
setHSL()
setHSL(
h,s,l):Color
Sets color from HSL values.
Parameters
h
number
hue value between 0.0 and 1.0.
s
number
saturation value between 0.0 and 1.0.
l
number
lightness value between 0.0 and 1.0.
Returns
Color
setStyle()
setStyle(
style):Color
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.
Parameters
style
string
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 X11 color name
- all 140 color names are supported ).
Returns
Color
Note
that for X11 color names, multiple words such as Dark Orange become the string ‘darkorange’ (all lowercase).
set()
set(
value):Color
See the Constructor above for full details of what Color_Hex_or_String| value can be. Delegates to .copy| .copy, .setStyle| .setStyle, or setHex depending on input type.
Parameters
value
string | number | Color
Value to set this color to.
Returns
Color
setScalar()
setScalar(
scalar):Color
Sets all three color components to the value Float| scalar.
Parameters
scalar
number
a value between 0.0 and 1.0.
Returns
Color
clone()
clone():
Color
Return a new Color with the same r, g and b values as this clone.
Returns
Color
cloneReadonly()
cloneReadonly():
Readonly<Pick<ReadonlyMarked,"_readonly_mark"|"cloneReadonly"|"clone"|"g"|"r"|"b"|"isColor"|"equals"|"getHex"|"getSerializeData">>
Returns
Readonly<Pick<ReadonlyMarked, "_readonly_mark" | "cloneReadonly" | "clone" | "g" | "r" | "b" | "isColor" | "equals" | "getHex" | "getSerializeData">>
copyGammaToLinear()
copyGammaToLinear(
color,gammaFactor?):Color
Copies the given color into this color, and then converts this color from gamma space to linear space by taking r, g and b to the power of Float| gammaFactor.
Parameters
color
Color
Color to copy.
gammaFactor?
number
(optional).
Returns
Color
Default Value
2.0.
copyLinearToGamma()
copyLinearToGamma(
color,gammaFactor?):Color
Copies the given color into this color, and then converts this color from linear space to gamma space by taking r, g and b to the power of 1 / Float| gammaFactor.
Parameters
color
Color
Color to copy.
gammaFactor?
number
(optional).
Returns
Color
Default Value
2.0.
convertGammaToLinear()
convertGammaToLinear(
gammaFactor?):Color
Converts this color from gamma space to linear space by taking .r| r, .g| g and .b| b to the power of Float| gammaFactor.
Parameters
gammaFactor?
number
(optional).
Returns
Color
Default Value
2.0.
convertLinearToGamma()
convertLinearToGamma(
gammaFactor?):Color
Converts this color from linear space to gamma space by taking .r| r, .g| g and .b| b to the power of 1 / Float| gammaFactor.
Parameters
gammaFactor?
number
(optional).
Returns
Color
Default Value
2.0.
copySRGBToLinear()
copySRGBToLinear(
color):Color
Copies the given color into this color, and then converts this color from sRGB space to linear space.
Parameters
color
Color
Color to copy.
Returns
Color
copyLinearToSRGB()
copyLinearToSRGB(
color):Color
Copies the given color into this color, and then converts this color from linear space to sRGB space.
Parameters
color
Color
Color to copy.
Returns
Color
convertSRGBToLinear()
convertSRGBToLinear():
Color
Converts this color from sRGB space to linear space.
Returns
Color
convertLinearToSRGB()
convertLinearToSRGB():
Color
Converts this color from linear space to sRGB space.
Returns
Color
getHex()
getHex():
number
Returns the hexadecimal value of this color.
Returns
number
getHexString()
getHexString():
string
Returns the hexadecimal value of this color as a string (for example, ‘FFFFFF’).
Returns
string
getHSL()
getHSL(
target):HSL
Convert this Color’s .r| r, .g| g and .b| b values to HSL
format and returns an object of the form:
{ h: 0, s: 0, l: 0 }
Parameters
target
HSL
the result will be copied into this Object. Adds h, s and l keys to the object (if not already present).
Returns
HSL
getStyle()
getStyle():
string
Returns the value of this color as a CSS style string. Example: rgb(255,0,0).
Returns
string
add()
add(
color):Color
Adds the RGB values of color to the RGB values of this color.
Parameters
color
Color
Returns
Color
addColors()
addColors(
color1,color2):Color
Sets this color’s RGB values to the sum of the RGB values of color1 and color2.
Parameters
color1
Color
color2
Color
Returns
Color
addScalar()
addScalar(
s):Color
Adds Number| s to the RGB values of this color.
Parameters
s
number
Returns
Color
sub()
sub(
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.
Parameters
color
Color
Returns
Color
multiply()
multiply(
color):Color
Multiplies this color’s RGB values by the given color’s RGB values.
Parameters
color
Color
Returns
Color
multiplyScalar()
multiplyScalar(
s):Color
Multiplies this color’s RGB values by s.
Parameters
s
number
Returns
Color
lerp()
lerp(
color,alpha):Color
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.
Parameters
color
Color
color to converge on.
alpha
number
interpolation factor in the closed interval [0, 1].
Returns
Color
equals()
equals(
c):boolean
Compares the RGB values of color with those of this object. Returns true if they are the same, false otherwise.
Parameters
c
Color
Returns
boolean
fromArray()
fromArray(
array,offset?):Color
Sets this color’s components based on an array formatted like [ r, g, b ].
Parameters
array
ArrayLike<number>
Array of floats in the form [ r, g, b ].
offset?
number
An optional offset into the array.
Returns
Color
getNumberCount()
getNumberCount():
number
r,g,b return 3
Returns
number
toArray()
toArray(
array?,offset?):number[]
Returns an array of the form [ r, g, b ].
Parameters
array?
number[]
An optional array to store the color to.
offset?
number
An optional offset into the array.
Returns
number[]
offsetHSL()
offsetHSL(
h,s,l):Color
Adds the given h, s, and l to this color’s values. Internally, this converts the color’s r, g and b values to HSL, adds h,s,l, and then converts the color back to RGB.
Parameters
h
number
s
number
l
number
Returns
Color
lerpHSL()
lerpHSL(
color,alpha):this
Linearly interpolates this color’s HSL values toward the HSL values of the passed argument. It differs from the classic 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.
Parameters
color
Color
alpha
number
Returns
this
hex2string()
statichex2string(hex):string
Converts a hexadecimal color number to a string.
Color.hex2string(0xffffff); -hex - Number in hex (e.g., 0xffffff) -return The string color (e.g., ”#ffffff”).
Parameters
hex
number
Returns
string
hex2rgb()
statichex2rgb(hex,out?):number[] |Float32Array<ArrayBufferLike>
Converts a hexadecimal color number to an [R, G, B] array of normalized floats (numbers from 0.0 to 1.0).
Color.hex2rgb(0xffffff); // returns [1, 1, 1] -hex The hexadecimal number to convert -out If supplied, this array will be used rather than returning a new one -return An array representing the [R, G, B] of the color where all values are floats.
Parameters
hex
number
out?
number[] | Float32Array<ArrayBufferLike>
Returns
number[] | Float32Array<ArrayBufferLike>
rgb2hex()
staticrgb2hex(rgb):number
Converts a color as an [R, G, B] array to a hex number
Parameters
rgb
number[] | Float32Array<ArrayBufferLike>
Returns
number
string2hex()
staticstring2hex(string):number
Converts a hexadecimal string to a hexadecimal color number.
Color.string2hex("#ffffff"); // returns 0xffffff -string - The string color (e.g., ”#ffffff”) -return Number in hexadecimal.
Parameters
string
string
Returns
number