Skip to main content
deleted 2 characters in body
Source Link
Liglo App
  • 3.8k
  • 4
  • 30
  • 55

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

const stringToColour = (str: string) => {
  constlet hash = 0;
  str.split('').forEach(char => {
    hash = char.charCodeAt(0) + ((hash << 5) - hash)
  })
  let colour = '#'
  for (let i = 0; i < 3; i++) {
    const value = (hash >> (i * 8)) & 0xff
    colour += value.toString(16).padStart(2, '0')
  }
  return colour
}

If you are using Eslint, you want to wrap this function in the

/* eslint-disable no-bitwise */
>> stringToColour func. definition here" <<
/* eslint-enable no-bitwise */

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

const stringToColour = (str: string) => {
  const hash = 0;
  str.split('').forEach(char => {
    hash = char.charCodeAt(0) + ((hash << 5) - hash)
  })
  let colour = '#'
  for (let i = 0; i < 3; i++) {
    const value = (hash >> (i * 8)) & 0xff
    colour += value.toString(16).padStart(2, '0')
  }
  return colour
}

If you are using Eslint, you want to wrap this function in the

/* eslint-disable no-bitwise */
>> stringToColour func. definition here" <<
/* eslint-enable no-bitwise */

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

const stringToColour = (str: string) => {
  let hash = 0;
  str.split('').forEach(char => {
    hash = char.charCodeAt(0) + ((hash << 5) - hash)
  })
  let colour = '#'
  for (let i = 0; i < 3; i++) {
    const value = (hash >> (i * 8)) & 0xff
    colour += value.toString(16).padStart(2, '0')
  }
  return colour
}

If you are using Eslint, you want to wrap this function in the

/* eslint-disable no-bitwise */
>> stringToColour func. definition here" <<
/* eslint-enable no-bitwise */

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

"var" shouldn't be use, please use "let". As for 2023 substr function is depreciated and bitwise is showing warning in eslint checks. We want to use Typescript in all examples of JS Stackoverflow even newbies should jump right on that.
Source Link

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

varconst stringToColour = function(str: string) => {
  varconst hash = 0;
  for (var i = 0; i < str.length; i++split('').forEach(char => {
    hash = strchar.charCodeAt(i0) + ((hash << 5) - hash);
  })
  varlet colour = '#';'#'
  for (varlet i = 0; i < 3; i++) {
    varconst value = (hash >> (i * 8)) & 0xFF;0xff
    colour += ('00' + value.toString(16)).substrpadStart(-2, '0');
  }
  return colour;colour
}

If you are using Eslint, you want to wrap this function in the

/* eslint-disable no-bitwise */
>> stringToColour func. definition here" <<
/* eslint-enable no-bitwise */

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

var stringToColour = function(str) {
  var hash = 0;
  for (var i = 0; i < str.length; i++) {
    hash = str.charCodeAt(i) + ((hash << 5) - hash);
  }
  var colour = '#';
  for (var i = 0; i < 3; i++) {
    var value = (hash >> (i * 8)) & 0xFF;
    colour += ('00' + value.toString(16)).substr(-2);
  }
  return colour;
}

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

const stringToColour = (str: string) => {
  const hash = 0;
  str.split('').forEach(char => {
    hash = char.charCodeAt(0) + ((hash << 5) - hash)
  })
  let colour = '#'
  for (let i = 0; i < 3; i++) {
    const value = (hash >> (i * 8)) & 0xff
    colour += value.toString(16).padStart(2, '0')
  }
  return colour
}

If you are using Eslint, you want to wrap this function in the

/* eslint-disable no-bitwise */
>> stringToColour func. definition here" <<
/* eslint-enable no-bitwise */

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

Rollback to Revision 1
Source Link
Martin Tournoij
  • 27.5k
  • 24
  • 107
  • 153

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

var stringToColour = function(str) {
 
    // str tovar hash
  = 0;
  for (var i = 0, hash = 0; i < str.length; i++) {
    hash = str.charCodeAt(i++i) + ((hash << 5) - hash));
 
  }
  //var int/hashcolour to= hex'#';
    for (var i = 0, colour = "#";0; i < 3; colouri++) +={
 ("00" + ( var value = (hash >> i++(i * 8)) & 0xFF)0xFF;
    colour += ('00' + value.toString(16)).slicesubstr(-2));
  }
    return colour;
}

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

var stringToColour = function(str) {
 
    // str to hash
     for (var i = 0, hash = 0; i < str.length; hash = str.charCodeAt(i++) + ((hash << 5) - hash));
 
    // int/hash to hex
    for (var i = 0, colour = "#"; i < 3; colour += ("00" + ((hash >> i++ * 8) & 0xFF).toString(16)).slice(-2));
  
    return colour;
}

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

Here's an adaptation of CD Sanchez' answer that consistently returns a 6-digit colour code:

var stringToColour = function(str) {
  var hash = 0;
  for (var i = 0; i < str.length; i++) {
    hash = str.charCodeAt(i) + ((hash << 5) - hash);
  }
  var colour = '#';
  for (var i = 0; i < 3; i++) {
    var value = (hash >> (i * 8)) & 0xFF;
    colour += ('00' + value.toString(16)).substr(-2);
  }
  return colour;
}

Usage:

stringToColour("greenish");
// -> #9bc63b

Example:

http://jsfiddle.net/sUK45/

(An alternative/simpler solution might involve returning an 'rgb(...)'-style colour code.)

deleted 31 characters in body
Source Link
yckart
  • 33.2k
  • 9
  • 123
  • 130
Loading
Source Link
Joe Freeman
  • 3.8k
  • 2
  • 22
  • 16
Loading