Forum Discussion

omsakthir's avatar
omsakthir
New Contributor
14 years ago

How to convert an rgb value into argb or vice versa in vbscript?

I have got argb as -65536 and rgb as 255,0,0

I want to compare and check whether these two are same or not?

Please tell me the formula to convert argb into rgb or rgb into argb.

It would be of great help if you could send me in vb.

6 Replies

  • Sakthi,


    As far as I know, in a typical case, an RGBa color is coded as a 4-byte value: (a, r, g, b). See http://en.wikipedia.org/wiki/RGBA_color_space. Depending on the data type, this 4-byte can be treated as an integer value, or a hexadecimal value. For instance, the color a=255, r=255, g=0, b=0 can be displayed as a signed integer (-65536, the highest bit is used to code the sign, that is why, you get a negative number) or like a hexadecimal number FFFF0000.


    I guess, you get colors as numbers. You can either compare the colors directly, or convert them into strings and then compare the strings. To get a string of hexadecimal symbols that correspond to a color, you can use VBScript's Hex function. The code snippet below demonstrates how you can use it in your tests.


    If you want to exclude the a channel from comparison, you can simply use only 6 right characters, that is,

      s = RGBaToStr(...) 

      s = Right(s, 6)


    If you decide to compare colors as ordinary numbers and want to exclude the a channel, you can use this code --

      colorWithoutA = colorWithA and &H00FFFFFF


    Here is a function that converts a color to a string of hex digits:




    Sub Test

      ' Use

      s1 = RGBaToStr(-65536)

      s2 = RGBaToStr(16711680)  ' r = 255, g = 0, b = 0

     

      Log.Message "Result: " + s1

      Log.Message "Result: " + s2

    End Sub


    Function RGBaToStr(AValue)

      ' Convert

      s = Hex(AValue)

     

      ' Add leading spaces, if needed

      L = Len(s)

      If L < 8 Then

        For i = 1 to 8 - L

          s = "0" + s

        Next

      End If

     

      ' Return the result

      RGBaToStr = s

    End Function

  • omsakthir's avatar
    omsakthir
    New Contributor
    Alex, thank you for the answer.



    I found another way too.



    Converting argb value into rgb value:



    Tools->Current Project Properties-->CLR Bridge

    Browse GAC add System.Drawing

    Then we will be able to use color class.


    Example:

    set ColourObject = dotNet.System_Drawing.Color.FromArgb("-16776961")

    Log.Message(ColourObject .R)  '0

    Log.Message(ColourObject .G) '0

    Log.Message(ColourObject .B) '255


     ColourObject = .System_Drawing.Color.FromArgb("-16776961").Message(ColourObject .R)  '0.Message(ColourObject .G) '0.Message(ColourObject .B) '255
  • Oh, it's a nice way! I didn't think of using .NET classes for this :-).
  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 1 rankChampion Level 1
    Hi Alex,



    Can you comment as for the performance penalties when using .Net classes from within scripts in TC?

    It is my understanding that a penalties should include, at least, .Net initialization and inter-process communication between script runtime and .Net runtime. Maybe some other penalties that I missed.

    I think that inter-process communication (including .Net objects marshaling, conversion and compilation) occurs for every script line token that uses .Net class, so this is an area where tester cannot do much to improve the performance, isn't it? Hopefully, the performance degradation can be ignored, but maybe you have some figures?

    Can you comment .Net initialization part? Does it occur when the test execution is started or at some other moment of time? Does it occur once for the whole test project or every time when the execution of test item is started during the test run?

    My general feeling is that (generally) action implemented via script is more efficient when compared to the implementation via .Net classes. Any comments on this?



    Thank you.
  • Alexei,


    Can you comment .Net initialization part? Does it occur when the test execution is started or at some other moment of time? Does it occur once for the whole test project or every time when the execution of test item is started during the test run?


    Initialization occurs for the project at the moment when TestComplete "accesses" the CLR Bridge for the first time. After this, .NET is not re-initialized during the test item execution. The inter-process penalties (marshalling, conversion) occur on each script line. Else, it would be impossible to call methods and properties.


    You are right that the "scripting" approach works faster than the ".NET-bridge" approach. I didn't have a chance to conduct thorough experiments, I just ran quick rough tests (getting two color values, comparing and outputting results). In these tests, the "scripting" solution worked about 2-5 times faster than the approach that involves using .NET classes. However, in my opinion, in absolute values the difference was not very large (e.g. ~17 s vs. ~4 s for 1,000 calls).


    The .NET Color object contains special methods which QA engineers may want to use further in their tests. So, I think the ".NET-bridge" approach is also acceptable in certain situations.

  • AlexKaras's avatar
    AlexKaras
    Icon for Champion Level 1 rankChampion Level 1
    Hi Alex,



    Thanks you for your comments and explanations. They coincide with my understanding.

    Obviously, I did not mean that .Net Bridge should not be used, quite the contrary, .Net methods can be quite useful as they implement a lot of complex functionality that is hard to implement (if it all possible) via regular scripting. But I think that the one should always remember about performance penalties and consider possible ways of the implementation.

    Thank you again, much appreciated.