Jump to content

Recommended Posts

Posted

I would like to have the user provide a percentage value (without percent symbol) and have the program convert that percentage to a hex value. The hex value is needed to then feed to a function.

Please see this chart: Hexadecimal color code for transparency

Examples:

user gives 100, program converts to FF

user gives 80, program converts to CC

I have played with a bunch of hex to dec and dec to hex functions in forum but clearly it is the percentage aspect that is throwing my mind off here. I feel like I have to convert the percentage to integer or dec first but this is not an area that I am good at.

Any help would be greatly appreciated. Thank you for your time.

Posted (edited)

What is FF when converted to decimal? You have minimum of zero in hexadecimal that matches 0 in dec. You also have FF in hexadecimal that matches to what in decimal? Then map (0,100) in decimal to (0,FF) afte you have converted it to decimal.

Edited by ahmet
Posted

Hi @WildByDesign 👋 ,

this is one of rar good examples on when to use AI (in my opinion). Please have a look:
https://chatgpt.com/share/68593c0e-abac-800f-b469-a29209d5d520

I really just copy and paste your question and already got an idea of how an approach could look like.
I don't suggest to use AI to generate AutoIt code, becauce AutoIt code examples are not listed/progressed enough in the most LLMs, but I suggest to get the idea (which is close to your thoughts above) and then come up with the code/solution in AutoIt for yourself or by us as community 😀 .

Hopefully this is a good starting point.

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server, 🔗 Cheat Sheet

  Reveal hidden contents
Posted
  On 6/23/2025 at 11:09 AM, ahmet said:

What is FF when converted to decimal? You have minimum of zero in hexadecimal that matches 0 in dec. You also have FF in hexadecimal that matches to what in decimal? Then map (0,100) in decimal to (0,FF) afte you have converted it to decimal.

Expand  

This is a good approach. And thank you for the quick reply as well. I like the idea of defining these ahead of time like you suggest. However, I have never done this before and also searching the forums and functions only come up with details about the newer Map functions (MapKeys, etc.) which of course are not related to what you suggest, I assume.

I am pretty sure that I can do the math conversion that you suggest. But I'm not sure how to define them as you suggest mapping the values. Could you please provide a bit more info about that aspect or example? Thank you

Posted
  On 6/23/2025 at 11:41 AM, SOLVE-SMART said:

this is one of rar good examples on when to use AI (in my opinion). Please have a look:
https://chatgpt.com/share/68593c0e-abac-800f-b469-a29209d5d520

I really just copy and paste your question and already got an idea of how an approach could look like.
I don't suggest to use AI to generate AutoIt code, becauce AutoIt code examples are not listed/progressed enough in the most LLMs, but I suggest to get the idea (which is close to your thoughts above) and then come up with the code/solution in AutoIt for yourself or by us as community 😀 .

Expand  

Well that is actually really interesting. And thank you for your reply as well, by the way.

I am still at that stage where I haven't really looked at AI at all yet. So looking at your AI response link there is my first actual look into something from AI. And to be honest, the response is really quite helpful. Particularly the way that it breaks down how it works.

One reason why I have stayed away from AI is because I am still very new to AutoIt and I really would like to learn and fully understand things as best as possible. And then maybe dip into AI if needed. But this is very helped, I must admit. Thank you. :)

Posted (edited)

If all this is for coloring, there is code for all that in https://www.autoitscript.com/forum/files/file/489-my-fine-tuned-high-contrast-theme/

  Reveal hidden contents


If is for Hex, do give the user 255% because, why not. It'd simplify your code, enlighten the user, and gives fine control ( other wise you'd have to calculate "value * 2.55" each step ) for those that are very picky with colors.

I look at it as from zero to Maximum Effort !

Edited by argumentum
gotta have fun

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted
  On 6/23/2025 at 1:46 PM, argumentum said:

If is for Hex, do give the user 255% because, why not. It'd simplify your code, enlighten the user, and gives fine control ( other wise you'd have to calculate "value * 2.55" each step ) for those that are very picky with colors.

Expand  

It is just for coloring, but it has to be specific to work properly. It took me weeks, but I finally figured out how to add color hint to the blur behind for any color.

I amended the original _WinAPI_DwmEnableBlurBehindWindow10 function by @scintilla4evr by adding optional color hint to the blur and color intensity (technically alpha).

As follows:

  Reveal hidden contents

I am calling that with:

Local $iIntensity = '80'
Local $iTintColor = '0x0078D4'
Local $iTintColorSwitch = Hex(_WinAPI_SwitchColor($iTintColor), 6)
Local $iColor = '0x' & $iIntensity & $iTintColorSwitch

_WinAPI_DwmEnableBlurBehindWindow10($hGUI, True, $iColor)

The '80' refers to 50% which I am doing for now. But I really need the conversion.

The API requires that the color comes in like:

'0xFF0000FF'

The last 6 are BGR which I've switched to RGB to make it easier for the user.

The first 2 after the '0x' are what I need. Those deal with the alpha transparency and I want to make that easier for the user to supply. I don't want the user to have to supply B8 or FF, etc.

I would like the user to supply anything between 0 and 100. And have the code convert to that 2 char hex.

Posted
  On 6/23/2025 at 1:58 PM, WildByDesign said:

