PixelGetColor 的 RGB 值在 Autohotkey 中不起作用

RGB Value With PixelGetColor Not Working in Autohotkey

使用 PixelGetColor 命令的 RGB(十进制)值在 Autohotkey 中不起作用。

如果你使用命令行,我确实期望:PixelGetColor, color, %MouseX%, %MouseY%, RGB

它会给我一个 [RGB(十进制)值。] 但它会给我一个 [RGB(十六进制)]

如果您使用命令行,则默认为:PixelGetColor, color, %MouseX%, %MouseY%

然后它会给我一个 [RGB(十六进制)值] 作为结果,但它会给我一个 [BGR(十六进制)值]

注意 - 我确实根据评论重新编辑了我的问题!

我最近听说了 BGR 格式,但我不明白我们为什么要那样。

我认为用户和我想要更多这样的结果 [默认值:RGB> 十六进制值] 以及参数 [RGB> 十进制值]

现在的问题是,如何从 PixelGetColor 命令获取 RGB(十进制)值。

这个 AHK 脚本不工作。

颜色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 文档命令 > PixelGetColor

如果你想获得[RGB值]

您可以先使用 PixelGetColor 命令

然后使用函数转换 HexToRgb(color)

试试这个 AHK 脚本:

颜色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"), "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

}

虽然另一个答案有效并且确实很聪明,但它有点冗长和复杂。 Format 函数是内置的,可以转换十六进制。到 12 月

因此,使用您的代码并添加一行用于格式化,我们可以这样解决:

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

我敢打赌还有一种更有效的方法,但目前我还无法做到。