Module: GeoPattern::PatternHelpers

Defined in:
lib/geo_pattern/pattern_helpers.rb

Class Method Summary collapse

Class Method Details

.generate_rgb_string(rgb) ⇒ Object


36
37
38
39
40
41
42
# File 'lib/geo_pattern/pattern_helpers.rb', line 36

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

  format('rgb(%d, %d, %d)', r, g, b)
end

.hex_val(hash, index, length) ⇒ Object


3
4
5
# File 'lib/geo_pattern/pattern_helpers.rb', line 3

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

.html_to_rgb(color) ⇒ Object


17
18
19
# File 'lib/geo_pattern/pattern_helpers.rb', line 17

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

.html_to_rgb_for_string(seed, base_color) ⇒ Object


21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/geo_pattern/pattern_helpers.rb', line 21

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


9
10
11
12
13
14
15
# File 'lib/geo_pattern/pattern_helpers.rb', line 9

def map(value, v_min, v_max, d_min, d_max) # v for value, d for desired
  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