I would like the user to supply anything between 0 and 100. And have the code convert to that 2 char hex.

Expand  
ConsoleWrite( percentageOfHex(110) & @CRLF)
ConsoleWrite( percentageOfHex(80) & @CRLF)
ConsoleWrite( percentageOfHex(-2) & @CRLF)
Func percentageOfHex($iVal)
    $iVal = Int($iVal > 99 ? 100 : ($iVal < 1 ? 0 : $iVal)) ; no more than 100% or less than 0%
    Return Hex(Ceiling(($iVal * 100) * (2.55 * 100) / (100 * 100)), 2) ; calculate in integers, as floating point numbers suck in a CPU
EndFunc

 

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

  • Solution
Posted (edited)
  On 6/23/2025 at 1:58 PM, WildByDesign said:

Those deal with the alpha transparency

Expand  
ConsoleWrite(percentageOfAlpha(80, 0xFF1234FF) & @CRLF)
ConsoleWrite(percentageOfAlpha(80, '0xFF0000FF') & @CRLF)
ConsoleWrite(percentageOfAlpha(80, 0x0000FF) & @CRLF)
ConsoleWrite(percentageOfAlpha(80, '0x0000FF') & @CRLF)
Func percentageOfAlpha($iVal, $iColor)
    Return "0x" & percentageOfHex($iVal) & Hex($iColor, 6)
;~  Local $a = StringSplit(Hex($iColor, 6), "", 0)
;~  Return "0x" & percentageOfHex($iVal) & $a[$a[0] - 5] & $a[$a[0] - 4] & $a[$a[0] - 3] & $a[$a[0] - 2] & $a[$a[0] - 1] & $a[$a[0] - 0]
EndFunc   ;==>percentageOfAlpha

Func percentageOfHex($iVal)
    $iVal = Int($iVal > 99 ? 100 : ($iVal < 1 ? 0 : $iVal)) ; no more than 100% or less than 0%
    Return Hex(Ceiling(($iVal * 100) * (2.55 * 100) / (100 * 100)), 2) ; calculate in integers, as floating point numbers suck in a CPU
EndFunc   ;==>percentageOfHex

gotta say, my math is exquisite :D 

Edited by argumentum
better ?

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Posted

Try:

Local $iIntensity = '80' ;hex
Local $iTintColor = '0x0078D4'

Local $iColor = BitOR(BitShift(Dec($iIntensity), -24), Int($iTintColor))
ConsoleWrite(Hex($iColor, 8) & @CRLF)

_WinAPI_DwmEnableBlurBehindWindow10($hGUI, True, $iColor)

 

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted
  On 6/23/2025 at 2:52 PM, argumentum said:

gotta say, my math is exquisite

Expand  

The more that I play around with these functions of yours, the more I realize how miraculous they are. One word: Magic! :)

I have implemented this in my project now and it helps tremendously. Thank you again. Marked as Solution.

  On 6/23/2025 at 3:36 PM, UEZ said:

Try:

Expand  

Thank you for your reply and for your time. I appreciate it. I really like your technique with this and that knowledge that I gained from your example will likely help me in other projects. So I definitely benefited from your example. The only problem is that it did not return the correct colorref transparency hex in the first two characters after the '0x'.

Posted (edited)
  On 6/23/2025 at 4:30 PM, WildByDesign said:

Thank you for your reply and for your time. I appreciate it. I really like your technique with this and that knowledge that I gained from your example will likely help me in other projects. So I definitely benefited from your example. The only problem is that it did not return the correct colorref transparency hex in the first two characters after the '0x'.

Expand  

I misunderstood the value but it's easy, too:

Local $iIntensity = '80' ;% of the alpha channel
Local $iTintColor = '0x0078D4'
$iIntensity = Int($iIntensity) * 255 / 100
Local $iColor = BitOR(BitShift($iIntensity > 255 ? 255 : $iIntensity < 0 ? 0: $iIntensity, -24), Int($iTintColor))
ConsoleWrite(Hex($iColor, 8) & @CRLF)

_WinAPI_DwmEnableBlurBehindWindow10($hGUI, True, $iColor)

 

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Posted

Chatgpt gave a nice answer I feel based on the table given it even suggest things about gamma correction on how humans perceive color so then it gives you clues to think broader then just percentage to hex if its about colors on a monitor 😉 

 

#FUNCTION# ====================================================================================================================
; Name...........: _PercentToHex
; Description ...: Converts a percentage (0–100) to a two-digit hex value (0x00–0xFF) following a gamma-like curve.
; Syntax.........: _PercentToHex($iPercent)
; Parameters ....: $iPercent - Integer between 0 and 100
; Return values .: Hex string (e.g. "FF", "A3", "00")
; ===============================================================================================================================
Func _PercentToHex($iPercent)
    If $iPercent < 0 Then $iPercent = 0
    If $iPercent > 100 Then $iPercent = 100

    ; Apply gamma-corrected curve
    Local $gamma = 2.2
    Local $normalized = $iPercent / 100
    Local $adjusted = $normalized ^ $gamma
    Local $intVal = Round($adjusted * 255)

    Return StringFormat("%02X", $intVal)
EndFunc

For $i = 100 To 0 Step -1
    ConsoleWrite($i & "%: " & _PercentToHex($i) & @CRLF)
Next

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...