Skip to main content

Content Starts Here

Get a control's background color

Article

The script below demonstrates how to get the background color of the specified control. Many controls provide this information via their properties available to TestComplete. However, some controls don't have such properties. The script below detects the predominant color in the controls image and returns this color. In most cases, this is the background color of the control.

Use this script if there is no other way to get this information from the control.

VBScript

Sub Main
  Dim wControl, vBKColor
  Set wControl = ' ... specify the path to the control
  vBKColor = getBKColor(wControl.Picture())
  Set attrs = Log.CreateNewAttributes()
  attrs.BackColor = vBKColor 
  Log.Message "Get the control's backcolor", "", 0, attrs  
End Sub

Function getBKColor(picObj)
  Dim colors, maxColor, maxColorCount, widthId, heighId
  Dim currentPix, currentValue
  Set colors = Sys.OleObject("Scripting.Dictionary")
  maxColor = 0
  maxColorCount = 0
  
  For widthId = 0 To picObj.Size.Width - 1 Step 3 
    For heighId = 0 To picObj.Size.Height - 1 Step 3
      currentPix = picObj.Pixels(widthId, heighId)
      If Not colors.Exists(currentPix) Then
        Call colors.Add(currentPix, 0)
      End If
      currentValue = colors.Item(currentPix)
      colors.Remove(currentPix)
      Call colors.Add(currentPix, currentValue + 1)
      If colors.Item(currentPix) > maxColorCount Then
        maxColorCount = colors.Item(currentPix)
        maxColor = currentPix
      End If
    Next
  Next

  getBKColor = maxColor
End Function

JScript

function Main()
{
  var wControl = // ... specify the path to the control
  var vBKColor = getBKColor(wControl.Picture());
  var attrs = Log.CreateNewAttributes();
  attrs.BackColor = vBKColor; 
  Log.Message("Get the control's backcolor", "", 0, attrs);  
}

function getBKColor(picObj)
{
  var colors = new Array();
  var maxColor = 0;
  var maxColorCount = 0;
  
  for (var widthId = 0; widthId < picObj.Size.Width; widthId += 3) {
    for (var heighId = 0; heighId < picObj.Size.Height; heighId += 3) {
      var currentPix = picObj.Pixels(widthId, heighId)
      if (null == colors[currentPix]) {
        colors[currentPix] = 0;
      }
      colors[currentPix]++;
      if (colors[currentPix] > maxColorCount) {
        maxColorCount = colors[currentPix];
        maxColor = currentPix;
      }
    }
  }

  return maxColor;
}
Previous MonthNext Month
SunMonTueWedThuFriSat