Jump to content
Sign in to follow this  

How to get RBG Pixel Color

Recommended Posts


I will add this to the unofficial documentation.

; User-defined function to get RGB pixel color
; How it works:
;  Get decimal color via AutoIt's PixelGetColor function
;  Convert result to a six-digit hexadecmial number
;  Take the digits in groups of two and convert back to decmial
;    to obtain the B, G, and R (respectively) color values
; Anyone want to post a more direct RGB function?

$c =   PixelGetRGB(0,0);upper left pixel 
MsgBox(0,"The RBG Pixel Color is", $c[1] & "," & $c[2] & "," & $c[3])

; Returns an array with element1=RED, element2=GREEN, element3=BLUE
Func PixelGetRGB($x, $y)
   $hex = Hex(PixelGetColor($x, $y), 6)
   $r = hexToDec(StringRight($hex, 2)) & "|"
   $g = hexToDec(StringMid($hex, 3,2)) & "|"
   $b = hexToDec(StringLeft($hex, 2))
   Return StringSplit($r & $g & $b, "|")

; Returns the decimal equivalent of a $hex string like "05ff"
Func hexToDec($hex)
   $dec = 0;running total
   $n = StringLen($hex);number of digits

   For $i = 1 to $n
      $d = StringMid($hex, $i, 1);digit
         Case $d >= "0" And $d <= "9"
            $dec = $dec + (Asc($d)-48) * power(16,$n-$i) 
         Case $d >= "A" And $d <= "G"
            $dec = $dec + (Asc($d)-55) * power(16,$n-$i) 
         Case $d >= "a" And $d <= "g"
            $dec = $dec + (Asc($d)-87) * power(16,$n-$i) 
   Return $dec

; Returns $base raised to the power of $exponent
Func power($base, $exponent)
   $tmp = 1
   For $i = 1 to $exponent
      $tmp = $tmp * $base
   Return $tmp

Converting RGB to decimal (for use with PixelSearch) is a bit easier

Func rgbToDec($r, $g, $b)
   return HexToDec(Hex($b,2) & Hex($g,2) & Hex($r,2))
Edited by CyberSlug

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.