Jump to content

Converting Transparency Level 0-255 To Percentage


Go to solution Solved by argumentum,

Recommended Posts

Posted

Either I had too much coffee today or possibly not enough, but the math part of my brain is not working right now. 😆

With _WinAPI_SetLayeredWindowAttributes there is a Transparency level of 0 to 255. I would like to simplify what the user needs to enter in an INI file with a value of 0 to 100, representing percentage.

So if a user enters 100, that would need to convert to 255 in a math function. 0 would obviously be 0. But it is the whole math thing that is escaping me right now.

Could someone please point me in the right direction for this? Thank you :)

Posted
ConsoleWrite( myPercentThing(200) & @CRLF)
ConsoleWrite( myPercentThing(100) & @CRLF)
ConsoleWrite( myPercentThing(50) & @CRLF)
ConsoleWrite( myPercentThing(0) & @CRLF)
ConsoleWrite( myPercentThing(-1000) & @CRLF)
Func myPercentThing($vDecimal)
    $vDecimal = ($vDecimal < 0 ? 0 : $vDecimal)
    $vDecimal = ($vDecimal > 100 ? 100 : $vDecimal)
    Return Ceiling($vDecimal * 2.55) ; round up
EndFunc

0 visibility is totally transparent. Make a minimum of 50 🤔 

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

Posted
9 minutes ago, Somerset said:

The starting digit is 0 not 1, so it is 256 levels of transparency.

You are absolutely right. So that would make the multiplier 2.56 then. Thanks for the heads up on that one.

Posted (edited)
1 hour ago, WildByDesign said:

So that would make the multiplier 2.56 then

Would be ( 2.55 * n ) + 1
But "Transparency: A number in the range 0 - 255. The lower the number, the more transparent the window will become. 255 = Solid, 0 = Invisible." 0x00 to 0xFF

Always doublecheck everything. Anyone can misremember or be just plain wrong. Me, you, @TheSaint ... anyone :shocked:

1 hour ago, Somerset said:

The starting digit is 0 not 1, so it is 256 levels of transparency.

And @Somerset can say something that is true :P

Edited by argumentum

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

Posted

I've done enough editing of image files to know a few things.

If you are going to have % of transparency you should have a small input section from 0 - 255 as exacting levels of transparency. Most people cannot tell the difference of 1% to about 5% level of transparency, but some might want the option to have exacting levels for better control. :)

42 minutes ago, argumentum said:

And @Somerset can say something that is true :P

Don't presume that about me. I don't like to lie as much as stretching the truth so far that is snaps back on me like a rubber band that has broke from the stress. :D

 

Posted
20 minutes ago, Somerset said:

Most people cannot tell the difference of 1% to about 5% level of transparency, but some might want the option to have exacting levels for better control.

True.
I rather enter a value between 0 and 255 than 0 to 100 because, why dumbify something when there is no need.
 

Spoiler
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
Example()
Func Example()
    GUICreate("slider", 220, 100, 100, 200)
    GUISetBkColor(0x00E0FFFF)     ; will change background color
    Local $idSlider1 = GUICtrlCreateSlider(10, 10, 200, 20)
    GUICtrlSetLimit(-1, 255, 0)     ; change min/max value
    Local $idButton = GUICtrlCreateButton("Value?", 75, 70, 70, 20)
    GUISetState(@SW_SHOW)
    GUICtrlSetData($idSlider1, 45)     ; set cursor
    Local $idMsg
    ; Loop until the user exits.
    Do
        $idMsg = GUIGetMsg()
        If $idMsg = $idButton Then
            MsgBox($MB_SYSTEMMODAL, "slider1", GUICtrlRead($idSlider1), 2)
        EndIf
    Until $idMsg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

 

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

Posted
6 hours ago, Somerset said:

Don't presume that about me.

Because that can make a Pres out of U and Me ... whatever a Pres is ... maybe some kind of Trump thing. :P 

6 hours ago, Somerset said:

I don't like to lie as much as stretching the truth so far that is snaps back on me like a rubber band that has broke from the stress.

Not only that, but the truth can be stretched so much it becomes unrecognizable ... that's when Oranges can be like Apples and Bananas like Sausages ... so Gastronomy suffers and we all end up with a belly ache. I don't know about you, but I prefer to not have sausages in my Fruit Salad ... or Bananas and Mash. :muttley:

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Posted (edited)

A generic map function:

;Coded by UEZ build 2025-10-29
Func Map($val, $source_start, $source_stop, $dest_start, $dest_stop)
    Return (($val - $source_start) * ($dest_stop - $dest_start) / ($source_stop - $source_start) + $dest_start)
EndFunc

$iCol = Int(Map(87, 0, 100, 0, 255))
ConsoleWrite($iCol & " - 0x" & Hex($iCol, 2) & @CRLF) ;87% from color value (0 - 255)

$iCol = 1 + Int(Map($iCol, 0, 255, 0, 100)) ;and back again
ConsoleWrite($iCol & " - 0x" & Hex($iCol, 2) & @CRLF)

 

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   1 member

×
×
  • Create New...