Color Transparency
#1

How do I change a color transparency, applying a fade in/out effect?

E.G.:
I have 0x33AA33FF which is color green, it will slowly fade out making the alpha value FF change to 99 and then so on until it become zero which is invisible/transparent. (vice-versa)
Reply
#2

just decrease and set the alpha
Reply
#3

Quote:
Originally Posted by Skream
View Post
just decrease and set the alpha
How?

(color - 0x99) ??
Reply
#4

https://sampforum.blast.hk/showthread.php?tid=422245
Reply
#5

PHP Code:
((color 0xFFFFFF00) | (alpha 0xFF)) 
Reply
#6

Quote:
Originally Posted by JaKe Elite
View Post
How?

(color - 0x99) ??
pawn Code:
#define SetAlpha(%0,%1)              (((%0) & 0xFFFFFF00) | ((%1) & 0xFF))
pawn Code:
SetAlpha(color, 0x30) // 2 - alpha
Reply
#7

Ahh thank you, I will try that out cheers!
Reply
#8

PHP Code:
while (color 0xFF) --color
Reply
#9

Alright now with all of those stuffs setted up, I have no idea on how will I do the fade in/out effect but I have already setted up the function & the timer.

PHP Code:
stock ShowPlayerObjective(playeridcolorstring[], time)
{
    
PlayerTextDrawSetString(playeridplayer_Textdraw[playerid][0], string);
    
PlayerTextDrawColor(playeridplayer_Textdraw[playerid][0], color);
    
PlayerTextDrawShow(playeridplayer_Textdraw[playerid][0]);
    
PlayerInfo[playerid][ptdTimer] = SetTimerEx("TextdrawFadeOut"100true"ddds"playeridcolortimestring);
    return 
1;
}

