Inequality If-Else expressions for colors

Get help with using AutoHotkey and its commands and hotkeys
WeThotUWasAToad
Posts: 295
Joined: 19 Nov 2013, 08:44

Inequality If-Else expressions for colors

01 Apr 2017, 11:48

Hello,

How do you write an inequality expression for a color?

For example, suppose you want to write an If-Else script in which the criteria is that each of the three color parameters — blue, green & red (BGR) — for a given pixel is greater than or equal to a hex value of C8. How should the script shown below be completed in order to accomplish that?

Thanks

Code: Select all

xpos:=400
ypos:=600
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
PixelGetColor, color, xpos, ypos
If (color > ???
...
Return
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
tomoe_uehara
Posts: 210
Joined: 05 Oct 2013, 12:37
Contact:

Re: Inequality If-Else expressions for colors

01 Apr 2017, 11:54

Hello, you can try it like this:

Code: Select all

F1::
MouseGetPos, xx, yy
PixelGetColor, color, xx, yy, RGB
StringMid, R, color, 3,2
StringMid, G, color, 5,2
StringMid, B, color, 7,2
SetFormat, IntegerFast, Hex
msgbox %color%`nR: %r%`nG: %g%`nB: %b%
exitapp
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Inequality If-Else expressions for colors

01 Apr 2017, 18:14

Code: Select all

q:: ;check pixel colour against range
xpos:=400
ypos:=600
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
MouseGetPos, xpos, ypos
PixelGetColor, color, xpos, ypos
if !(((color >> 16) & 0xFF) >= 0xC8)
|| !(((color >> 8) & 0xFF) >= 0xC8)
|| !((color & 0xFF) >= 0xC8)
	MsgBox, % "not match: " color
else
	MsgBox, % "match: " color
return
If anyone can better this in any way, please go ahead.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4440
Joined: 17 Jul 2016, 01:02
Contact:

Re: Inequality If-Else expressions for colors

02 Apr 2017, 03:33

Use a function, eg,

Code: Select all

ColorGreaterThan(col,tol){
	; Returns true if all color components of color col is greater than tol.
	return ((0xff0000 & col) >> 16)>tol && ((0x00ff00 & col) >> 8)>tol && (0x0000ff & col)>tol
}
Explicit usage,

Code: Select all

xpos:=400
ypos:=600
CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
PixelGetColor, color, xpos, ypos
If ColorGreaterThan(color,0xc8)
	MsgBox, Yes!
Return
ColorGreaterThan(col,tol){
	; Returns true if all color components of color col is greater than tol.
	return ((0xff0000 & col) >> 16)>tol && ((0x00ff00 & col) >> 8)>tol && (0x0000ff & col)>tol
}
(Briefly tested)
Good luck.

Edit: Changed >= to >.
WeThotUWasAToad
Posts: 295
Joined: 19 Nov 2013, 08:44

Re: Inequality If-Else expressions for colors

10 Apr 2017, 17:22

Thanks for the responses.

The following are notes for my own future reference but any corrections or clarifications are very welcome:

Re ((0xff0000 & col) >> 16)>tol && ((0x00ff00 & col) >> 8)>tol && (0x0000ff & col)>tol

1) "0x" signifies a hex number

2) in AHK, colors are displayed as BGR, not RGB

3) The operator in a hex expression functions as a Bitwise operator so the ampersand is not addition nor does it mean to concatenate. Instead, it's a way of isolating each of the three individual colors from a 6-digit value. Anytime 00 is present in one of the color positions, it "masks" the value in the corresponding position in the other number.

For example, for some arbitrary value col:=0x70A6E7,

(0xff0000 & col) = 0x700000
(0x00ff00 & col) = 0x00A600
(0x0000ff & col) = 0x0000E7

(It's sort of analogous to dominant and recessive gene expression, ie a recessive trait [01 thru FF] is expressed only when no dominant trait [00] is present to mask it.)

4) Double greater-than signs move the value to the right (double less-than signs move it to the left) by the binary value indicated. Following is the description from the docs but even with it, I don't really understand why (since you are moving the values two positions to the right each time) the shifts are not 16 & 4 or 32 & 8 rather than 16 & 8.
Bit shift left (<<) and right (>>). Example usage: Value1 << Value2. Any floating point input is truncated to an integer prior to the calculation. Shift left (<<) is equivalent to multiplying Value1 by "2 to the Value2th power". Shift right (>>) is equivalent to dividing Value1 by "2 to the Value2th power" and rounding the result to the nearest integer leftward on the number line; for example, -3>>1 is -2.
A ------------------------------ [A LOT OF SPACE] ------------------------------ LOT

"ALOT" is not a word. It never has been a word and it never will be a word.
"A LOT" is 2 words. Remember it as though there's [A LOT OF SPACE] between them.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Inequality If-Else expressions for colors

10 Apr 2017, 21:38

I've been intending to write a maths tutorial for AutoHotkey. The questions you pose are some of the main issues to address. I'll probably use this in the tutorial.

I first learnt about: &, |, and bitshifting, in AutoHotkey. It's when you visually imagine the bits that everything becomes clear.

1) 0x. Yes it does. '0x4' can look a bit odd to a beginner, 'is that 0 times 4?' I really like the notation now though.
0x4 = 4, 0xA = 10, 0xF = 15, 0x10 = 16, 0xFF = 255, 0x100 = 256.
Better than 100 (base 16) = 100 (hex) = 256.

2) PixelGetColor is BGR by default, but there is an RGB option.

Code: Select all

PixelGetColor, vColBGR, % vPosX, % vPosY
PixelGetColor, vColRGB, % vPosX, % vPosY, RGB
Although in AHK v2, RGB will be the default.

3) & and |:

Code: Select all

44 & 38 = 36
44 | 38 = 46

for &, AND, if both bits in a column are 1, write 1, else 0
   44: 101100
   38: 100110
44&38: 100100 ;36 in decimal

for |, OR, if at least one bit in a column is 1, write 1, else 0
   44: 101100
   38: 100110
44|38: 101110 ;46 in decimal

         0xFF0000: 111111110000000000000000
         0xABCDEF: 101010111100110111101111
0xFF0000&0xABCDEF: 101010110000000000000000 ;0xAB0000
0xFF0000|0xABCDEF: 111111111100110111101111 ;0xFFCDEF
44 & 38 = 36, and, 44 | 38 = 46, were a complete mystery to me, until I read on the forum somewhere, about treating them as binary numbers, and possibly they suggested putting one top of another, like you would for a normal addition operation in school. You think you've got it ... but then you haven't ... until I found the binary approach.

4) It's odd to give an example that uses negative numbers. I never think of bitshifting in terms of negative numbers. But as often happens in maths, a formula that works for positive numbers, can be extended to apply to negative numbers also.

Think of a positive integer written in binary.
- Bitshift right: move each bit right and discard the last bit.
- Bitshift right: divide by 2 and discard the remainder.
- Bitshift left: move each bit left and append a zero.
- Bitshift left: multiply by 2.

Code: Select all

   15: 1111
15>>1:  111 ;Floor(15/(2**1)) = Floor(15/2)  = 7
15>>2:   11 ;Floor(15/(2**2)) = Floor(15/4)  = 3
15>>3:    1 ;Floor(15/(2**3)) = Floor(15/8)  = 1
15>>4:    0 ;Floor(15/(2**4)) = Floor(15/16) = 0

   15:     1111
15<<1:    11110 ;15*(2**1) = 15*2  = 30
15<<2:   111100 ;15*(2**2) = 15*4  = 60
15<<3:  1111000 ;15*(2**3) = 15*8  = 120
15<<4: 11110000 ;15*(2**4) = 15*16 = 240

0xAB0000    : 101010110000000000000000
0xAB0000>>8 :         1010101100000000 ;discard 8 bits (1 byte)
0xAB0000>>16:                 10101011 ;discard 16 bits (2 bytes)

0xAB    :                 10101011
0xAB<<8 :         1010101100000000 ;double it 8 times (multiply by 256) (move up 1 byte)
0xAB<<16: 101010110000000000000000 ;double it 16 times (multiply by 65536) (move up 2 bytes)
==================================================

[EDIT:]
[I believe this is the page where I first understood bitwise-and and bitwise-or.]
Bitwise-and Bitwise-or - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/7455 ... itwise-or/

[This link is also useful as it talks about 'rotate', e.g. rotate right where each bit moves right, and the rightmost bit becomes the leftmost bit. It also has some interesting links.]
Bitwise Operators - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=1744
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], bjukrain, TAC109 and 28 guests