Module: Util

Defined in:
lib/boring_avatar/util.rb

Class Method Summary collapse

Class Method Details

.get_boolean(number, ntn) ⇒ Object



30
31
32
# File 'lib/boring_avatar/util.rb', line 30

def get_boolean(number, ntn)
  !(get_digit(number, ntn) % 2 > 0)
end

.get_contrast(hex) ⇒ Object



34
35
36
37
38
# File 'lib/boring_avatar/util.rb', line 34

def get_contrast(hex)
  red, green, blue = hex.delete_prefix("#").scan(/../).map { |c| c.to_i(16) }
  yiq = ((red * 299) + (green * 587) + (blue * 114)) / 1000
  yiq > 128 ? "black" : "white"
end

.get_digit(number, ntn) ⇒ Object



26
27
28
# File 'lib/boring_avatar/util.rb', line 26

def get_digit(number, ntn)
  ((number / (10 ** ntn)) % 10).to_i
end

.get_number(string) ⇒ Object



7
8
9
10
11
12
# File 'lib/boring_avatar/util.rb', line 7

def get_number(string)
  string.chars.reduce(0) do |sum, char|
    sum = (sum << 5) - sum + char.ord
    [sum].pack("L").unpack("l").first
  end.abs
end

.get_random_color(colors, number, range) ⇒ Object



14
15
16
# File 'lib/boring_avatar/util.rb', line 14

def get_random_color(colors, number, range)
  colors[number % range] || colors.last
end

.get_unit(number, range, index = nil) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/boring_avatar/util.rb', line 18

def get_unit(number, range, index = nil)
  value = number % range
  if index && (get_digit(number, index) % 2) == 0
    return -value
  end
  value
end

.random_palette ⇒ Object



40
41
42
# File 'lib/boring_avatar/util.rb', line 40

def random_palette
  color_palettes[(0..99).to_a.sample]
end