Class: Color

pc.Color

Representation of an RGBA color

Constructor

new Color(ropt, gopt, bopt, aopt)

Create a new Color object
Parameters:
Name Type Attributes Description
r Number <optional>
The value of the red component (0-1). If r is an array of length 3 or 4, the array will be used to populate all components.
g Number <optional>
The value of the green component (0-1)
b Number <optional>
The value of the blue component (0-1)
a Number <optional>
The value of the alpha component (0-1)
Properties:
Name Type Description
r Number The red component of the color
g Number The green component of the color
b Number The blue component of the color
a Number The alpha component of the color
Source:

Methods

clone() → {pc.Color}

Returns a clone of the specified color.
Source:
Returns:
A duplicate color object
Type
pc.Color

copy(rhs) → {pc.Color}

Copies the contents of a source color to a destination color.
Parameters:
Name Type Description
rhs pc.Color A color to copy to the specified color.
Source:
Returns:
Self for chaining
Type
pc.Color
Example
var src = new pc.Color(1, 0, 0, 1);
var dst = new pc.Color();

dst.copy(src);

console.log("The two colors are " + (dst.equals(src) ? "equal" : "different"));

fromString(hex) → {pc.Color}

Set the values of the color from a string representation '#11223344' or '#112233'.
Parameters:
Name Type Description
hex String A string representation in the format '#RRGGBBAA' or '#RRGGBB'. Where RR, GG, BB, AA are red, green, blue and alpha values. This is the same format used in HTML/CSS.
Source:
Returns:
Self for chaining
Type
pc.Color

lerp(lhs, rhs, alpha) → {pc.Color}

Returns the result of a linear interpolation between two specified colors
Parameters:
Name Type Description
lhs pc.Color The color to interpolate from
rhs pc.Color The color to interpolate to.
alpha Number The value controlling the point of interpolation. Between 0 and 1, the linear interpolant will occur on a straight line between lhs and rhs. Outside of this range, the linear interpolant will occur on a ray extrapolated from this line.
Source:
Returns:
Self for chaining.
Type
pc.Color
Example
var a = new pc.Color(0, 0, 0);
var b = new pc.Color(1, 1, 0.5);
var r = new pc.Color();

r.lerp(a, b, 0);   // r is equal to a
r.lerp(a, b, 0.5); // r is 0.5, 0.5, 0.25
r.lerp(a, b, 1);   // r is equal to b

set(r, g, b, aopt) → {pc.Color}

Assign values to the color components, including alpha
Parameters:
Name Type Attributes Description
r Number The value for red (0-1)
g Number The value for blue (0-1)
b Number The value for green (0-1)
a Number <optional>
The value for the alpha (0-1), defaults to 1
Source:
Returns:
Self for chaining
Type
pc.Color

toString(alpha) → {String}

Converts the color to string form. The format is '#RRGGBBAA', where RR, GG, BB, AA are the red, green, blue and alpha values. When the alpha value is not included (the default), this is the same format as used in HTML/CSS.
Parameters:
Name Type Description
alpha Boolean If true, the output string will include the alpha value.
Source:
Returns:
The color in string form.
Type
String
Example
var c = new pc.Color(1, 1, 1);
// Should output '#ffffffff'
console.log(c.toString());