locked
Averaging colours RRS feed

  • Question

  • Hi, i am trying to create a program that finds the average colour in a area around the mouse but the GraphicsWindow.GetPixel(x,y) returns letters and numbers , is there any whay to create a scale or convert them into numbers.

    Thank you for reading, Sam

    Friday, April 3, 2015 8:50 PM

Answers

  • Hello Sam27_G,

    My program CKK055 has a subroutine Color_RGBToValues which converts Hexadecimal color to decimal numbers.


    Nonki Takahashi



    • Edited by Nonki Takahashi Monday, April 6, 2015 1:14 AM added hello
    • Proposed as answer by Nonki Takahashi Tuesday, April 7, 2015 4:05 PM
    • Marked as answer by litdev Friday, April 17, 2015 7:09 PM
    Monday, April 6, 2015 1:11 AM
  • Here is a quick mod from another program, to show values (in title bar) and color itself (in the GW background) for the average color of an (here 21x21 Pxl) area/region from an image in the GW. Just drag&drop an image onto the GW and move the mouse. (ID: PNK567, needs LitDev extension)

    After testing, i see that this solution is'nt the best to show the average color in the GW background (the image should have smaller dimensions than the GW itself: imageW < gw and imageH < gh, to see the color) and should be modded back again to show the average color in a separate (dragable) RectangleShape like below. But it shows how to get the average color via LDImage.Histogram or LDImage.Statistics with LitDev extension.

    • Marked as answer by litdev Friday, April 17, 2015 7:09 PM
    Thursday, April 9, 2015 9:07 PM
    Answerer

All replies

  • Hi Sam27_G

    The GraphicsWindow.GetPixel(x,y) method returns numbers. Not decimal numbers, but hexadecimal numbers as

    (#)RRGGBB  where eg.  RR (red component) ranges from #00 to #FF  (in decimal 00 to 255), like...

    dec    hex

    1       1

    2       2

    ....

    10    A

    11    B

    12    C

    ...

    16    10

    ...

    255   FF

    Same for GG (green component) and BB (blue component).

    So pure black would be #000000 (0,0,0 in R,G,B or #RRGGBB if you want) till up to

    pure white, which would be #FFFFFF (255,255,255 in R,G,B)

    Dec-Hex Table

    Average color of a pixel then would be (RR + GG + BB)/3  or (R+G+B)/3.

    To access the single components you can use

    hex = GraphicsWindow.GetPixel(x,y)

    red = Text.GetSubText(hex, 2,2)  ' as 00 - FF

    green = Text.GetSubText(hex, 4,2)  ' as 00 - FF

    blue = Text.GetSubText(hex, 6,2)  ' as 00 - FF

    • Edited by Pappa LapubEditor Friday, April 3, 2015 9:55 PM
    • Proposed as answer by Nonki Takahashi Friday, April 3, 2015 10:54 PM
    • Marked as answer by Sam27_G Saturday, April 4, 2015 7:39 AM
    • Unmarked as answer by Sam27_G Saturday, April 4, 2015 6:07 PM
    • Unproposed as answer by Sam27_G Saturday, April 4, 2015 6:07 PM
    Friday, April 3, 2015 9:33 PM
    Answerer
  • Hello Sam27_G,

    My program CKK055 has a subroutine Color_RGBToValues which converts Hexadecimal color to decimal numbers.


    Nonki Takahashi



    • Edited by Nonki Takahashi Monday, April 6, 2015 1:14 AM added hello
    • Proposed as answer by Nonki Takahashi Tuesday, April 7, 2015 4:05 PM
    • Marked as answer by litdev Friday, April 17, 2015 7:09 PM
    Monday, April 6, 2015 1:11 AM
  • Here is a quick mod from another program, to show values (in title bar) and color itself (in the GW background) for the average color of an (here 21x21 Pxl) area/region from an image in the GW. Just drag&drop an image onto the GW and move the mouse. (ID: PNK567, needs LitDev extension)

    After testing, i see that this solution is'nt the best to show the average color in the GW background (the image should have smaller dimensions than the GW itself: imageW < gw and imageH < gh, to see the color) and should be modded back again to show the average color in a separate (dragable) RectangleShape like below. But it shows how to get the average color via LDImage.Histogram or LDImage.Statistics with LitDev extension.

    • Marked as answer by litdev Friday, April 17, 2015 7:09 PM
    Thursday, April 9, 2015 9:07 PM
    Answerer