-
-
Save adam12/e50252be3be2b70a509d5d62280e3ca4 to your computer and use it in GitHub Desktop.
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
# frozen_string_literal: true | |
module Hue | |
# Calculate a hue +value+ between 1 and 360 from a +String+ value. | |
# | |
# = Usage | |
# | |
# hue = Hue.from_string("Adam Daniels") | |
# css = "background-color: hsl(#{hue}, 100%, 30%);" | |
def self.from_string(value) | |
djb2(value) % 360 | |
end | |
def self.djb2(value) | |
hash = 5381 | |
value.each_byte do |b| | |
hash = (((hash << 5) + hash) + b) % (2 ** 32) | |
end | |
hash | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment