Reference Source
public class | source

UtilsSingleton

You can directly use an instance of this class. Utils

A utilities class

Constructor Summary

Public Constructor
public

Member Summary

Public Members
public

version: *

The version of the module.

Private Members
private

The logger module this module uses to log errors / logs

private

An array storing all the reserved unique IDS

private

Object storing all color objects being transitioned at the moment

Method Summary

Public Methods
public

addIntensity(pColor: string | number, pPercent: number): *

Add intensity to this color to get a brighter or dimmer effect

public

calculateIconOffset(pIconSize: Object, pAnchor: Object, pScale: Object): Object

Calculate the icon offset to compensate for a non-zero anchor.

public

Calculates the new position of a point based on distance and angle.

public

calculateProportionalLength(pCurrent: number, pMax: number, pTotalLength: number): number

Calculates the proportional length based on a current value, a maximum value, and a specified total length.

public

Cancels an ongoing transition

public

centerRectangleOnParent(pChildWidth: number, pChildHeight: number, pParentWidth: number, pParentHeight: number, pParentX: number, pParentY: number): Object

Centers a rectangle (defined by its dimensions) within a parent rectangle.

public

clamp(pNumber: number, pMin: number, pMax: number): number

Clamps a number between a minimum and maximum value.

public

Converts a raw angle to be the proper angle in Vylocity.

public

decimalRand(pNum1: number, pNum2: number, pPlaces: number): number

Generates a random decimal number between two numbers with a specified number of decimal places.

public

decimalToHex(pDecimal: number, pChars: number): *

Converts a color in decimal format into hex format

public

flooredLerp(pStart: number, pEnd: number, pAmount: number): number

Linearly interpolates between two values by a specified amount and returns the result as a floored integer.

public

Formats a number by rounding it to the nearest integer and adding commas to separate thousands places.

public

generateID(pIDLength: string): *

Generates a unique id

public

getAngle(pStartPoint: Object, pEndPoint: Object): number

Gets the angle between two points

public

getAngle2(pStartPoint: Object, pEndPoint: Object): number

Gets the angle between two points but in VYLO / PIXI coordinate space.

public

Calculates the angle (in radians) from a given direction.

public

Calculates the compass direction based on the given angle.

public

getDistance(pStartPoint: Object, pEndPoint: Object): number

Calculates the Euclidean distance between two points in a two-dimensional space.

public

Gets the inverse direction of the direction passed

public

getPercentage(pValue: number, pTotalValue: number): number

Calculates the percentage of a value relative to a total value.

public

getPointRotated(pRect: object, pTheta: number, pPoint: object): object

Calculates the position of a point after rotating it around a center point by a given angle.

public

Calculates the position of a rectangle's corner points and center point after rotating it around a center point by a given angle.

public

Generates a random angle in radians.

public

Gets a random color

public

getRandomColorBetween(pColor1: number | string, pColor2: number | string, pAmount: number): string

Gets a random color between two colors

public

grabColor(pSwitch: string | number, g: number, b: number): ColorObject

Convert a color to different formats or get a random color

public

Converts a hexadecimal color value to a decimal value.

public

Converts a hexadecimal color value to an RGB color value.

public

lerp(pStart: number, pEnd: number, pAmount: number): number

Linearly interpolates between two values by a specified amount.

public

linearDecay(pInitialValue: number, pCurrentTime: number, pMaxTime: number, pDecayRate: number): number

Calculates the linear decay of a variable over time.

public

normalize(pVal: number, pMin: number, pMax: number): number

Normalizes a value between a minimum and maximum value.

public

normalizeRanged(pVal: number, pMin: number, pMax: number): number

Normalizes a value between a minimum and maximum value, clamped to the range of -1 to 1.

public

pick(pArray: Array): *

Returns a random element from the given array.

public

prob(pChance: number): boolean

Returns true with probability proportional to the given number.

public

rand(pNum1: number, pNum2: number): number

Generates a random decimal number between two numbers with a specified number of decimal places.

public

removeProperties(pObject: object, pExclude: Array)

Removes properties from an object except those listed in the exclude array.

public

rgbToDecimal(pR: number, pG: number, pB: number): *

Converts RGB color values to a decimal value.

public

rgbToHex(pR: number, pG: number, pB: number): *

Converts an RGB color value to a hexadecimal color value.

public

round(pNumber: number, pPlace: number): number

Rounds a number to a specified number of decimal places.

public

toDegrees(pRadians: number): number

Converts radians to degrees.

public

toRadians(pDegrees: number): number

