1
votes

RGB(Decimal) Value With PixelGetColor Command does Not Working in Autohotkey.

I did Expect if you use the command Line: PixelGetColor, color, %MouseX%, %MouseY%, RGB

That it Will Give me as Result, a [RGB (Decimal) Value.] but it give me a [RGB (hex)]

and as Default if you use the command Line: PixelGetColor, color, %MouseX%, %MouseY%

That it then Will Give me as Result, a [RGB (Hex) Value] but it give me a [BGR (Hex) Value]

Note - i did re-edit my Question based on comments!

I Newer Hear from a BGR Format, and i do not understand why we want that.

I think the Users and me want more Results like this [Default:RGB>Hex Value] and with the Parameter [,RGB>Decimal Value]

The Question now is, How can i get the RGB(decimal) Value from PixelGetColor Command.

This AHK Script does not work.

Color Picker.ahk

;#notrayicon
#SingleInstance force

; + = Shift
; ! = Alt
; ^ = Ctrl
; # = Win (Windows logo key)

esc::exitapp ;You can click the (esc) key to stop the script.

f1::  
MouseGetPos MouseX, MouseY
;PixelGetColor, color, %MouseX%, %MouseY% ;The default result is a BGR>Hex Value - i wish this should be RGB>Hex
PixelGetColor, color, %MouseX%, %MouseY%, RGB ;RGB Parameter i wish it should be have a RGB>Decimal value - otherwise this parameter does not have for me a useful function. 
MsgBox,, , The color at the current cursor position is %color%., 3
return

Autohotkey docs Commands > PixelGetColor

color picker

2
You've misunderstood what the RGB option does. Note that "RGB value" can be commonly seen as hex. or dec. and that it isn't implicitly decimal which your question suggests. It just so happens that AHK retrieves RGB values as hex. Thus, it does work correctly. I proposed an edit that clarified that you want the decimal values, but you rejected it.... Anyway, I would suggest clarifying your question to reflect what you're actually asking - which is a conversion from hex RGB values to decimal RGB values. - EJE
@Evan Elrod - You are right that a "RGB value" can be a (hex value) or (decimal value) - the reason why i did rejected your editing is this - the Parameter ,RGB must be have an function - the default command code for hex is PixelGetColor, color, %MouseX%, %MouseY% and then for decimal should be PixelGetColor, color, %MouseX%, %MouseY%, RGB - i Hope you understand why i did do that - but any way i want to thank you for that info. - stevecody
The RGB option for the PixelGetColor command does not give it in decimal. It specifies the order. The default will list it as BGR, with the red an blue swapped. From the link you posted, "RGB: Retrieves the color in RGB vs. BGR format. In other words, the red and the blue components are swapped." - EJE
@Evan Elrod - That is strange i newer hear from a BGR format, but you are right, that the default is BGR and that the red & Blue is swapped. i do not understand why they want that - i think the Users and me want more like this [Default = RGB>HEX Value] and with the Parameter [,RGB = RGB>Decimal Value] - But thanks for that info i will re-edit my Question - stevecody
I agree that it would be useful to have the decimal value as an alternative output. I also haven't had any need for BGR. - EJE

2 Answers

3
votes

While the other answer works and is indeed clever, it is a bit lengthy and complex. The Format function is built-in and can convert hex. to dec.

So, using your code and adding one line for formatting, we can solve it with this:

f1::
MouseGetPos MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%, RGB
color := Format( "{1:u},{1:u},{1:u}", "0x" . SubStr(color, 3, 2), "0x" . SubStr(color, 5, 2), "0x" . SubStr(color, 7, 2))
MsgBox,, , The color at the current cursor position is %color%., 3
return

I would bet that there is an even more efficient method, but that is beyond me at the moment.

1
votes

If you want to get the [RGB Value]

you can first use the PixelGetColor Command

And then use a Function to Convert HexToRgb(color)

try this AHK Script:

Color Picker.ahk

;#notrayicon
#SingleInstance force

; + = Shift
; ! = Alt
; ^ = Ctrl
; # = Win (Windows logo key)

esc::exitapp ;You can click the (esc) key to stop the script.

f1::  
MouseGetPos MouseX, MouseY
PixelGetColor, color, %MouseX%, %MouseY%, RGB 
a := HexToRgb(color)
MsgBox,, , The color at the current cursor position is %a%, 0
;MsgBox, % "Example:`n`n" . rgbToHex("255,255,255") . "`n" . HexToRgb("0xFFFFFF") . "`n" . CheckHexC("000000")
return


rgbToHex(s, d = "") {

    StringSplit, s, s, % d = "" ? "," : d

    SetFormat, Integer, % (f := A_FormatInteger) = "D" ? "H" : f

    h := s1 + 0 . s2 + 0 . s3 + 0

    SetFormat, Integer, %f%

    Return, "#" . RegExReplace(RegExReplace(h, "0x(.)(?=$|0x)", "0$1"), "0x")

}



hexToRgb(s, d = "") {

    SetFormat, Integer, % (f := A_FormatInteger) = "H" ? "D" : f

    Loop, % StrLen(s := RegExReplace(s, "^(?:0x|#)")) // 2

        c%A_Index% := 0 + (h := "0x" . SubStr(s, A_Index * 2 - 1, 2))

    SetFormat, Integer, %f%

    Return, c1 . (d := d = "" ? "," : d) . c2 . d . c3

}



CheckHexC(s, d = "") {

    If InStr(s, (d := d = "" ? "," : d))

        e := hexToRgb(rgbToHex(s, d), d) = s

    Else e := rgbToHex(hexToRgb(s)) = (InStr(s, "#") ? "" : "#"

        . RegExReplace(s, "^0x"))

    Return, e

}

rgb color picker