Here's a function in TypeScript that generates a random hex color:
index.ts284 chars8 lines
Explanation:
Math.random()
generates a random number between 0 and 1.Math.floor()
rounds down that number to the nearest integer.16777215
is 0xFFFFFF
in decimal, which is the highest possible value for a 24-bit RGB color code..toString(16)
converts the random number to a hexadecimal string.You can use this function in your TypeScript code like this:
index.ts85 chars3 lines
gistlibby LogSnag