Converts degrees to radians.

public

transitionColor(pInstance: Object, pStartColor: string | number, pEndColor: string | number, pDuration: number, pIterativeCallback: Function, pEndCallback: Function): *

Transition a color to another color in pDuration time.

public

within(pVal: number, pMin: number, pMax: number): boolean

Checks if a value is within a range of minimum and maximum values (inclusive).

Public Constructors

public constructor() source

Public Members

public version: * source

The version of the module.

Private Members

private logger: Object source

The logger module this module uses to log errors / logs

private storedIDs: Array source

An array storing all the reserved unique IDS

private transitions: Object source

Object storing all color objects being transitioned at the moment

Public Methods

public addIntensity(pColor: string | number, pPercent: number): * source

Add intensity to this color to get a brighter or dimmer effect

Params:

NameTypeAttributeDescription
pColor string | number

Color in hex format or decimal format

pPercent number

The percent of brightness to add to this color

Return:

*

public calculateIconOffset(pIconSize: Object, pAnchor: Object, pScale: Object): Object source

Calculate the icon offset to compensate for a non-zero anchor.

Params:

NameTypeAttributeDescription
pIconSize Object
  • optional

The size of the icon with properties .x and .y.

pIconSize.width number
  • optional
  • default: 32

The size of the icon's width.

pIconSize.height number
  • optional
  • default: 32

The size of the icon's height'.

pAnchor Object
  • optional

The anchor point with properties .x and .y.

pAnchor.x number
  • optional
  • default: 0.5

The anchor's x value.

pAnchor.y number
  • optional
  • default: 0.5

The anchor's y value.

pScale Object
  • optional

The scale factor applied to the object with properties .x and .y.

pScale.x number
  • optional
  • default: 1

The scale's y value.

pScale.y number
  • optional
  • default: 1

The scale's y value.

Return:

Object

The calculated icon offset with properties .x and .y.

public calculateNewPositionFromDistanceAndAngle(pPoint: Object, pDistance: number, pAngle: number): Object source

Calculates the new position of a point based on distance and angle.

Params:

NameTypeAttributeDescription
pPoint Object

The initial position of the point with x and y coordinates.

pPoint.x number

The initial x-coordinate of the point.

pPoint.y number

The initial y-coordinate of the point.

pDistance number

The distance by which to move the point.

pAngle number

The angle (in radians) at which to move the point.

Return:

Object

The new position of the point after moving by the specified distance and angle.

Example:

const initialPosition = { x: 10, y: 20 };
const distance = 5;
const angleInRadians = 0.785398; // 45 degrees
const newPosition = calculateNewPositionFromDistanceAndAngle(initialPosition, distance, angleInDegrees);
// Returns the new position of the point after moving by 5 units at a 45-degree angle.

public calculateProportionalLength(pCurrent: number, pMax: number, pTotalLength: number): number source

Calculates the proportional length based on a current value, a maximum value, and a specified total length.

Params:

NameTypeAttributeDescription
pCurrent number

The current value to be scaled.

pMax number

The maximum value for scaling.

pTotalLength number

The specified total length.

Return:

number

The proportional length based on the current value, maximum value, and total length.

Example:

const current = 25;
const max = 50;
const totalLength = 100;
const proportionalLength = calculateProportionalLength(current, max, totalLength); // 50
// Returns the proportional length based on the current value, maximum value, and total length.

public cancelTransitionColor(pID: string) source

Cancels an ongoing transition

Params:

NameTypeAttributeDescription
pID string

The ID of the ongoing transition to cancel

public centerRectangleOnParent(pChildWidth: number, pChildHeight: number, pParentWidth: number, pParentHeight: number, pParentX: number, pParentY: number): Object source

Centers a rectangle (defined by its dimensions) within a parent rectangle.

Params:

NameTypeAttributeDescription
pChildWidth number

The width of the child rectangle.

pChildHeight number

The height of the child rectangle.

pParentWidth number

The width of the parent rectangle.

pParentHeight number

The height of the parent rectangle.

pParentX number

The x-coordinate of the parent rectangle.

pParentY number

The y-coordinate of the parent rectangle.

Return:

Object

An object representing the new coordinates of the centered rectangle: { x: centerX, y: centerY }.

Example:

const childWidth = 50;
const childHeight = 30;
const parentWidth = 100;
const parentHeight = 80;
const parentX = 20;
const parentY = 10;
const centeredCoordinates = centerRectangleOnParent(childWidth, childHeight, parentWidth, parentHeight, parentX, parentY);
// Returns {x: 45, y: 35}

