HSB to HEX Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

HSB to HEX

17 Feb 2018, 14:56

Good afternoon everyone. I've searched the forums and found a few results, but unable to find an exact solution.

can AHK convert HSB to HEX?

The HSB digits would be 288 65 76

Does anyone might know how to do this? Many thanks in advance.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: HSB to HEX

17 Feb 2018, 15:27

Try this: https://autohotkey.com/boards/viewtopic ... sb#p144347 ("HSV stands for hue, saturation, and value, and is also often called HSB (B for brightness)."

Edit: Not sure, if this lib will really give you HEX values :think:
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: HSB to HEX

17 Feb 2018, 15:41

thanks Gregster... this is daunting. I'm trying to work with some of the code in that topic... looks like I might need to convert HSV(same as HSB) to RGB, then to HEX?
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: HSB to HEX

17 Feb 2018, 15:44

I first (falsely) assumed you wanted RGB. Can't you just take the single values and transform them with Transform and/or SetFormat to HEX?
Last edited by gregster on 17 Feb 2018, 15:49, edited 1 time in total.
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: HSB to HEX

17 Feb 2018, 15:47

Sorry, I'm not sure... I was looking to convert HSB (or, HSV- whichever one AHK understands since they are both the same), to HEX.

I'm trying out some code on the link but it's too far over my head.

Is it an easy task to transform them with SetFormat from HSV/HSB to HEX?

I can post the code I've gathered so far, but it's really long.. perhaps uneccisarily.
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: HSB to HEX

17 Feb 2018, 15:57

Not tested, but people made already functions like Dec2Hex here: https://autohotkey.com/boards/viewtopic ... Hex#p21249

Code: Select all

; Converts a decimal value to hex
; Example:    x := Dec2Hex(10) will return "0A".
Dec2Hex(h)
    {
    format = %A_FormatInteger%
    SetFormat, Integer, hex
    H += 0
    SetFormat, Integer, d
    StringLeft, C, H, 2
    If C = 0x
        {
        L := StrLen(h) -2
        StringRight, h, h, L
        }

    If StrLen(h) < 2
        H = 0%h%
    StringUpper, h, h
    SetFormat Integer, %format%
    Return, "0x" . h
    }
or just this?

Code: Select all

Num := 288 
SetFormat, IntegerFast, hex
Num .= ""  ; Necessary due to the "fast" mode. see 'SetFormat' docs
MsgBox % Num
Last edited by gregster on 17 Feb 2018, 16:09, edited 2 times in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: HSB to HEX

17 Feb 2018, 16:05

Might come in handy at some point.

Code: Select all

q:: ;dec2hex / hex2dec
;dec2hex
vNum := 255
MsgBox, % Format("0x{:X}", vNum)

;hex2dec
vHex := 0xFF
MsgBox, % vHex+0
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: HSB to HEX

17 Feb 2018, 16:31

thank you to you both. The values I'm trying out are:

hue=288
saturation=65
brightness(or is it value?)=76


I am trying out the following code.

Code: Select all

;dec2hex:
vNum := 2886576 ;this is the hue, saturation and brightness values all in one string
MsgBox, % Format("0x{:X}", vNum)

;hex2dec:
vHex := 0xFF
MsgBox, % vHex+0
return
..it returns 0x2C0BB0, which is a blue/purple type color.

but when entering 288,65,76 values into the Photoshop HSB fields respectively, it converts those values to 0xa944c2.
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: HSB to HEX

17 Feb 2018, 16:37

if I know how to upload images here, I will post a screenshot

here we go:



https://ibb.co/joBegS

can I embed the image here?
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: HSB to HEX

17 Feb 2018, 16:47

I guess, you have to separate the hex into three values (two digits per value): a9 44 c2 . Otherwise, you have only one big number without color meaning.

BTW, a9 in hex is 169 in decimal, and 44 is 68, so... this is the hex of the RGB values
Last edited by gregster on 17 Feb 2018, 16:57, edited 2 times in total.
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: HSB to HEX

17 Feb 2018, 16:53

Thanks again. Man, I don't know. Might have to find another way to do this. Appreciate the help to both of you.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: HSB to HEX  Topic is solved

17 Feb 2018, 16:55

- These calculations gave the same result as Photoshop, although, I just computed the numbers, I can't vouch for their accuracy. Multiplying by 255 didn't quite give the right answers, although multiplying by 256 did. [EDIT: Multiplying by 255 and rounding gave the right answers.]
- I'd been meaning to look at HSV at some point, so thanks for the reminder.
- The HSV_Convert2RGB() function is from jballi's library mentioned above.
- This image gave me a clue, as to what needed to be done:
PS-colorPicker.png
http://livecode.byu.edu/images/photosho ... Picker.png

Code: Select all

q:: ;HSV degree/%/% to RGB as hex
vHSV := "288,65,76"
oHSV := StrSplit(vHSV, ",")
oCol := HSV_Convert2RGB(oHSV.1/360, oHSV.2/100, oHSV.3/100)
MsgBox, % Format("0x{:02X}{:02X}{:02X}", Round(oCol.r*255), Round(oCol.g*255), Round(oCol.b*255)) ;0xA944C2
return

