Home Reference Source

Function

Static Public Summary
public

Convert a hexadecimal color string to an rgb() string.

public

findGreaterNumber(a: *, b: *): *

public

generateCanvas(document: object, id: string, dimensions: object)

Creates a canvas element and append it to the document body.

public

generateColor(seedMultiplier: number, seed: number): ...ColorObject

Generates a color object from a modified seed using the tinycolor2 library.

public

generateCount(limit: number, seedMultiplier: number, seed: number): number

Generates a number used for decision making based on the seed and a given multiplier.

public

generateSeed(seedString: string): *

Generate the seed from a seed string.

public

Generates a seed multiplier converting the characters of the provided string to numbers name.

public

Gets the last digit from a number.

public

hexToRgb(hex: string): {r: number, b: number, g: number}

Turn a hexadecimal color string into an rgb() object.

public

modifySeed(seed: number, seedMultiplier: number): number

Modify a seed by multiplying it by a value.

public

Takes an aspect ratio string, e.g.: 1:2, 2:3, 3:5, and returns an object with height (h) and width (w) keys.

public

Shuffles an array predictably using the Fisher Yates shuffle algorithm.

public

randomHex(seed: number, seedModifier: number): string

Generate a random hex color based on the seed.

public

Turn an aspect object into more realistic dimensions.

Static Public

public convertHex(hex: string): string source

import {convertHex} from '@carwin/flag-generator/src/utilities.js'

Convert a hexadecimal color string to an rgb() string.

Params:

NameTypeAttributeDescription
hex string

A hexadecimal color string.

Return:

string

An RGB color string.

Example:

// Returns rgb(255, 255, 255)
convertHex('#ffffff');

public findGreaterNumber(a: *, b: *): * source

import {findGreaterNumber} from '@carwin/flag-generator/src/utilities.js'

Params:

NameTypeAttributeDescription
a *
b *

Return:

*

public generateCanvas(document: object, id: string, dimensions: object) source

import {generateCanvas} from '@carwin/flag-generator/src/utilities.js'

Creates a canvas element and append it to the document body.

Params:

NameTypeAttributeDescription
document object

A document object.

id string

The Id to give the created canvas element.

dimensions object

An object containing height(h) and width (w) keys.

Example:

// Creates and appends the canvas with an id of 'myCanvas'.
generateCanvas(document, 'myCanvas');

public generateColor(seedMultiplier: number, seed: number): ...ColorObject source

import {generateColor} from '@carwin/flag-generator/src/utilities.js'

Generates a color object from a modified seed using the tinycolor2 library.

Params:

NameTypeAttributeDescription
seedMultiplier number

The multiplier used to alter the seed to generate values.

seed number

The seed number on which generation depends.

Return:

...ColorObject

A module:flag-generator/utilities~ColorObject.

Example:

// Generates a ColorObject with a primary color key of #575109;
const colorObject = generateColor(80857473, 0.06556305047688744);

public generateCount(limit: number, seedMultiplier: number, seed: number): number source

import {generateCount} from '@carwin/flag-generator/src/utilities.js'

Generates a number used for decision making based on the seed and a given multiplier.

Params:

NameTypeAttributeDescription
limit number

The maximum value of the count to be returned. Values above 9 are ignored.

seedMultiplier number

The multiplier used to alter the seed to generate values.

seed number

The seed number on which generation depends.

Return:

number

A single digit number between 0 and 9.

Example:

// Returns 1
generateCount(5, 0.8112494706388412, 0.6568774660735);

public generateSeed(seedString: string): * source

import {generateSeed} from '@carwin/flag-generator/src/utilities.js'

Generate the seed from a seed string.

Params:

NameTypeAttributeDescription
seedString string

A string on which to run the prng function.

Return:

*

Example:

// Returns 0.8112494706388412
generateSeed('test');

public generateSeedMultiplier(str: string): number source

import {generateSeedMultiplier} from '@carwin/flag-generator/src/utilities.js'

