Skip to content

Instantly share code, notes, and snippets.

@adam12

adam12/hue.rb Secret

Created March 5, 2020 17:55
Show Gist options
  • Save adam12/e50252be3be2b70a509d5d62280e3ca4 to your computer and use it in GitHub Desktop.
Save adam12/e50252be3be2b70a509d5d62280e3ca4 to your computer and use it in GitHub Desktop.
# 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