Skip to content

Instantly share code, notes, and snippets.

@viktorbenei
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viktorbenei/8099429429ca461e8c03 to your computer and use it in GitHub Desktop.
Save viktorbenei/8099429429ca461e8c03 to your computer and use it in GitHub Desktop.
Hexa color code generation from any text string
function generate_color_from_text(text) {
var redRandom = 0;
var greenRandom = 0;
var blueRandom = 0;
for (var i = 0; i < text.length; i++) {
var charCode = text.charCodeAt(i);
redRandom += charCode;
greenRandom += charCode+1;
blueRandom += charCode+4;
}
redRandom = 40 + (redRandom % 5) * 40;
greenRandom = 40 + (greenRandom % 5) * 40;
blueRandom = 40 + (blueRandom % 5) * 40;
return "#" + componentToHex(redRandom) + componentToHex(greenRandom) + componentToHex(blueRandom);
}
function componentToHex(c) {
var hex = c.toString(16);
return hex.length == 1 ? "0" + hex : hex;
}
require 'digest'
def generate_color_from_text(text)
hexd = Digest::MD5.new.update(text.to_s).hexdigest
hexa_color_str = '#' + hexd[0,2] + hexd[2,2] + hexd[4,2];
return hexa_color_str
end
def generate_color_from_text(text)
red, green, blue = 0, 0, 0
text.each_char do |char|
charCode = char.ord
red += charCode
green += charCode+1
blue += charCode+4
end
red = 40 + (red%45)*4
green = 40 + (green%45)*4
blue = 40 + (blue%45)*4
hexa_color_str = "#%02x%02x%02x" % [red, green, blue]
return hexa_color_str
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment