Last active
August 29, 2015 14:04
-
-
Save viktorbenei/8099429429ca461e8c03 to your computer and use it in GitHub Desktop.
Hexa color code generation from any text string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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