Class: ColorRoi

Inherits:
Roi show all
Includes:
CanHaveSpecialMethods
Defined in:
lib/roi/color/color_roi.rb

Overview

The ColorRoi class defines a color Roi and its methods

Instance Attribute Summary collapse

Attributes inherited from Roi

#height, #name, #ref_img, #width, #x, #y

Instance Method Summary collapse

Methods included from CanHaveSpecialMethods

included

Methods inherited from Roi

#find_frame_displayed, #find_frame_not_displayed, #precise_time_to_change, #precise_time_to_disappear, #precise_time_to_display, #precise_time_to_match_dut, #precise_time_to_transition, #press_key_until_displayed?, #rectangle, #rectangle=, #time_to_change, #time_to_disappear, #time_to_display, #time_to_transition, #wait_for_change?, #wait_for_transition?, #wait_until_displayed?, #wait_until_not_displayed?

Constructor Details

#initialize(dut, args = {}) ⇒ ColorRoi

Public: Initializes a color Roi.

dut - Platform (or subclass) instance to which this Roi belongs. x - Integer x coordinate (default: nil). y - Integer y coordinate (default: nil). width - Integer width (default: nil). height - Integer height (default: nil). rectangle - Hash defining rectangle with keys :x, :y, :width, :height (default: nil). element - Hash same as rectangle (default: nil). red - Integer red value 0 - 255 (default: nil). Required unless rgb is given. green - Integer green value 0 - 255 (default: nil). Required unless rgb is given. blue - Integer blue value 0 - 255 (default: nil). Required unless rgb is given. rgb - Integer Array of 3 values (0 - 255) for red, green, blue (default: nil).

Required unless red, green, blue are given.

similarity - Integer color similarity threshold 0 - 100 (default: 85). ref_img - String path to reference image (default: nil).

Returns nothing.



29
30
31
32
33
34
35
36
37
# File 'lib/roi/color/color_roi.rb', line 29

def initialize(dut, args={})
  super(dut, args)
  if args[:rgb].nil?
    self.rgb=([args.fetch(:red), args.fetch(:green), args.fetch(:blue)])
  else
    self.rgb=(args[:rgb])
  end
  @similarity = args.fetch(:similarity, 85)
end

Instance Attribute Details

#blueObject

Returns the value of attribute blue.



9
10
11
# File 'lib/roi/color/color_roi.rb', line 9

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



9
10
11
# File 'lib/roi/color/color_roi.rb', line 9

def green
  @green
end

#redObject

Returns the value of attribute red.



9
10
11
# File 'lib/roi/color/color_roi.rb', line 9

def red
  @red
end

#similarityObject

Returns the value of attribute similarity.



9
10
11
# File 'lib/roi/color/color_roi.rb', line 9

def similarity
  @similarity
end

Instance Method Details

#displayed?(args = {}) ⇒ Boolean

Public: Checks if color is displayed as defined in the given ROI.

Corresponds to: api_is_color_displayed?

key - String name of key to press to trigger evaluation (default: nil).

If default, no keys will be pressed.

delay - Integer total milliseconds to delay before starting evaluation (default: 0). timeout - Integer total milliseconds to allow before timing out (default: 0). verifies_for - Integer total milliseconds for which the ROI must verify in order to succeed (default: 0).

If default, the ROI must only verify once.

priority - Symbol representing evaluation priority used to throttle CPU usage (default: :normal):

:critical   - Sleep 10 milliseconds between intensive tasks (should be used sparingly).
:high       - Sleep 100 milliseconds between intensive tasks.
:normal     - Sleep 1 second between intensive tasks (default).
:low        - Sleep 10 seconds between intensive tasks.
:background - Sleep 1 minute between intensive tasks (should be used sparingly).

log_every - Integer interval in milliseconds between logs (default: 1000).

Returns a Boolean true if color is displayed before the timeout, otherwise false.

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/roi/color/color_roi.rb', line 82

def displayed?(args={})
  logger.info('Checking if color is displayed')
  raise "Bad RGB: #{rgb}" unless rgb.all?
  frame = args[:frame]
  values = []
  verify(args, values) do |sims|
    if frame.nil?
      json = get_json_for_compare
      sim = test_case.send(:tmc_post, "/api/roi/color/actions/#{dut.id}", json: json)['value']
    else
      json = get_json_for_compare(frame: frame)
      sim = test_case.send(:tmc_post, '/api/roi/color/actions', json: json)['value']
    end
    sim *= 100
    sims << sim
    result = sim >= @similarity
    msg = "Color is#{result ? '' : ' not'} displayed #{frame.nil? ? 'on screen' : 'in frame'} " +
        "(similarity: #{sim.round}%)"
    logger.roi(self, args.merge(message: msg, use_last_image: frame.nil?, screenshot: frame.nil?))
    result
  end
end

#infoObject

Public: Gets color Roi info.

Returns a tidy String with color Roi info.



60
61
62
# File 'lib/roi/color/color_roi.rb', line 60

def info
  "#{super} RGB=#{@red}:#{@green}:#{@blue} Similarity=#{@similarity}"
end

#retrieve(args = {}) ⇒ Object

Public: Gets the color of the region defined in the Roi.

Corresponds to: api_read_color_from_screen

Returns the color as a Hash with keys :red, :green, :blue, each being an Integer value (0 - 255).



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/roi/color/color_roi.rb', line 110

def retrieve(args={})
  logger.info('Retrieving color')
  frame = args[:frame]
  value(args) do
    if frame.nil?
      # Get the color from the screen
      json = get_json_for_get
      color = test_case.send(:tmc_post, "/api/roi/color/actions/#{dut.slot}", json: json)['value']
    else
      # Get the color from the frame
      json = get_json_for_get(frame: frame)
      color = test_case.send(:tmc_post, '/api/roi/color/actions', json: json)['value']
    end
    color = {red: color['red'], green: color['green'], blue: color['blue']}
    logger.roi(self, args.merge(message: "Retrieved color #{color.inspect}", use_last_image: frame.nil?,
                                screenshot: frame.nil?))
    color
  end
end

#rgbObject

Public: Gets RGB value.

Returns Integer Array of 3 values (0 - 255) for red, green, blue.



42
43
44
# File 'lib/roi/color/color_roi.rb', line 42

def rgb
  [@red, @green, @blue]
end

#rgb=(rgb) ⇒ Object

Public: Sets RGB value.

rgb - Integer Array of 3 values (0 - 255) for red, green, blue.

Returns nothing.



51
52
53
54
55
# File 'lib/roi/color/color_roi.rb', line 51

def rgb=(rgb)
  @red = rgb[0]
  @green = rgb[1]
  @blue = rgb[2]
end