Forum Discussion

simonaferrara's avatar
simonaferrara
Frequent Contributor
3 years ago
Solved

Retrieve RGB color values from Pixels property

Hi all,

 

how can I retrieve the RGB values (red-green-blue) from the returned integer value of picture Pixels property (https://support.smartbear.com/testcomplete/docs/reference/program-objects/picture/pixels.html)

?

 

Thanks

Simona

 

 

  • AlexKaras's avatar
    AlexKaras
    3 years ago

    Hi,

     

    If RGB = r + (g * 256) + (b * 65536), then for

    colorElement == 9118312 (untested code snippet):

     

    var b = Math.floor(colorElement / 65536); // == 139

    var g = Math.floor((colorElement - b * 65536) / 256); // == 34

    var r = colorElement - b * 65536 - g * 256; // == 104

    // 104 + (34 * 256) + (139 * 65536) == 9118312

    // RGB(104, 34, 139) == colorElement

     

7 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    simonaferrara 

     

    1) I don't 'see a particular example for retrieving the color, but my first thought would be to try $get in the example that says $set

    2) Just a hint for the future, URLs are great to include in a post but don't put ( ) around them or that becomes part of the link and we can't click it.  🙂

     

    sonya_m  I couldn't find a "retrieve color" example in here even though the header of the article mentions it.  Could you point it out or get us one?  Thanks.

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/picture/pixels.html

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi Simona,

     

    If I got your question right...

    Below VBScript function creates color number based on RGB.

    You can revert it to get RGB values out of color number.

     

    Function RGB(ByVal r, ByVal g, ByVal b)
      RGB = r + (g * 256) + (b * 65536)
    End Function

     

  • simonaferrara's avatar
    simonaferrara
    Frequent Contributor

    The question is that I don't know how to retrieve the RGB values from the integer values that is returned by Pixels property.

    The javascript code that I've implemented is like this:

     

    let colorElement = canvas.Picture(697, 23, 249, 23, false);
    let x = colorElement.Size.Width - 10;
    let y = colorElement.Size.Height - 10;
    let color = colorElement.Pixels(x, y);

     

    When executing this code, the color variable contains the following value:

    9118312

     

    How can I have the RGB values (r,g,b) from this integer number?

     

    AlexKaras your example is great to have the vice-versa (given the r,g,b, have the integer number) but I don't know how can I do the reverse (given the integer number, have the r,g,b).

     

    Marsha_R thanks for your hint for urls in post and to ask for a documentation for my question.

     

    Regards,

    Simona 

     

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Hi,

       

      If RGB = r + (g * 256) + (b * 65536), then for

      colorElement == 9118312 (untested code snippet):

       

      var b = Math.floor(colorElement / 65536); // == 139

      var g = Math.floor((colorElement - b * 65536) / 256); // == 34

      var r = colorElement - b * 65536 - g * 256; // == 104

      // 104 + (34 * 256) + (139 * 65536) == 9118312

      // RGB(104, 34, 139) == colorElement