Skip to content

randomHexShort() Helper Method

The randomHexShort() Helper Method module returns a randomized and formatted "shorthand" hex code.

PLEASE BE AWARE

When using the randomHexShort() Helper Method to generate a random shorthand hex code, the nature of shortened hex codes means that the normal range of RGB color space colors (16,777,216 colors) is drastically reduced to a total possible number of 4,096 values!

A really cool page to check out to visually see both the vastness and the finite amount of 3 digit hex code available is https://borderleft.com/toolbox/hex/.

Import

To import the randomHexShort() Helper Method:

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

Arguments

Returns: String

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

Use Example

javascript
console.log( randomHexShort() )
// returns (string): "#fff", "#333", "#168", "#bd1" etc.

Module Code

ts
// ./src/helpers/randomHexShort.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)]
    const g = chars[getRandomInt(min, max)]
    const b = chars[getRandomInt(min, max)]
    return '#' + r + g + b
}

Released under the MIT License