format hex to dec and dec to hex Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

format hex to dec and dec to hex

Post by Hellbent » 04 Aug 2021, 16:02

I have banged my head against this problem a number of times now and despite looking it up over and over again I always end up having to brute force things to work.

All I want is to take a hex value, split it up from AARRGGBB to Red := RR, Green := GG, Blue := BB then convert that to dec

ff = 255

Then with another value, take the dec, convert to hex and repeat the steps above.

Code: Select all

;__________________________________________________________________________________
msgbox, % sRed := SubStr( SearchColor, 5, 2)  ;Should be "FF" or something like the (a 2 digit hex value)

msgbox, % sRed := Dec( sRed ) ;Should be "255" or something like that
;__________________________________________________________________________________

color := "Dec Number" ;336763294623

color := Hex( color ) ;0xFFFFFFFF

cRed :=  SubStr( SearchColor, 5, 2) ;"FF"

cRed := Dec( cRed ) ;255



Source code

Code: Select all

SetPixels( ByRef pBitmap , obj , SearchColor , ReplaceColor , Tolerance := 0 ){
	sRed := SubStr( SearchColor, 5, 2)
	sGreen := SubStr( SearchColor, 7, 2)
	sBlue := SubStr( SearchColor, 9, 2)
	sRed := "0x" sRed , sGreen := "0x" sGreen , sBlue := "0x" sBlue
	sRed := sRed+0  , sGreen := sGreen+0 , sBlue := sBlue+0
	y := obj.Y
	
	;~ sRed := sRed+0  , sGreen := sGreen+0 , sBlue := sBlue+0
	Loop, % obj.H 	{
		x := obj.X
		Loop, % obj.W	{
			SetFormat, IntegerFast, hex
			col := Gdip_GetPixel( pBitmap, x , y )
		;~ MsgBox, % col
			cRed := SubStr( col, 5, 2)
			cGreen := SubStr( col, 7, 2)
			cBlue := SubStr( col, 9, 2)
			SetFormat, IntegerFast, dec
			sRed := sRed+0  , sGreen := sGreen+0 , sBlue := sBlue+0
			;~ cRed := "0x" cRed , cGreen := "0x" cGreen , cBlue := "0x" cBlue
			cRed := cRed+0  , cGreen := cGreen+0 , cBlue := cBlue+0
			;~ MsgBox, % cRed " " sRed "`n" cGreen " " sGreen "`n" cBlue " " sBlue
			if( col = SearchColor || ( cRed >= ( sRed - Tolerance ) && cRed <= ( sRed + Tolerance ) && cGreen >= ( sGreen - Tolerance ) && cGreen <= ( sGreen + Tolerance ) cBlue >= ( sBlue - Tolerance ) && cBlue <= ( sBlue + Tolerance ) ) ){
				Gdip_SetPixel( pBitmap , x , y , ReplaceColor )
				MsgBox, % cRed " " sRed "`n" cGreen " " sGreen "`n" cBlue " " sBlue
			}
			x++
		}
		y++
	}
	;~ SetFormat, IntegerFast, dec
}
I feel like I would have a easier time if I just started braking it down ( n * 16**0 ) ( n * 16**1 ) ( n * 16**2 ) at least then I would get what I wanted...

*Note* I did try Format https://www.autohotkey.com/docs/commands/Format.htm, but have no love.

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: format hex to dec and dec to hex

Post by Hellbent » 04 Aug 2021, 16:46

This works.

Code: Select all

SetPixels( ByRef pBitmap , obj , SearchColor , ReplaceColor , Tolerance := 0 ){
	sRed := SubStr( SearchColor, 5, 2) , sGreen := SubStr( SearchColor, 7, 2) , sBlue := SubStr( SearchColor, 9, 2)
	sRed := "0x" sRed , sGreen := "0x" sGreen , sBlue := "0x" sBlue
	sRed := sRed+0 , sGreen := sGreen+0 , sBlue := sBlue+0
	y := obj.Y
	Loop, % obj.H 	{
		x := obj.X
		Loop, % obj.W	{
			SetFormat, IntegerFast, hex
			col := Gdip_GetPixel( pBitmap, x , y )
			SetFormat, IntegerFast, dec
			cRed := SubStr( col, 5, 2) , cGreen := SubStr( col, 7, 2) , cBlue := SubStr( col, 9, 2)
			cRed := "0x" cRed , cGreen := "0x" cGreen , cBlue := "0x" cBlue
			cRed := cRed+0  , cGreen := cGreen+0 , cBlue := cBlue+0
			if( col = SearchColor || ( cRed >= sRed - Tolerance && cRed <= sRed + Tolerance  && cGreen >= sGreen - Tolerance && cGreen <= sGreen + Tolerance && cBlue >= sBlue - Tolerance && cBlue <= sBlue + Tolerance ) ) 
				Gdip_SetPixel( pBitmap , x , y , ReplaceColor )
			x++
		}
		y++
	}
}

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: format hex to dec and dec to hex

Post by jNizM » 05 Aug 2021, 02:28

[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: format hex to dec and dec to hex  Topic is solved

Post by Hellbent » 05 Aug 2021, 17:51

jNizM wrote:
05 Aug 2021, 02:28
Long time ago I did this: https://www.autohotkey.com/boards/viewtopic.php?t=3925&p=312862
Thanks.

I adapted it to use alpha and use variables rather than a string.

Code: Select all

hex2rgb( CR , ByRef R , ByRef G , ByRef B , ByRef A := "NA" ){ ;https://www.autohotkey.com/boards/viewtopic.php?t=3925&p=312862
    H := InStr(CR, "0x") ? CR : ( InStr(CR, "#" ) ? "0x" SubStr( CR , 2 ) : "0x" CR )
	R := (H & 0xFF0000) >> 16 , G := (H & 0xFF00) >> 8 , B := (H & 0xFF) , ( A != "NA" ) ? ( A := (H & 0xFF000000) >> 24 ) 
}

Post Reply

Return to “Ask for Help (v1)”