public clamp(pNumber: number, pMin: number, pMax: number): number source

Clamps a number between a minimum and maximum value.

Params:

NameTypeAttributeDescription
pNumber number

The number to clamp.

pMin number
  • optional
  • default: 0

The minimum value to clamp the number to. Defaults to 0 if not provided.

pMax number
  • optional
  • default: 1

The maximum value to clamp the number to. Defaults to 1 if not provided.

Return:

number

The clamped number between the minimum and maximum values.

public convertRaWAngleToVyloCoords(pAngle: number): * source

Converts a raw angle to be the proper angle in Vylocity. By removing 180 degrees

Params:

NameTypeAttributeDescription
pAngle number

The angle to convert.

Return:

*

The converted angle

public decimalRand(pNum1: number, pNum2: number, pPlaces: number): number source

Generates a random decimal number between two numbers with a specified number of decimal places.

Params:

NameTypeAttributeDescription
pNum1 number

The first number to use for generating the random decimal number.

pNum2 number

The second number to use for generating the random decimal number.

pPlaces number
  • optional
  • default: 1

The number of decimal places to include in the generated random decimal number. Defaults to 1 if not provided.

Return:

number

A random decimal number between the two numbers with the specified number of decimal places.

public decimalToHex(pDecimal: number, pChars: number): * source

Converts a color in decimal format into hex format

Params:

NameTypeAttributeDescription
pDecimal number

The color in decimal format

pChars number

The length to make the hex string

Return:

*

The decimal color converted into hex format

public flooredLerp(pStart: number, pEnd: number, pAmount: number): number source

Linearly interpolates between two values by a specified amount and returns the result as a floored integer.

Params:

NameTypeAttributeDescription
pStart number

The start value to interpolate from.

pEnd number

The end value to interpolate to.

pAmount number

The amount to interpolate between the start and end values.

Return:

number

The interpolated value between the start and end values based on the specified amount, rounded down to the nearest integer.

public formatIntegerWithCommas(pNum: number): string source

Formats a number by rounding it to the nearest integer and adding commas to separate thousands places.

Params:

NameTypeAttributeDescription
pNum number

The number to format.

Return:

string

A string representation of the formatted number.

public generateID(pIDLength: string): * source

Generates a unique id

Params:

NameTypeAttributeDescription
pIDLength string

The length of the ID to create

Return:

*

A unique ID

public getAngle(pStartPoint: Object, pEndPoint: Object): number source

Gets the angle between two points

Params:

NameTypeAttributeDescription
pStartPoint Object

The starting point

pEndPoint Object

The ending point

Return:

number

The angle between the starting point and the ending point

public getAngle2(pStartPoint: Object, pEndPoint: Object): number source

Gets the angle between two points but in VYLO / PIXI coordinate space. Removes 180 degrees from a raw angle

Params:

NameTypeAttributeDescription
pStartPoint Object

The starting point

pEndPoint Object

The ending point

Return:

number

The angle between the starting point and the ending point

public getAngleFromDirection(pDirection: string): number source

Calculates the angle (in radians) from a given direction.

Params:

NameTypeAttributeDescription
pDirection string

The direction to calculate the angle from.

Return:

number

The angle (in radians) associated with the given direction.

Throw:

Error

Throws an error if the direction is not recognized.

public getDirection(pAngle: number): string source

Calculates the compass direction based on the given angle.

Params:

NameTypeAttributeDescription
pAngle number

The angle in radians.

Return:

string

The compass direction (e.g., 'east', 'southeast', 'south', etc.).

Example:

const angle = Math.PI / 4; // 45 degrees in radians
const direction = getDirection(angle); // Returns 'northeast'

public getDistance(pStartPoint: Object, pEndPoint: Object): number source

Calculates the Euclidean distance between two points in a two-dimensional space.

Params:

NameTypeAttributeDescription
pStartPoint Object

The starting point with x and y coordinates.

pStartPoint.x number

The x-coordinate of the starting point.

pStartPoint.y number

The y-coordinate of the starting point.

pEndPoint Object

The ending point with x and y coordinates.

pEndPoint.x number

The x-coordinate of the ending point.

pEndPoint.y number

The y-coordinate of the ending point.

Return:

number

The Euclidean distance between the two points.

Example:

const startPoint = { x: 1, y: 2 };
const endPoint = { x: 4, y: 6 };
const distance = getDistance(startPoint, endPoint); // 5
// Returns the Euclidean distance between the points (1, 2) and (4, 6).