;[HSV_Convert2RGB function]
;[Library] HSL & HSV v0.1 (Preview) - Convert RGB To/From HSL or HSV - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=6&t=30908
HSV_Convert2RGB(h,s,v)
    {
    if (s=0)
        Return {r:v,g:v,b:v}

    hh:=h
    if (hh=1)  ;-- Exactly 360 degrees
        hh:=0.0

    hh*=6.0
    i:=Floor(hh)

    ff:=hh-i
    p:=v*(1.0-s)
    q:=v*(1.0-(s*ff))
    t:=v*(1.0-(s*(1.0-ff)))

    if (i=0)
        {
        r:=v
        g:=t
        b:=p
        }
     else if (i=1)
        {
        r:=q
        g:=v
        b:=p
        }
     else if (i=2)
        {
        r:=p
        g:=v
        b:=t
        }
     else if (i=3)
        {
        r:=p
        g:=q
        b:=v
        }
     else if (i=4)
        {
        r:=t
        g:=p
        b:=v
        }
     else
        {
        r:=v
        g:=p
        b:=q
        }

    Return {r:r,g:g,b:b}
    }
Last edited by jeeswg on 26 Feb 2018, 00:47, edited 4 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: HSB to HEX

17 Feb 2018, 16:57

But is HSB really used as Hex, anyway? The first value could lead to more than 2 digits... so how to parse such a hex string (288 would be 0x120)? So you would have to keep the 3 values separate, I guess. In RGB, that is no problem because max. value is 255 = 0xFF

But I admit, I have no clue about colors and their formats... ;)
User avatar
boiler
Posts: 16920
Joined: 21 Dec 2014, 02:44

Re: HSB to HEX

17 Feb 2018, 17:17

scriptor2016 wrote:thank you to you both. The values I'm trying out are:

hue=288
saturation=65
brightness(or is it value?)=76


I am trying out the following code.

Code: Select all

;dec2hex:
vNum := 2886576 ;this is the hue, saturation and brightness values all in one string
MsgBox, % Format("0x{:X}", vNum)

;hex2dec:
vHex := 0xFF
MsgBox, % vHex+0
return
..it returns 0x2C0BB0, which is a blue/purple type color.

but when entering 288,65,76 values into the Photoshop HSB fields respectively, it converts those values to 0xa944c2.
What you are attempting to do here doesn't make sense. You can't just butt up the individual values of 288, 65, and 76 into the integer 2886576 have it be meaningful, let alone convert it to its hex equivalent and have that be meaningful.

Also, the premise of the thread doesn't make sense. Converting HSB to to hex isn't a thing. HSB is a method of describing colors. Hex is a numbering system. As has been suggested, and since you say above "is a blue/purple type color", you are looking to convert HSB to RGB. That requires algorithms to convert between the color description models, which has been done in AHK by jballi here:
https://autohotkey.com/boards/viewtopic.php?f=6&t=30908
scriptor2016
Posts: 854
Joined: 21 Dec 2015, 02:34

Re: HSB to HEX

17 Feb 2018, 23:14

hi jeeswg, I apologize but I just saw your code now-- this works!!! I can't thank you enough!! Your script should be a sticky topic, as it successfully converts an HSV value to a HEX value. I haven't found any other code anywhere that works except this one. It has allowed me to move my project to near-completion- the remaining part is for me to iron out on my end.

Again I thank you both :)
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Re: HSB to HEX

24 Feb 2018, 21:30

Old post but I just discovered it.
jeeswg wrote:Multiplying by 255 didn't quite give the right answers, although multiplying by 256 did.
Although there are 256 values in an RGB color, they range from 0 to 255 and since the return value from the function is represented by a real number from 0 to 1, multiplying the result by 255 to convert the value to a RGB color is correct. However, since a real number from 0 to 1 can represent billions of possible values, the conversion must be rounded in order to generate the correct results. Ex: Green:=Round(oCol.g*255). I tested this conversion on this example and it appears to do the trick.

I apologize for waking the dead. I just wanted to add my 2 cents.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: HSB to HEX

26 Feb 2018, 00:56

- Thanks jballi, makes sense that 0 to 1 maps to 0 to 255, and that rounding to the nearest integer should be done (not rounding down). I've corrected the script above.
- I've always found it curious, the range 0 to 255, surely 0 to 256 (257 numbers) would have made more sense, but you only get 256 numbers in a byte. You get odd things like RGB 0x808080 v. 0xFFFFFF, not quite double. I had thought that a system could limit everything to 0 to 128 (129 numbers), although obviously there are a lot of wasted bits there.

Code: Select all

;incorrect
MsgBox, % Format("0x{:02X}{:02X}{:02X}", oCol.r*256, oCol.g*256, oCol.b*256) ;0xA944C2
;correct
MsgBox, % Format("0x{:02X}{:02X}{:02X}", Round(oCol.r*255), Round(oCol.g*255), Round(oCol.b*255)) ;0xA944C2
- Many thanks for the conversion functions.
- Btw do you find HSL and HSV to have advantages over RGB in certain situations? Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jballi
Posts: 724
Joined: 29 Sep 2013, 17:34

Re: HSB to HEX

26 Feb 2018, 03:33

jeeswg wrote:- Btw do you find HSL and HSV to have advantages over RGB in certain situations? Cheers.
Yes. HSL/HSV gives the developer, artist, or whomever a reliable way to make changes to a color. For example, changing a color's brightness is a snap with HSL/HSV. And no, you can't just add or subtract equal amounts of Red, Green, and Blue to get the same thing. The Microsoft Color Dialog Box which uses HSL does this with the vertical color bar that can be dragged up or down to change the color's brightness.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, Nerafius and 103 guests