Skip to content

randomHex() Helper Method

The randomHex() Helper Method module returns a randomized and formatted full length hex code.

Import

To import the randomHex() Helper Method:

javascript
import { randomHex } from '@obewds/vueventus'

Arguments

Returns: String

The randomHex() Helper Method does not have any arguments.

Use Example

javascript
console.log( randomHex() )
// returns (string): "#d0a1fc", "#8d0340" etc.

Module Code

ts
// ./src/helpers/randomHex.ts

import getRandomInt from './getRandomInt'
import hexadecimals from './hexadecimals'

export default function (): string {
    const chars = hexadecimals()
    const min = 0
    const max = 15
    const r = chars[getRandomInt(min, max)] + chars[getRandomInt(min, max)]
    const g = chars[getRandomInt(min, max)] + chars[getRandomInt(min, max)]
    const b = chars[getRandomInt(min, max)] + chars[getRandomInt(min, max)]
    return '#' + r + g + b
}

Released under the MIT License