public getInverseDirection(pDirection: string): string source

Gets the inverse direction of the direction passed

Params:

NameTypeAttributeDescription
pDirection string

The direction to get the inverse of.

Return:

string

The inverse direction

public getPercentage(pValue: number, pTotalValue: number): number source

Calculates the percentage of a value relative to a total value.

Params:

NameTypeAttributeDescription
pValue number

The value to calculate the percentage of.

pTotalValue number

The total value to calculate the percentage relative to.

Return:

number

The percentage of the value relative to the total value.

public getPointRotated(pRect: object, pTheta: number, pPoint: object): object source

Calculates the position of a point after rotating it around a center point by a given angle.

Params:

NameTypeAttributeDescription
pRect object

The rectangle object to rotate the point around. pRect.anchor.x and pRecent.anchor.y is used to control the "center" of the rectangle.

pTheta number

The angle (in radians) to rotate the point by.

pPoint object

The point object to rotate around the center of the rectangle.

pPoint.x number

The x-coordinate of the point to rotate.

pPoint.y number

The y-coordinate of the point to rotate.

Return:

object

An object with the rotated point's new x and y coordinates.

public getPointsOfRotatedRect(pRect: object, pTheta: number): object source

Calculates the position of a rectangle's corner points and center point after rotating it around a center point by a given angle.

Params:

NameTypeAttributeDescription
pRect object

The rectangle object to rotate the point around. pRect.anchor.x and pRecent.anchor.y is used to control the "center" of the rectangle.

pTheta number

The angle (in radians) to rotate the point by.

Return:

object

An object with the rotated rectangle's new corner points and center points.

public getRandomAngle(): number source

Generates a random angle in radians.

Return:

number

A random angle in radians.

public getRandomColor(): string source

Gets a random color

Return:

string

A random color in the hex format

public getRandomColorBetween(pColor1: number | string, pColor2: number | string, pAmount: number): string source

Gets a random color between two colors

Params:

NameTypeAttributeDescription
pColor1 number | string

The first color to get a color between

pColor2 number | string

The second color to get a color between

pAmount number
  • optional
  • default: 0.5

The closer the random color will be to either input colors on a range of 0-1 0 to 0.5 (closer to pColor1) 0.5 to 1 (closer to pColor2)

Return:

string

A random color in the decimal format

public grabColor(pSwitch: string | number, g: number, b: number): ColorObject source

Convert a color to different formats or get a random color

Params:

NameTypeAttributeDescription
pSwitch string | number

A hex string representing a color (with or without the tag) A color formatted in the decimal format. Or the r value of a rgb color.

g number
  • optional

g value of a rgb color

b number
  • optional

b value of a rgb color

Return:

ColorObject

A color object with various different export options. hex, hexTagless, rgb, rgbArray, rgbObject, rgbNormal, decimal formats.

public hexToDecimal(pHex: string): number source

Converts a hexadecimal color value to a decimal value.

Params:

NameTypeAttributeDescription
pHex string

The hexadecimal color value to convert (e.g. "#FF0000" for red).

Return:

number

The decimal representation of the hexadecimal color value.

public hexToRgb(pHex: string): Array source

Converts a hexadecimal color value to an RGB color value.

Params:

NameTypeAttributeDescription
pHex string

The hexadecimal color value to convert (e.g. "#FF0000" for red).

Return:

Array

An array containing the red, green, and blue components of the RGB color value.

public lerp(pStart: number, pEnd: number, pAmount: number): number source

Linearly interpolates between two values by a specified amount.

Params:

NameTypeAttributeDescription
pStart number

The start value to interpolate from.

pEnd number

The end value to interpolate to.

pAmount number

The amount to interpolate between the start and end values.

Return:

number

The interpolated value between the start and end values based on the specified amount.

public linearDecay(pInitialValue: number, pCurrentTime: number, pMaxTime: number, pDecayRate: number): number source

Calculates the linear decay of a variable over time.

Params:

NameTypeAttributeDescription
pInitialValue number

The initial value of the variable.

pCurrentTime number

The current time at which to calculate the variable value.

pMaxTime number

The maximum time for the decay process.

pDecayRate number
  • optional
  • default: 0.5

The decay rate (default is 0.5).

Return:

number

The remaining value of the variable after linear decay.

Example:

const initialValue = 100;
const currentTime = 50;
const maxTime = 1000;
const decayRate = 0.3;
const remainingValue = linearDecay(initialValue, currentTime, maxTime, decayRate);
// Returns the remaining value after linear decay.

