Color Transparency -
JaKe Elite - 26.09.2017
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)
Re: Color Transparency -
Skream - 26.09.2017
just decrease and set the alpha
Re: Color Transparency -
JaKe Elite - 26.09.2017
Quote:
Originally Posted by Skream
just decrease and set the alpha
|
How?
(color - 0x99) ??
Re: Color Transparency -
GustavoKarasek - 26.09.2017
https://sampforum.blast.hk/showthread.php?tid=422245
Re: Color Transparency -
AbyssMorgan - 26.09.2017
PHP Code:
((color & 0xFFFFFF00) | (alpha & 0xFF))
Re: Color Transparency -
Skream - 26.09.2017
Quote:
Originally Posted by JaKe Elite
How?
(color - 0x99) ??
|
pawn Code:
#define SetAlpha(%0,%1) (((%0) & 0xFFFFFF00) | ((%1) & 0xFF))
pawn Code:
SetAlpha(color, 0x30) // 2 - alpha
Re: Color Transparency -
JaKe Elite - 26.09.2017
Ahh thank you, I will try that out cheers!
Re: Color Transparency -
OneDay - 26.09.2017
PHP Code:
while (color & 0xFF) --color;
Re: Color Transparency -
JaKe Elite - 26.09.2017
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(playerid, color, string[], time)
{
PlayerTextDrawSetString(playerid, player_Textdraw[playerid][0], string);
PlayerTextDrawColor(playerid, player_Textdraw[playerid][0], color);
PlayerTextDrawShow(playerid, player_Textdraw[playerid][0]);
PlayerInfo[playerid][ptdTimer] = SetTimerEx("TextdrawFadeOut", 100, true, "ddds", playerid, color, time, string);
return 1;
}
public TextdtawFadeOut(playerid, color, time, string[])
{
PlayerInfo[playerid][ptdCount] += 1000;
}
Re: Color Transparency -
NaS - 26.09.2017
The easiest way is taking the color only (without transparency), eg:
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.
Re: Color Transparency -
JaKe Elite - 26.09.2017
Quote:
Originally Posted by NaS
The easiest way is taking the color only (without transparency), eg:
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(playerid, color, string[], time)
{
PlayerTextDrawSetString(playerid, player_Textdraw[playerid][0], string);
PlayerTextDrawColor(playerid, player_Textdraw[playerid][0], color);
PlayerTextDrawShow(playerid, player_Textdraw[playerid][0]);
PlayerInfo[playerid][ptdTimer] = SetTimerEx("TextdrawFadeOut", 100, true, "ddds", playerid, color, time, string);
print("ShowPlayerObjective");
return 1;
}
public TextdtawFadeOut(playerid, color, time, string[])
{
PlayerInfo[playerid][ptdCount] += 100;
PlayerTextDrawColor(playerid, player_Textdraw[playerid][0], color + 255 - floatround(float(100) / float(time) * 255.0));
if(floatround((time / 100)) >= floatround(PlayerInfo[playerid][ptdCount]))
{
PlayerTextDrawHide(playerid, player_Textdraw[playerid][0]);
KillTimer(PlayerInfo[playerid][ptdTimer]);
}
return 1;
}
// EG
ShowPlayerObjective(playerid, 0x33AA3300, "Test my nigga", 4000);
And it just displayed a black colored textdraw.
Re: Color Transparency -
NaS - 26.09.2017
Quote:
Originally Posted by JaKe Elite
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(playerid, color, string[], time)
{
PlayerTextDrawSetString(playerid, player_Textdraw[playerid][0], string);
PlayerTextDrawColor(playerid, player_Textdraw[playerid][0], color);
PlayerTextDrawShow(playerid, player_Textdraw[playerid][0]);
PlayerInfo[playerid][ptdTimer] = SetTimerEx("TextdrawFadeOut", 100, true, "ddds", playerid, color, time, string);
print("ShowPlayerObjective");
return 1;
}
public TextdtawFadeOut(playerid, color, time, string[])
{
PlayerInfo[playerid][ptdCount] += 100;
PlayerTextDrawColor(playerid, player_Textdraw[playerid][0], color + 255 - floatround(float(100) / float(time) * 255.0));
if(floatround((time / 100)) >= floatround(PlayerInfo[playerid][ptdCount]))
{
PlayerTextDrawHide(playerid, player_Textdraw[playerid][0]);
KillTimer(PlayerInfo[playerid][ptdTimer]);
}
return 1;
}
// EG
ShowPlayerObjective(playerid, 0x33AA3300, "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.
Re: Color Transparency -
JaKe Elite - 27.09.2017
Still gives out a black color.
Re: Color Transparency -
NaS - 27.09.2017
Quote:
Originally Posted by JaKe Elite
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?
Re: Color Transparency -
JaKe Elite - 27.09.2017
Quote:
Originally Posted by NaS
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(playerid, color, string[], time)
{
PlayerTextDrawSetString(playerid, player_Textdraw[playerid][0], string);
PlayerTextDrawColor(playerid, player_Textdraw[playerid][0], color);
PlayerTextDrawShow(playerid, player_Textdraw[playerid][0]);
PlayerInfo[playerid][ptdCount] = time;
PlayerInfo[playerid][ptdTimer] = SetTimerEx("TextdrawFadeOut", 100, true, "ddds", playerid, color, time, string);
print("ShowPlayerObjective");
return 1;
}
public TextdrawFadeOut(playerid, color, time, string[])
{
PlayerInfo[playerid][ptdCount] += 100;
printf("%d | %d", PlayerInfo[playerid][ptdCount], time);
PlayerTextDrawColor(playerid, player_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.
Re: Color Transparency -
JaKe Elite - 27.09.2017
Got it fixed never mind, Thank you for the help guys!