LittleJS 1.31.0

Engine

Color

Color object (red, green, blue, alpha) with some helpful functions

Constructor

new Color(r, g, b, a)engineMath.js:1076

Create a color with the rgba components passed in, white by default, each 0 to 1, as rgb takes them

r number optional, default 1

red

g number optional, default 1

green

b number optional, default 1

blue

a number optional, default 1

alpha

let a = new Color;              // white
let b = new Color(1, 0, 0);     // red
let c = new Color(0, 0, 0, 0);  // transparent black
let d = rgb(0, 0, 1);         // blue using rgb color
let e = hsl(.3, 1, .5);         // green using hsl color

Members

Methods

Members

Alpha

Blue

Green

Red

Methods

Returns a copy of this color plus the color passed in

c Color

other color

Returns a copy of this color clamped to the valid range between 0 and 1

Returns a new color that is a copy of this

Returns a copy of this color divided by the color passed in

c Color

other color

HSLA() → [number, number, number, number]engineMath.js:1210

Returns this color expressed in hsla format

isValid() → booleanengineMath.js:1308

Checks if this is a valid color

lerp(c, percent) → ColorengineMath.js:1171

Returns a new color that is p percent between this and the color passed in, percent is clamped to 0-1

c Color

other color

percent number
multiply(c) → ColorengineMath.js:1149

Returns a copy of this color times the color passed in

c Color

other color

mutate(amount, alphaAmount) → ColorengineMath.js:1238

Returns a new color that has each component randomly adjusted

amount number optional, default 0.05
alphaAmount number optional, default 0
rgbaInt() → numberengineMath.js:1296

Returns this color expressed as 32 bit RGBA value

scale(scale, alphaScale) → ColorengineMath.js:1160

Returns a copy of this color scaled by the value passed in, alpha can be scaled separately

scale number
alphaScale number optional, default scale
set(r, g, b, a) → ColorengineMath.js:1102

Sets values of this color and returns self

r number optional, default 1

red

g number optional, default 1

green

b number optional, default 1

blue

a number optional, default 1

alpha

setAlpha(a) → ColorengineMath.js:1120

Sets the alpha of this color and returns self

a number optional, default 1

alpha

Sets this color from another color and returns self

c Color

other color

setHex(hex) → ColorengineMath.js:1267

Set this color from a hex code

hex string

html hex code

setHSLA(h, s, l, a) → ColorengineMath.js:1190

Sets this color given a hue, saturation, lightness, and alpha, each 0 to 1 as hsl takes them

  • The hue is not degrees: setHSLA(1/3, 1, .5) is green, and it wraps around, so 120 is red
h number optional, default 0

hue, 0 to 1 around the color wheel from red

s number optional, default 0

saturation, 0 to 1

l number optional, default 1

lightness, 0 to 1

a number optional, default 1

alpha, 0 to 1

subtract(c) → ColorengineMath.js:1144

Returns a copy of this color minus the color passed in

c Color

other color

toString(useAlpha) → stringengineMath.js:1254

Returns this color expressed as a hex color code

useAlpha boolean optional, default true

if alpha should be included in result

withAlpha(a) → ColorengineMath.js:1134

Returns a copy of this color with the alpha set

a number optional, default 1

alpha