public 
TextdtawFadeOut(playeridcolortimestring[])
{
    
PlayerInfo[playerid][ptdCount] += 1000;


Reply
#10

The easiest way is taking the color only (without transparency), eg:

Code:
0xFF33CC00
the last two digits are the Alpha channel, which is ranging from 0x00 to 0xFF (0 - 255). That means you can just add a value from 0 to 255 to the base color, 0 being invisible and 255 being completely visible.

For a fade in/out to take 5000 ms you can do the following calculation for the color:

Fade in:

Code:
PlayerTextDrawColor(playerid, player_TextDraw[playerid][0], 0xFF33CC00 + floatround(float(1250) / float(5000) * 255.0));
Fade out:

Code:
PlayerTextDrawColor(playerid, player_TextDraw[playerid][0], 0xFF33CC00 + 255 - floatround(float(1250) / float(5000) * 255.0));
In this example 5000 is the total time, 1250 is the time in ms that had already passed.

Using float for the calculation is important here, since 1250 / 5000 would be zero if calculated as integer values. floatround will then convert it to an integer value.

Make sure that the time passed is never higher than the time it should take in total, otherwise it will screw up the color as adding 256 to 0xFF33CC00 would change the Blue color channel.

You can use the SetAlpha function Skream suggested too, but it doesn't make a difference in this example.
Reply
#11

Quote:
Originally Posted by NaS
View Post
The easiest way is taking the color only (without transparency), eg:

Code:
0xFF33CC00
the last two digits are the Alpha channel, which is ranging from 0x00 to 0xFF (0 - 255). That means you can just add a value from 0 to 255 to the base color, 0 being invisible and 255 being completely visible.

For a fade in/out to take 5000 ms you can do the following calculation for the color:

Fade in:

Code:
PlayerTextDrawColor(playerid, player_TextDraw[playerid][0], 0xFF33CC00 + floatround(float(1250) / float(5000) * 255.0));
Fade out:

Code:
PlayerTextDrawColor(playerid, player_TextDraw[playerid][0], 0xFF33CC00 + 255 - floatround(float(1250) / float(5000) * 255.0));
In this example 5000 is the total time, 1250 is the time in ms that had already passed.

Using float for the calculation is important here, since 1250 / 5000 would be zero if calculated as integer values. floatround will then convert it to an integer value.

Make sure that the time passed is never higher than the time it should take in total, otherwise it will screw up the color as adding 256 to 0xFF33CC00 would change the Blue color channel.

You can use the SetAlpha function Skream suggested too, but it doesn't make a difference in this example.
I have tried understanding what you have just explained above and this is what happens, I coded & followed your instruction exceptions to some parts.

PHP Code:
stock ShowPlayerObjective(playeridcolorstring[], time)
{
    
PlayerTextDrawSetString(playeridplayer_Textdraw[playerid][0], string);
    
PlayerTextDrawColor(playeridplayer_Textdraw[playerid][0], color);
    
PlayerTextDrawShow(playeridplayer_Textdraw[playerid][0]);
    
PlayerInfo[playerid][ptdTimer] = SetTimerEx("TextdrawFadeOut"100true"ddds"playeridcolortimestring);
    print(
"ShowPlayerObjective");
    return 
1;
}
public 
TextdtawFadeOut(playeridcolortimestring[])
{
    
PlayerInfo[playerid][ptdCount] += 100;
    
PlayerTextDrawColor(playeridplayer_Textdraw[playerid][0], color 255 floatround(float(100) / float(time) * 255.0));
    if(
floatround((time 100)) >= floatround(PlayerInfo[playerid][ptdCount]))
    {
        
PlayerTextDrawHide(playeridplayer_Textdraw[playerid][0]);
        
KillTimer(PlayerInfo[playerid][ptdTimer]);
    }
    return 
1;
}
// EG
ShowPlayerObjective(playerid0x33AA3300"Test my nigga"4000); 
And it just displayed a black colored textdraw.
Reply
#12

Quote:
Originally Posted by JaKe Elite
View Post
I have tried understanding what you have just explained above and this is what happens, I coded & followed your instruction exceptions to some parts.

PHP Code:
stock ShowPlayerObjective(playeridcolorstring[], time)
{
    
PlayerTextDrawSetString(playeridplayer_Textdraw[playerid][0], string);
    
PlayerTextDrawColor(playeridplayer_Textdraw[playerid][0], color);
    
PlayerTextDrawShow(playeridplayer_Textdraw[playerid][0]);
    
PlayerInfo[playerid][ptdTimer] = SetTimerEx("TextdrawFadeOut"100true"ddds"playeridcolortimestring);
    print(
"ShowPlayerObjective");
    return 
1;
}
public 
TextdtawFadeOut(playeridcolortimestring[])
{
    
PlayerInfo[playerid][ptdCount] += 100;
    
PlayerTextDrawColor(playeridplayer_Textdraw[playerid][0], color 255 floatround(float(100) / float(time) * 255.0));
    if(
floatround((time 100)) >= floatround(PlayerInfo[playerid][ptdCount]))
    {
        
PlayerTextDrawHide(playeridplayer_Textdraw[playerid][0]);
        
KillTimer(PlayerInfo[playerid][ptdTimer]);
    }
    return 
1;
}
// EG
ShowPlayerObjective(playerid0x33AA3300"Test my nigga"4000); 
And it just displayed a black colored textdraw.
Code:
float(100) / float(time)
is not quite correct. This will indeed always give a low value, and after the full calculation it will be black. You must replace 100 with the time already passed, which will result in

Code:
float(PlayerInfo[playerid][ptdCount]) / float(time)
You should also make sure that PlayerInfo[playerid][ptdCount] never grows higher than time before inserting it into that calculation, that will result in a totally different color.
Reply
#13

Still gives out a black color.
Reply
#14

Quote:
Originally Posted by JaKe Elite
View Post
Still gives out a black color.
With what exact values did you call the function (especially how much time)?

If I do a simple calculation (like 1250 ms passed of a 5000 ms fade) it outputs the correct color:

255 - (1250 / 5000 * 255) = 191.25 (rounded 191 = 0xBF)

0xFF33CC00 + 0xBF = 0xFF33CCBF which is 25% faded - which is correct.

So either you don't update the textdraw (re-show it after setting the color!) or time doesn't hold the correct value. Or something else I didn't see. Could you post your updated code if the bug still persists?
Reply
#15

Quote:
Originally Posted by NaS
View Post
With what exact values did you call the function (especially how much time)?

If I do a simple calculation (like 1250 ms passed of a 5000 ms fade) it outputs the correct color:

255 - (1250 / 5000 * 255) = 191.25 (rounded 191 = 0xBF)

0xFF33CC00 + 0xBF = 0xFF33CCBF which is 25% faded - which is correct.

So either you don't update the textdraw (re-show it after setting the color!) or time doesn't hold the correct value. Or something else I didn't see. Could you post your updated code if the bug still persists?
The time is set out depending on the ShowPlayerObjective time parameter.

PHP Code:
stock ShowPlayerObjective(playeridcolorstring[], time)
{
    
PlayerTextDrawSetString(playeridplayer_Textdraw[playerid][0], string);
    
PlayerTextDrawColor(playeridplayer_Textdraw[playerid][0], color);
    
PlayerTextDrawShow(playeridplayer_Textdraw[playerid][0]);
    
PlayerInfo[playerid][ptdCount] = time;
    
PlayerInfo[playerid][ptdTimer] = SetTimerEx("TextdrawFadeOut"100true"ddds"playeridcolortimestring);
    print(
"ShowPlayerObjective");
    return 
1;
}
public 
TextdrawFadeOut(playeridcolortimestring[])
{
    
PlayerInfo[playerid][ptdCount] += 100;
    
printf("%d | %d"PlayerInfo[playerid][ptdCount], time);
    
PlayerTextDrawColor(playeridplayer_Textdraw[playerid][0], color 255 floatround(float(PlayerInfo[playerid][ptdCount]) / float(time) * 255.0));
    
/*if(floatround(time) >= floatround(PlayerInfo[playerid][ptdCount]))
    {
        PlayerTextDrawHide(playerid, player_Textdraw[playerid][0]);
        KillTimer(PlayerInfo[playerid][ptdTimer]);
    }*/
    
return 1;

EDIT: I have to reshow the textdraw to apply the color, it works fine. Now I have to determine the time passed (base on time parameter) so I can hide and kill the repeating the timer.
Reply
#16

Got it fixed never mind, Thank you for the help guys!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)