Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to generate a random color in HEX format with JavaScript

6 Answers

0 votes
function getRandomHexColor() {
  return ("#" + Math.random().toString(16).slice(2, 8));
}
 
console.log(getRandomHexColor());
 
   
     
     
/*
run:
     
#a7971c
     
*/

 



answered Jan 25, 2021 by avibootz
edited 2 days ago by avibootz
0 votes
function getRandomHexColor() {
  return ("#" + Math.random().toString(16).slice(2, 8));
}


document.body.style.background = getRandomHexColor();

  
  
    
    
/*
run:
    

    
*/

 



answered Jan 25, 2021 by avibootz
0 votes
function random_color() {
        return  '#' + Math.floor(Math.random()*16777215).toString(16);
}
 
const color = random_color();
  
console.log(color);
      
      
/*
run:
      
#b5ac85
 
      
*/
 

 



answered 2 days ago by avibootz
0 votes
function generateRandomHexColor() {
  const hexChars = "0123456789ABCDEF";
  let hex = "";
   
  for (let i = 0; i < 6; i++) {
    hex += hexChars[Math.floor(Math.random() * 16)];
  }
   
  return hex;
}
 
console.log(`Random HEX Color: #${generateRandomHexColor()}`); 
 
 
/*
run:
 
Random HEX Color: #8D279F
 
*/

 



answered 2 days ago by avibootz
0 votes
/**
 * Generate a random color in hex format (#RRGGBB).
 * This program demonstrates how numbers and bits are used
 * to produce a valid 24‑bit color value.
 */

/**
 * Create a 24‑bit random integer (0x000000–0xFFFFFF).
 * Math.random() gives a floating‑point number in [0, 1),
 * so multiplying by 0x1000000 (2^24) gives a range of 24 bits.
 */
function randomColorInt() {
  // 24 bits → values from 0 to 16,777,215 (0xFFFFFF)
  return Math.floor(Math.random() * 0x1000000);
}

/**
 * Convert a 24‑bit integer into a hex color string.
 * padStart(6, "0") ensures the hex string is always 6 characters.
 */
function intToHexColor(intValue) {
  // Convert number → hex string (e.g., "3fa2c")
  const hex = intValue.toString(16);

  // Ensure 6 hex digits (e.g., "003fa2c")
  const padded = hex.padStart(6, "0");

  // Add the leading "#"
  return `#${padded}`;
}

/**
 * Produce a random hex color by combining the two functions.
 */
function generateRandomHexColor() {
  const value = randomColorInt();     // 24‑bit random number
  const hexColor = intToHexColor(value); // Convert to #RRGGBB
  
  return { value, hexColor };
}

// Run the program
const result = generateRandomHexColor();
console.log("Random 24‑bit value:", result.value);
console.log("Hex color:", result.hexColor);


/*
run:

Random 24‑bit value: 8950214
Hex color: #8891c6

*/

 



answered 2 days ago by avibootz
0 votes
/**
 * Generates a random hex color code in the format #RRGGBB
 * @returns {string} Random hex color
 */
function getRandomHexColor() {
    // Generate a random integer between 0 and 0xFFFFFF (16777215)
    const randomInt = Math.floor(Math.random() * 0xFFFFFF);

    // Convert to hexadecimal and pad with leading zeros if needed
    const hexColor = "#" + randomInt.toString(16).padStart(6, "0");

    return hexColor;
}

// Example usage:
console.log(getRandomHexColor()); 


/*
run:

#497ca8

*/

 



answered 2 days ago by avibootz
...