distinguishing between light or dark color of a pixel

General Macro Scheduler discussion

Moderators: Dorian (MJT support), JRL

Post Reply
guyash2
Junior Coder
Posts: 40
Joined: Thu May 05, 2005 2:45 pm

distinguishing between light or dark color of a pixel

Post by guyash2 » Mon Nov 28, 2005 10:17 pm

Hi! 8)
I am quite new here and I have a question: I want to take a color of a specific pixel (with the getpixelcolor command) and then check if it is light color (like pale blue, white, grey etc.) or dark color (shades of brown, black etc.) and to act accordingly to the type of the color- light or dark.
I think that the computer can't distinguish between light or dark colors automaticly so that I guess I will have to decide what is a light color and what is a dark color (the colors that I work with them are pale blue, white-grey which are very light and some shades of a quite a dark brown, so the distinguish should work easily...).
So I do I do that? :roll:

Thanks. :lol:

adroege
Automation Wizard
Posts: 438
Joined: Tue Dec 07, 2004 7:39 pm

determine if pixel is light or dark

Post by adroege » Wed Jun 09, 2010 3:37 pm

This should give you some ideas. Worked OK for me. You might want to tweak the color value I used to suit your purpose.

Code: Select all


Label>Loop
  GetCursorPos>X,Y
  GetPixelColor>X,Y,result
  If>result>13540046
    Message>%result%  Pixel is light colored
  Else
    Message>%result%  Pixel is dark colored
  Endif
  Wait>0.1
  Goto>Loop


gdyvig
Automation Wizard
Posts: 447
Joined: Fri Jun 27, 2008 7:57 pm
Location: Seattle, WA

Post by gdyvig » Wed Jun 09, 2010 9:27 pm

Here is a more flexible approach. Convert the color code into the Red, Green, and Blue components - RGB. Each will have a value ranging from 0-255. If all 3 numbers are 0 the color is black. If all 3 numbers are 255 the color is white.

Here is code that displays the RGB colors whereever you point the cursor. Type E to exit the tool.

Code: Select all

Let>MSG_CENTERED=1
SRT>EXIT_LOOP
    Press Enter
    EXIT>
END>EXIT_LOOP
SRT>MSG_POS
    Let>MSG_CENTERED=0
    Let>MSG_XPOS=%X%
    Let>MSG_YPOS=%Y%
END>MSG_POS


OnEvent>KEY_DOWN,E,0,EXIT_LOOP
OnEvent>KEY_DOWN,M,0,MSG_POS

Let>counter=0
Repeat>counter

   wait>0.3
   GetCursorPos>X,Y
   GetPixelColor>X,Y,nColor
   //Convert MScheduler color to RGB
   Let>red={%nColor% MOD 256}
   Let>green={((%nColor%-%red%) MOD (256*256))/256}
   Let>blue={(((%nColor%-%red%)/256)-%green%)/256}
   Message>%red%-%green%-%blue%%CRLF%(Press E to exit, M to move message)
   //Message>%nColor%
Until>counter>10
Exit>
This code is based on someone elses contribution in the forums.

Gale

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Jun 09, 2010 10:49 pm

I agree with Gale, you need to convert the color to its red, green and blue (RGB) components before you can determine how dark or light it might be. Color numbers are sequential but the colors they represent are not. You can't select a number and expect that everything above or below it will have certain properties. here's my contribution to the discussion.

Set the "breakpoint" variable to a number that will be compared to the sum of the RGB values. if the sum is above the value of "breakpoint", the color is determined to be light. I arbitrarily selected 500 for the value to test against, you can choose a different number.

Code: Select all


Let>breakpoint=500

Dialog>Dialog1
   Caption=Dialog1
   Width=445
   Height=250
   Top=186
   Left=137
   Memo=msMemo1,20,15,150,90,msMemo1
EndDialog>Dialog1

Show>dialog1

Label>Loop
  GetDialogAction>dialog1,res1
  If>res1=2
    Exit>0
  EndIf
  GetCursorPos>curX,curY
  GetPixelColor>curX,curY,color
  GoSub>ConvertColor
  SetDialogObjectColor>Dialog1,,%color%
  Let>test={%red%+%grn%+%blu%}
  If>%test%>%breakpoint%
    Let>test2=LIGHT
  Else
    Let>test2=DARK
  EndIf
  Let>dialog1.msmemo1=Red = %red%%CRLF%Green = %grn%%CRLF%Blue = %blu%%CRLF%Total = %test% of %breakpoint%%CRLF%Color = %color%%CRLF%Luminence = %test2%
  ResetDialogAction>dialog1
