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

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted
42 minutes ago, 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.

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
14 minutes ago, 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 😀 .

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/

Spoiler

MyFineTunedHCT(0_2019_12.11).png.7db1614


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
3 minutes ago, 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.

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:

Spoiler
; #FUNCTION# ====================================================================================================================
; Name ..........: _WinAPI_DwmEnableBlurBehindWindow10
; Description ...: Enables Aero-like blurred background in Windows 10.
; Syntax ........: _WinAPI_DwmEnableBlurBehindWindow10($hWnd[, $iMode = $ACCENT_ENABLE_BLURBEHIND])
; Parameters ....: $hWnd                - Window handle.
;                  $bEnable             - [optional] Enable or disable the blur effect.
;                  $iBlurColor          - [optional] Sets blur tint color and intensity (alpha)
; Return values .: 1 on success, 0 otherwise. Call _WinAPI_GetLastError on failure for more information.
; Author ........: scintilla4evr
; Modified ......: WildByDesign
; Remarks .......:
; Related .......:
; Link ..........: http://vhanla.codigobit.info/2015/07/enable-windows-10-aero-glass-aka-blur.html and http://undoc.airesoft.co.uk/user32.dll/SetWindowCompositionAttribute.php
; Example .......: Yes
;    ACCENT_DISABLED = 0
;    ACCENT_ENABLE_GRADIENT = 1
;    ACCENT_ENABLE_TRANSPARENTGRADIENT = 2
;    ACCENT_ENABLE_BLURBEHIND = 3           ; works great
;    ACCENT_ENABLE_ACRYLICBLURBEHIND = 4    ; works great
;    ACCENT_ENABLE_HOSTBACKDROP = 5
;    ACCENT_INVALID_STATE = 6
; ===============================================================================================================================
Func _WinAPI_DwmEnableBlurBehindWindow10($hWnd, $bEnable = True, $iBlurColor = "")
    Local $tAccentPolicy = DllStructCreate("int AccentState; int AccentFlags; int GradientColor; int AnimationId")
    Local $tAttrData = DllStructCreate("dword Attribute; ptr DataBuffer; ulong Size")
    $tAccentPolicy.AccentState = $bEnable ? 4 : 0
    If $iBlurColor <> "" Then $tAccentPolicy.GradientColor = $iBlurColor
    $tAttrData.Attribute = 19 ; WCA_ACCENT_POLICY
    $tAttrData.DataBuffer = DllStructGetPtr($tAccentPolicy)
    $tAttrData.Size = DllStructGetSize($tAccentPolicy)

    Local $aResult = DllCall("user32.dll", "bool", "SetWindowCompositionAttribute", "hwnd", $hWnd, "ptr", DllStructGetPtr($tAttrData))
    If @error Then Return SetError(@error, @extended, 0)

    Return $aResult[0]
EndFunc

 

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
31 minutes ago, WildByDesign said:

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

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)
1 hour ago, WildByDesign said:

Those deal with the alpha transparency

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
1 hour ago, argumentum said:

gotta say, my math is exquisite

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.

52 minutes ago, UEZ said:

Try:

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)
2 hours ago, 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'.

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!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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...