Generates a seed multiplier converting the characters of the provided string to numbers name.

Params:

NameTypeAttributeDescription
str string

A string value to turn into charcodes.

Return:

number

A multiplier to be used with seed multiplication based decision making.

Example:

// Returns 0.5363260631705106
generateSeedMultiplier('Border');

TODO:

  • I've read somewhere that parseFloat is dangerous without radx, I should figure out if that's true. @todo: handle the case where settings.seed may not be set.

public getLastDigit(n: number): number source

import {getLastDigit} from '@carwin/flag-generator/src/utilities.js'

Gets the last digit from a number.

Params:

NameTypeAttributeDescription
n number

A number.

Return:

number

The last digit of n.

Example:

// Returns 7
getLastDigit(.2357);

public hexToRgb(hex: string): {r: number, b: number, g: number} source

Turn a hexadecimal color string into an rgb() object.

Params:

NameTypeAttributeDescription
hex string

A hexadecimal color string.

Return:

{r: number, b: number, g: number}

An object containing RGB keys.

Example:

// returns {r: 255, g: 255, b: 255}
hexToRgb('#ffffff');

public modifySeed(seed: number, seedMultiplier: number): number source

import {modifySeed} from '@carwin/flag-generator/src/utilities.js'

Modify a seed by multiplying it by a value.

Params:

NameTypeAttributeDescription
seed number

The seed number on which generation depends.

seedMultiplier number

The multiplier used to alter the seed to generate values.

Return:

number

The product of seed and seedMultiplier.

Example:

// Returns 0.05483563
modifySeed(0.2602354456965794, 0.2107154537);

public processAspectRatioString(aspect: string): object source

import {processAspectRatioString} from '@carwin/flag-generator/src/utilities.js'

Takes an aspect ratio string, e.g.: 1:2, 2:3, 3:5, and returns an object with height (h) and width (w) keys.

Params:

NameTypeAttributeDescription
aspect string

A string describing a ratio.

Return:

object

An aspect ratio object containing height (h) and width (w) keys from the aspect ratio string.

Example:

// Returns
// {
//     h: 3,
//     w: 5,
// }
const aspectObject = processAspectRatioString('3:5');

TODO:

  • This function could be shortened significantly with a map callback.

public pseudoShuffle(arr: Array, seed: number): Array source

import {pseudoShuffle} from '@carwin/flag-generator/src/utilities.js'

Shuffles an array predictably using the Fisher Yates shuffle algorithm.

Params:

NameTypeAttributeDescription
arr Array

The array to be shuffled.

seed number

The pseudorandom string used to predictabbly apply the algorithm.

Return:

Array

The arr parameter, but shuffled.

Example:

// Returns [1, 2, 3, 5, 4]
const originalArray = [1, 2, 3, 4, 5]
const shuffledArray = pseudoShuffle(originalArray, 0.7243609520746538);

public randomHex(seed: number, seedModifier: number): string source

import {randomHex} from '@carwin/flag-generator/src/utilities.js'

Generate a random hex color based on the seed.

Params:

NameTypeAttributeDescription
seed number

The prng generated seed value under 1.

seedModifier number

A number used to perform modifications to the seed.

Return:

string

The pseudo-randomly generated hexadecimal color value.

Example:

// returns #2b32ad
randomHex(0.8112494706388412);

public setDimensionsFromAspectObject(aspect: object, multiplier: number): object source

import {setDimensionsFromAspectObject} from '@carwin/flag-generator/src/utilities.js'

Turn an aspect object into more realistic dimensions.

Params:

NameTypeAttributeDescription
aspect object

An aspect object.

multiplier number

A number to multiply the aspect keys by. Defaults to 100.

Return:

object

An object containing height (h) and width (w) keys representing dimensions.

Example:

// Returns
// {
//   h: 300,
//   w: 500,
// }
const dimensions = setDimensionsFromAspectObject({h: 3, w: 5});