Goto>Loop

SRT>ConvertColor
  //Thank you me_again for
  //this code that converts a
  //color number to RGB values
  Let>px2={%color% MOD 16}
  Let>px3={(%color%-%px2%)/16}
  Let>px4={%px3% MOD 16}
  Let>red={%px2%+(%px4%*16)}
  Let>px5={(%px3%-%px4%)/16}
  Let>px6={%px5% MOD 16}
  Let>px7={(%px5%-%px6%)/16}
  Let>px8={%px7% MOD 16}
  Let>grn={%px6%+(%px8%*16)}
  Let>px9={(%px7%-%px8%)/16}
  Let>px10={%px9% MOD 16}
  Let>px11={(%px9%-%px10%)/16}
  Let>px12={%px11% MOD 16}
  Let>blu={%px10%+(%px12%*16)}
////////////////////////////
END>ConvertColor
Last edited by JRL on Thu Jun 10, 2010 3:15 am, edited 1 time in total.

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Wed Jun 09, 2010 10:56 pm

Just noticed this is a 4 1/2 year old post. Maybe someone will find this useful anyway.

Me_again
Automation Wizard
Posts: 1101
Joined: Fri Jan 07, 2005 5:55 pm
Location: Somewhere else on the planet

Post by Me_again » Wed Jun 16, 2010 9:43 pm

Yup, a lot of grave digging on this forum recently, but that's not all bad :D

Anyway, I think one of the best ways (with some scientific basis) to do this light/dark discrimination would be to calculate and set a threshold on Luminance which is:

Y= 0.3 * R + 0.59 * G + 0.11 * B

resulting in Y between 0 and 255.

User avatar
JRL
Automation Wizard
Posts: 3505
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Thu Jun 17, 2010 9:52 pm

OK... so here's the same script as posted above with two lines altered to accommodate the luminance formula. I can see where this will provide different results because the formula will use differing percentages of the red, green and blue makeup of a specified color to create a summed value to judge against for light and dark. "Light" or "dark" is still a judgment call, so the user will still need to adjust the value of the variable "breakpoint" to suit their individual taste. I have set "breakpoint" to 128 in the sample. 128 is an arbitrarily chosen number, roughly half of 255.

Code: Select all

//Remove original breakpoint value based on 0 to 750
//use me_again's breakpoint value based on 0 to 255
//Let>breakpoint=500
Let>breakpoint=128

Dialog>Dialog1
   Caption=Dialog1
   Width=445
   Height=250
   Top=186
   Left=137
   Memo=msMemo1,20,15,150,90,msMemo1
EndDialog>Dialog1

Show>dialog1

Label>Loop
  GetDialogAction>dialog1,res1
  If>res1=2
    Exit>0
  EndIf
  GetCursorPos>curX,curY
  GetPixelColor>curX,curY,color
  GoSub>ConvertColor
  SetDialogObjectColor>Dialog1,,%color%
  //Remove the original test and put in me_again's Luminousity formula
  //Let>test={%red%+%grn%+%blu%}
  //Y= 0.3 * R + 0.59 * G + 0.11 * B
  Let>test={(0.3*%red%)+(0.59*%grn%)+(0.11*%blu%)}
  If>%test%>%breakpoint%
    Let>test2=LIGHT
  Else
    Let>test2=DARK
  EndIf
  Let>dialog1.msmemo1=Red = %red%%CRLF%Green = %grn%%CRLF%Blue = %blu%%CRLF%Total = %test% of %breakpoint%%CRLF%Color = %color%%CRLF%Luminence = %test2%
  ResetDialogAction>dialog1
Goto>Loop

SRT>ConvertColor
  //Thank you me_again for
  //this code that converts a
  //color number to RGB values
  Let>px2={%color% MOD 16}
  Let>px3={(%color%-%px2%)/16}
  Let>px4={%px3% MOD 16}
  Let>red={%px2%+(%px4%*16)}
  Let>px5={(%px3%-%px4%)/16}
  Let>px6={%px5% MOD 16}
  Let>px7={(%px5%-%px6%)/16}
  Let>px8={%px7% MOD 16}
  Let>grn={%px6%+(%px8%*16)}
  Let>px9={(%px7%-%px8%)/16}
  Let>px10={%px9% MOD 16}
  Let>px11={(%px9%-%px10%)/16}
  Let>px12={%px11% MOD 16}
  Let>blu={%px10%+(%px12%*16)}
////////////////////////////
END>ConvertColor

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts