Class: Circule

Inherits:
Object
  • Object
show all
Defined in:
lib/circule.rb,
lib/circule/version.rb

Overview

Convert a hash to an icon.

Defined Under Namespace

Classes: Error, GUI

Constant Summary collapse

VERSION =
'0.1.4'

Instance Method Summary collapse

Constructor Details

#initialize(hex: nil, btc_block: nil, bit0: 0, step: 16, bitn: 255, canvas: 24, ox: 0, oy: ox) ⇒ Circule

Returns a new instance of Circule.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/circule.rb', line 14

def initialize(
  hex: nil, btc_block: nil,
  bit0: 0, step: 16, bitn: 255,
  canvas: 24, ox: 0, oy: ox
) @_int      = hex.to_i(16) if hex
  @btc_block = btc_block
  @bit0      = bit0
  @step      = step
  @bitn      = bitn
  @canvas    = canvas
  @ox        = ox
  @oy        = oy
end

Instance Method Details

#circlesObject



49
50
51
52
53
# File 'lib/circule.rb', line 49

def circles
  @_circles ||=
    slices
    .map { |s| [ s[0, 5], s[5, 5], s[10, 6] ] }
end

#imageObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/circule.rb', line 55

def image
  @_image ||=
    ChunkyPNG::Image
    .new(@canvas, @canvas)
    .tap do |img|
      @canvas.times do |a|
        @canvas.times do |b|
          img[a, b] = ChunkyPNG::Color.html_color(
            ChunkyPNG::Color::PREDEFINED_COLORS.keys[
              circles
              .select { |x, y, r| (a+@ox-x)**2 + (b+@oy-y)**2 < r**2 }
              .size
            ]
          )
        end
      end
    end
end

#intObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/circule.rb', line 28

def int
  @_int ||=
    if @btc_block
      Net::HTTP
      .get_response(
        URI("https://live.blockcypher.com/btc/block/#{@btc_block}/"))
      .header['location']
      .split('/')[-1]
    else
      SecureRandom.hex(32)
    end
    .to_i(16)
end

#slicesObject



42
43
44
45
46
47
# File 'lib/circule.rb', line 42

def slices
  @_slices ||=
    @bit0
    .step(@bitn, @step)
    .map { |n| int[n, 16] }
end