Class: Percept::Field

Inherits:
Object
  • Object
show all
Includes:
ColorUtils
Defined in:
lib/percept/field.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ColorUtils

#color!

Constructor Details

#initialize(params) ⇒ Field

Returns a new instance of Field.



9
10
11
12
13
14
15
16
# File 'lib/percept/field.rb', line 9

def initialize(params)
  self.start_x = params.fetch(:start_x)
  self.end_x = params.fetch(:end_x)
  self.start_y = params.fetch(:start_y)
  self.end_y = params.fetch(:end_y)
  self.line = params.fetch(:line)
  self.image = params.fetch(:image)
end

Instance Attribute Details

#end_xObject

Returns the value of attribute end_x.



7
8
9
# File 'lib/percept/field.rb', line 7

def end_x
  @end_x
end

#end_yObject

Returns the value of attribute end_y.



7
8
9
# File 'lib/percept/field.rb', line 7

def end_y
  @end_y
end

#imageObject

Returns the value of attribute image.



7
8
9
# File 'lib/percept/field.rb', line 7

def image
  @image
end

#lineObject

Returns the value of attribute line.



7
8
9
# File 'lib/percept/field.rb', line 7

def line
  @line
end

#start_xObject

Returns the value of attribute start_x.



7
8
9
# File 'lib/percept/field.rb', line 7

def start_x
  @start_x
end

#start_yObject

Returns the value of attribute start_y.



7
8
9
# File 'lib/percept/field.rb', line 7

def start_y
  @start_y
end

Instance Method Details

#blackish_pixelsObject



34
35
36
# File 'lib/percept/field.rb', line 34

def blackish_pixels
  enclosed_pixels.select(&:blackish?)
end

#contains_text?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/percept/field.rb', line 38

def contains_text?
  blackish_pixels.size > 100
end

#enclosed_pixelsObject



30
31
32
# File 'lib/percept/field.rb', line 30

def enclosed_pixels
  image.get_pixels(start_x, start_y, width, height)
end

#heightObject



22
23
24
# File 'lib/percept/field.rb', line 22

def height
  end_y - start_y
end

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/percept/field.rb', line 42

def overlaps?(other)
  !(start_x > other.end_x || end_x < other.start_x ||
    start_y > other.end_y || end_y < other.start_y)
end

#pixelsObject



18
19
20
# File 'lib/percept/field.rb', line 18

def pixels
  @pixels ||= top_pixels + bottom_pixels + left_pixels + right_pixels
end

#store_pixels!Object



47
48
49
50
51
52
# File 'lib/percept/field.rb', line 47

def store_pixels!
  image.store_pixels(start_x, start_y, width, 1, top_pixels)
  image.store_pixels(start_x, end_y - 1, width, 1, bottom_pixels)
  image.store_pixels(start_x, start_y + 1, 1, height - 2, left_pixels)
  image.store_pixels(end_x - 1, start_y + 1, 1, height - 2, right_pixels)
end

#widthObject



26
27
28
# File 'lib/percept/field.rb', line 26

def width
  end_x - start_x
end