public normalize(pVal: number, pMin: number, pMax: number): number source

Normalizes a value between a minimum and maximum value.

Params:

NameTypeAttributeDescription
pVal number

The value to normalize.

pMin number

The minimum value for normalization.

pMax number

The maximum value for normalization.

Return:

number

The normalized value between 0 and 1 based on the input value's position between the minimum and maximum values. If the difference between pMax and pMin is 0, returns 1 to avoid dividing by zero.

public normalizeRanged(pVal: number, pMin: number, pMax: number): number source

Normalizes a value between a minimum and maximum value, clamped to the range of -1 to 1.

Params:

NameTypeAttributeDescription
pVal number

The value to normalize.

pMin number

The minimum value for normalization.

pMax number

The maximum value for normalization.

Return:

number

The normalized and clamped value between -1 and 1 based on the input value's position between the minimum and maximum values. If the difference between pMax and pMin is 0, returns 1 to avoid dividing by zero.

public pick(pArray: Array): * source

Returns a random element from the given array.

Params:

NameTypeAttributeDescription
pArray Array

The input array.

Return:

*

A random element from the array.

public prob(pChance: number): boolean source

Returns true with probability proportional to the given number. The higher the number, the higher the chance of returning true.

Params:

NameTypeAttributeDescription
pChance number

The probability value, between 0 and 100 (inclusive).

Return:

boolean

Returns true or false, based on the probability value.

public rand(pNum1: number, pNum2: number): number source

Generates a random decimal number between two numbers with a specified number of decimal places.

Params:

NameTypeAttributeDescription
pNum1 number

The first number to use for generating the random decimal number.

pNum2 number

The second number to use for generating the random decimal number.

Return:

number

A random decimal number between the two numbers with the specified number of decimal places.

public removeProperties(pObject: object, pExclude: Array) source

Removes properties from an object except those listed in the exclude array.

Params:

NameTypeAttributeDescription
pObject object

The object to remove properties from.

pExclude Array

The array of property names to exclude from removal.

public rgbToDecimal(pR: number, pG: number, pB: number): * source

Converts RGB color values to a decimal value.

Params:

NameTypeAttributeDescription
pR number

The red component of the RGB color value (0-255).

pG number

The green component of the RGB color value (0-255).

pB number

The blue component of the RGB color value (0-255).

Return:

*

public rgbToHex(pR: number, pG: number, pB: number): * source

Converts an RGB color value to a hexadecimal color value.

Params:

NameTypeAttributeDescription
pR number

The red component of the RGB color value (0-255).

pG number

The green component of the RGB color value (0-255).

pB number

The blue component of the RGB color value (0-255).

Return:

*

public round(pNumber: number, pPlace: number): number source

Rounds a number to a specified number of decimal places.

Params:

NameTypeAttributeDescription
pNumber number

The number to round.

pPlace number
  • optional
  • default: 1

The number of decimal places to round to. Defaults to 1 if not provided.

Return:

number

The rounded number to the specified number of decimal places.

public toDegrees(pRadians: number): number source

Converts radians to degrees.

Params:

NameTypeAttributeDescription
pRadians number

The angle in radians.

Return:

number

The angle in degrees.

public toRadians(pDegrees: number): number source

Converts degrees to radians.

Params:

NameTypeAttributeDescription
pDegrees number

The angle in degrees.

Return:

number

The angle in radians.

public transitionColor(pInstance: Object, pStartColor: string | number, pEndColor: string | number, pDuration: number, pIterativeCallback: Function, pEndCallback: Function): * source

Transition a color to another color in pDuration time.

Params:

NameTypeAttributeDescription
pInstance Object

The instance to transition it's color property. pInstance's color will be transitioned either via pInstance.color = newColor or pInstance.color.tint = newColor (if the color is defined as an object)

pStartColor string | number

The start color

pEndColor string | number

The end color

pDuration number

The duration of the transition

pIterativeCallback Function

Callback to call every tick of the transition

pEndCallback Function

Callback to call at the end of the transition

Return:

*

An ID that references this transition to be passed to cancelTransition to stop an ongoing transition.

public within(pVal: number, pMin: number, pMax: number): boolean source

Checks if a value is within a range of minimum and maximum values (inclusive).

Params:

NameTypeAttributeDescription
pVal number

The value to check.

pMin number

The minimum value of the range to check against.

pMax number

The maximum value of the range to check against.

Return:

boolean

True if the value is within the range (inclusive), false otherwise.