Module: GeoPattern::PatternHelpers

Defined in:
lib/geo_pattern/pattern_helpers.rb

Class Method Summary collapse

Class Method Details

.generate_rgb_string(rgb) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/geo_pattern/pattern_helpers.rb', line 39

def generate_rgb_string(rgb)
  r = (rgb.r * 255).round
  g = (rgb.g * 255).round
  b = (rgb.b * 255).round

  format("rgb(%<r>d, %<g>d, %<b>d)", r: r, g: g, b: b)
end

.hex_val(hash, index, length) ⇒ Object



5
6
7
# File 'lib/geo_pattern/pattern_helpers.rb', line 5

def hex_val(hash, index, length)
  hash[index, length || 1].to_i(16)
end

.html_to_rgb(color) ⇒ Object



20
21
22
# File 'lib/geo_pattern/pattern_helpers.rb', line 20

def html_to_rgb(color)
  generate_rgb_string(::Color::RGB.from_html(color))
end

.html_to_rgb_for_string(seed, base_color) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/geo_pattern/pattern_helpers.rb', line 24

def html_to_rgb_for_string(seed, base_color)
  hue_offset = map(seed.to_i(14, 3), 0, 4095, 0, 359)
  sat_offset = seed.to_i(17, 1)
  base_color = ::Color::RGB.from_html(base_color).to_hsl
  base_color.hue = base_color.hue - hue_offset

  base_color.saturation = if sat_offset % 2 == 0
    base_color.saturation + sat_offset
  else
    base_color.saturation - sat_offset
  end

  generate_rgb_string(base_color.to_rgb)
end

.map(value, v_min, v_max, d_min, d_max) ⇒ Object

Ruby implementation of Processing’s map function processing.org/reference/map_.html v for value, d for desired



12
13
14
15
16
17
18
# File 'lib/geo_pattern/pattern_helpers.rb', line 12

def map(value, v_min, v_max, d_min, d_max)
  v_value = value.to_f # so it returns float

  v_range = v_max - v_min
  d_range = d_max - d_min
  (v_value - v_min) * d_range / v_range + d_min
end