Module: GeoPattern::PatternHelpers
- Defined in:
- lib/geo_pattern/pattern_helpers.rb
Class Method Summary collapse
- .generate_rgb_string(rgb) ⇒ Object
- .hex_val(hash, index, length) ⇒ Object
- .html_to_rgb(color) ⇒ Object
- .html_to_rgb_for_string(seed, base_color) ⇒ Object
-
.map(value, v_min, v_max, d_min, d_max) ⇒ Object
Ruby implementation of Processing's map function http://processing.org/reference/map_.html v for value, d for desired.
-
.offset_color(html_color, hue_offset, sat_offset) ⇒ Object
Rotates the hue and shifts the saturation of an HTML color, returning a Color::RGB.
Class Method Details
.generate_rgb_string(rgb) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/geo_pattern/pattern_helpers.rb', line 54 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 |
# 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) generate_rgb_string(offset_color(base_color, hue_offset, sat_offset)) end |
.map(value, v_min, v_max, d_min, d_max) ⇒ Object
Ruby implementation of Processing's map function http://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 |
.offset_color(html_color, hue_offset, sat_offset) ⇒ Object
Rotates the hue and shifts the saturation of an HTML color, returning a Color::RGB. Color objects are immutable as of color 2.0, so this builds a new color instead of mutating one in place.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/geo_pattern/pattern_helpers.rb', line 34 def offset_color(html_color, hue_offset, sat_offset) hsl = ::Color::RGB.from_html(html_color).to_hsl hue = (hsl.hue - hue_offset) / 360.0 hue += 1.0 if hue < 0.0 hue -= 1.0 if hue > 1.0 saturation = if sat_offset % 2 == 0 hsl.saturation + sat_offset else hsl.saturation - sat_offset end ::Color::HSL.from_fraction( hue.clamp(0.0, 1.0), (saturation / 100.0).clamp(0.0, 1.0), hsl.l ).to_rgb end |